Re: Getting Started




>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
.



Relevant Pages

  • Getting Started
    ... I'm trying to get to grips with MS SQL and ADO at the same time. ... got a working method of adding a record to a table with unique ... function TMSSQLImporter.AddLocation(const Location: string): Integer; ...
    (borland.public.delphi.database.ado)
  • Non updatable query & duped IDs
    ... There is some VBA code (Access2k, Win2k, ODBC linked SQL ... Server tables) that has run perfectly for a weekly process ... The table had an identity field but no ... Why is a non-duplicated id field required to do lookups? ...
    (microsoft.public.access.modulesdaovba)
  • SQL Insert that return Identity, any samples
    ... I am looking for SQL Insert sample which returns the newly created id.I am ... using HTTP adapter to send an xml message that contains 1 Order and many ... the field Order.Id because it is an identity field.If i ...
    (microsoft.public.biztalk.general)
  • Re: IDENTITY Data Type Gaps
    ... > We recently upgraded an Access XP database to SQL Server, ... In the original Access database, ... to converting to SQL Server.) ... Identity field value. ...
    (microsoft.public.sqlserver.odbc)
  • Re: Creating an Auto-Number and Currency data type in SQL
    ... indeed have an auto-number type called and Identity field. ... is essentially the same as MSAccess AutoNumber. ... the SQL Server Books Online is a great source of info. ...
    (microsoft.public.vb.database.ado)