Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql insert statement with guid column

Status
Not open for further replies.

threeo

Programmer
Mar 31, 2005
49
US
anyone know how.....

i have a sql server table i am trying to do an insert to...
the table's last column is a unique identifier guid column

so....

when i build my insert statement....what do i put for that column?

since it's generated by sql server....i don't know what to put and it won't allow me to put nothing.
 
You would do the same as if doing an insert into a table with an identity.

Insert Into <table>(col1, col2, col3 GUIDCol)
Values (val1, val2, val3)
 
I am sorry, I have to amend my last post, you need to use this syntax:

Code:
Insert Into <table>(col1, col2, col3 GUIDCol)
Values (val1, val2, val3, NewID())
 
are you saying....
your table has three columns or four?
if you have 4...won't it choke on the missing comma between "col3" and "GUIDCol"?

here's my insert statement:
INSERT INTO StackTab(recId, op_id, ctl_plan_no, rowguid)
VALUES (1, '1164618', '0520040256',???)

i have no idea what to put in place of the ???
 
in sql server, the uniqueidentifier column is already set to perform the function NewID() - so i shouldn't need it in my query statement
 
What do you mean it is already set? Do you mean it is set as a Default? If so your statment is true, otherwise you explicitly have to call the NewID() function.
 
yes - as a default - when i look at the properties in sql server for that column, the default value for that column is (newid())

so....
that being the case.....

how would one build an insert statement?
 
As I said before: Assuing your table has 4 columns and the last(col4) is the GUID column:

Code:
   Insert Into <table>(col1, col2, col3)
   Values (val1, val2, val3)

You don't define the column in the insert list and you don't specify a value. On an Insert, the NewID() function will be called. Again, this works similar to and identity, you don't specify the column or value on an Insert.
 
Sorry I did not see your previous code: Here is what you should use for your table:

Code:
   INSERT INTO StackTab(recId, op_id, ctl_plan_no, rowguid)
   VALUES (1, '1164618', '0520040256')
 
Sorry.. cut and paste problem... remove the rowguid from the insert list

Code:
   INSERT INTO StackTab(recId, op_id, ctl_plan_no)
   VALUES (1, '1164618', '0520040256')
 
No problem, glad to help, sorry for all the fixes at the end. Anyway, I hope you are understanding how it is working.
 
Yeah, if you have a default value set on a column, you wouldn't even mention it in your update/insert statements. The database server will supply a value itself.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top