Board index » delphi » Help Required please.

Help Required please.

Hello Everyone.

We are having a specific requirement in one of our projects .
We are using SQL Server7.0 as our database.
A column in a table in SQL Server can be set with an "identity" property  by
using the user interface .
We wish to do this programmatically.

Any suggestions?

Regards

Bhuvana

 

Re:Help Required please.


I'm am not sure what you want here. Do you want to create a table with code
where one of the fields are Identity or do you want to increment the number
with code every time you do an insert.

Ok I can help you on the last one better that on the first because I can
create tables in code, but don't make a hobby out of it.

If you want to get the next number in the table you wish to use you use can
do it two ways. You can get the ID and store it in a variable, and use the
variable in the insert statement, or you can do it all in one statement. Now
here is examples:

1st choise

var
    NextID: Integer

..
..
SQL.Clear;
SQL.Add('Select case when MAX(Fieldname) is null then 1 else MAX(Fieldname)
+ 1 end from tablename');
Open;
NextID := Fields[0].AsInteger;

or

SQL.Clear
SQL.Add('Insert table 'Select case when MAX(Fieldname) is null then 1 else
MAX(Fieldname) + 1 end, field2, field3 from table');
ExecSQL;

There is two choices. See what yuo can do, and I hope it was of some help to
you.

--
Jaques Burger
Computer Programmer
Tel +27 11 315 8761
Fax +27 11 315 8760
Mobile +27 82 927 0284
email : jaqu...@leverage.co.za

Other Threads