?
Solved

insert uniqueidentifier

Posted on 2008-11-12
6
Medium Priority
?
1,522 Views
Last Modified: 2012-05-05
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 ?
0
Comment
Question by:vu3lmg
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22945000
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
 
LVL 3

Expert Comment

by:dro_law
ID: 22945095
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
 
LVL 8

Assisted Solution

by:i2mental
i2mental earned 800 total points
ID: 22945119
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 1200 total points
ID: 22945127
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22949759
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
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22949912
Then add a default of new() to the column as I suggested or apply it on insert like angelIII suggested.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question