Monday, March 26, 2012

Query

Hello folks,

Im new in database bussines. I will appreciate if someone can help me.
I need to insert new records to a table. First of all I dont know what is the syntax for insert command in SQL Server2000. Also one of the fields to insert is changing every second so I think I will need to include in the insert command a nested select statement.
For example,

The table has the ID field and this field is the one is changing every time. If in the n second I do a select to know what value such field has I could use the value equal to ID + 1. But while I build query, type and execute it will be the second n+1, so probably the value for ID now is already equal to ID + 1 and my query will fail because the duplicated record error.

I hope is clear what the proble I have,

Regards,You can look in BooksOnline (comes with the sql server) for the syntax on insert statements.
If your changing field is the ID for a record, you can use auto numbering on this field. This means that sql server itself will keep track of the value of the field and makes sure there are no duplicates.

hth|||So can I just use:

insert into table_name values(ID,'MNAME','F_NAME')...

I was thinking that some other query was needed to be introduced to get the new value for ID field, ie.

insert into table_name values(((select ID from TABLE)+1),'M_NAME','F_NAME')

Thanks|||Not completely correct yet, your insert statement. If the ID column is set to auto numbering, you don't include it in the insert statement. Your statement will be:
insert into table_name values('MNAME','F_NAME')|||Thanks, How I can know if the field is autonumbering?

How I can get the table fields with all it properties using SQL Command?

In db2 it was with command describe table...
It is the same?

Regards,|||in sql server, there is a stored proeedure for this information called 'sp_columns'. See BOL for more information. You can also find information about using auto numbering in BOL, just search on 'identity'.sql

No comments:

Post a Comment