Re: Getting Started
- From: Brian Bushay TeamB <BBushay@xxxxxxxxx>
- Date: Wed, 15 Jun 2005 21:43:09 -0500
>I'm trying to get to grips with MS SQL and ADO at the same time. I've
>got a working method of adding a record to a table with unique
>constraints and getting back an autonumbered, but it seems decidely
>sub-optimal.
>
>I'm pretty sure there'll be a better way than this, can someone point
>me in the right direction?
>
>function TMSSQLImporter.AddLocation(const Location: string): Integer;
>var
> SQLCmd: string;
> Qry: TADOQuery;
>begin
> Qry:=TADOQuery.Create(nil);
> try
> Qry.Connection:=FConnection;
>
> SQLCmd:=Format('Insert Into Location (Location) VALUES
>(''%s'')',[Location]);
You can have multiple SQL commands in SQL
In the code below you have an Insert statement followed by a Select @@Identity
which returns the value placed in the Identity field of the last record
inserted.
If you create SQL with a parameter for the value of location and re-use the
query it is faster than formatting the SQL on the fly each time you do the
insert
Insert Into Location (Location) VALUES(:Location) ;
Select @@Identity
--
Brian Bushay (TeamB)
Bbushay@xxxxxxxxx
.
- Follow-Ups:
- Re: Getting Started
- From: Rik Barker
- Re: Getting Started
- From: Rik Barker
- Re: Getting Started
- References:
- Getting Started
- From: Rik Barker
- Getting Started
- Prev by Date: Re: TADOQuery and Date fields problem
- Next by Date: Re: Command object with named Parameters?
- Previous by thread: Getting Started
- Next by thread: Re: Getting Started
- Index(es):
Relevant Pages
|