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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
i2mentalCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

BrandonGalderisiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.