Page 1 of 1

Sql Server Unique Identifier

Posted: Mon Sep 17, 2012 10:47 am
by devsonali
Hello Gurus

Here is the problem

I need to insert Columns A and B from say table T1 to Table T2 . Table T2 has primary key defined as column PK1(GUID) .

Now lets say I have one row corresponding to Column A and B from T1 that I need to insert into T2 .
When I do the usual insert into Table T2 (Sql Server ) , Should I be using User Defined SQL to load into T2, if yes how would I take care of the GUID (PK1) here ?
Any help is appreciated .

I have searched for this topic with Key works GUID and SQL Server Unique ID but could not find any direct answers .

Posted: Mon Sep 17, 2012 11:25 am
by devsonali
Missed to add this information :

When I use insert option instead of user defined SQL

It fails with " Cannot insert the value NULL into column . "

Posted: Mon Sep 17, 2012 1:46 pm
by chulett
I'm an Oracle person but don't you need to use custom SQL and include a call to newid() in there for that column? :?

Posted: Mon Sep 17, 2012 3:16 pm
by devsonali
Yup , I we would need one (I am new to sql server too ) but what about metadata , Should this pk be the part of metadata ? (If Yes then since there is no value for this new column coming in it wouldn't compile ?)
or should it be like insert into table T1 ( pk1,a,b ) VALUES (newid(),'value1','Value2')

Posted: Mon Sep 17, 2012 5:10 pm
by chulett
No, don't include the PK in the metadata or (I assume) it will want to bind an input column to it - and you won't have one. Your example insert should be pretty close to the mark.

Posted: Tue Sep 18, 2012 8:23 am
by ArndW
Does the SQL-Server DDL specify that the key column has "IDENTITY" set to on? If so, you may not specify that key column on writing; if not, then you need to fill that column with a value at runtime.