insert uniqueidentifier

My insert looks as follows
"INSERT INTO GROUPS(GROUP_NAME, GROUP_DESCRIPTION, QueryId) VALUES (@GROUP_NAME, @GROUP_DESCRIPTION, @QueryId)"
The table has as primary key column called GroupId of type uniqueiedntifier not nullable.
My insert fails with error
       "Cannot insert the value NULL into column 'GroupId'"
Isn't SQL supposed to generat the value for GroupId by itself ?
LVL 1
vu3lmgAsked:
Who is Participating?
 
BrandonGalderisiConnect With a Mentor Commented:
You could also change the default value of the column to be newid().
create table #TempStuff 
     (ID  uniqueidentifier not null default newid()
     ,a   char(1)
     )
go
insert into #TempStuff (a) values('a')
go
select * from #tempstuff
go
drop table #TempStuff 
go

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:
INSERT INTO GROUPS(GROUPID, GROUP_NAME, GROUP_DESCRIPTION, QueryId) SELECT NEWID(), @GROUP_NAME, @GROUP_DESCRIPTION, @QueryId  
and if you want to use the generated id value: 
DECLARE @newid UNIQUEIDENTIFIER
SET @newid = NEWID()
INSERT INTO GROUPS(GROUPID, GROUP_NAME, GROUP_DESCRIPTION, QueryId) VALUES( @newid, @GROUP_NAME, @GROUP_DESCRIPTION, @QueryId )

Open in new window

0
 
dro_lawCommented:
Or you could just make the GroupID column an identity type and it would automatically insert the value for you. Either way will work.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
i2mentalConnect With a Mentor Commented:
GroupID needs to be set as an  "Identity" field if it's to auto generate.

You can do this through the GUI by Right Clicking the table and selecting design. Highlight the GroupID field and look in the bottom section for "Identity Specification". Expand that and set "Is Identity" to true. This will cause the table to rebuild.

If you don't have access to change the structure, then the above post should at least let you insert
0
 
vu3lmgAuthor Commented:
Dear i2mental
I there is no field type called "Identity".
When field type is set to "uniqueidentifier" it is not letting me set those properties.
I selected field type "int" and set the "Identity Specification" to "yes",  it worked.
Following is my SQL server 2005 details :
_____________________________________________________________
Microsoft SQL Server Management Studio      9.00.3042.00
Microsoft Analysis Services Client Tools            2005.090.3042.00
Microsoft Data Access Components (MDAC)      2000.086.3959.00 (srv03_sp2_rtm.070216-1710)
_____________________________________________________________
 I am looking for SQL to generate the "uniqueidentifier" (or other field type which is a random hash value) automatically.
0
 
BrandonGalderisiCommented:
Then add a default of new() to the column as I suggested or apply it on insert like angelIII suggested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.