Ciel
Ciel

Reputation: 17752

GUID Primary Key not being created

When I add a user to my database, I am using a Guid for the primary key. But it is coming in as just a solid string of 0's. Where am I supposed to set it? I set it to "RowGuid()" in the MS-SQL server...

Upvotes: 0

Views: 177

Answers (3)

Webking
Webking

Reputation: 1862

A little example below how a users table could look with guid as primarykey column:

CREATE TABLE [dbo].[Users](
    [userId] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](255) NOT NULL,
 CONSTRAINT [PK_Users_1] PRIMARY KEY CLUSTERED 
(
    [userId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[Users] ADD  CONSTRAINT [DF_Users_userId]  DEFAULT (newid()) FOR [userId]

Upvotes: 0

Ken White
Ken White

Reputation: 125651

You don't want RowGuid(). You want NewID().

CREATE TABLE myTable(GuidCol uniqueidentifier, NumCol int)
INSERT INTO myTable Values(NEWID(), 4)
SELECT * FROM myTable

Upvotes: 3

Gregoire
Gregoire

Reputation: 24832

try to set the default value of the column to NEWID()

Upvotes: 9

Related Questions