views:

63

answers:

3

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...

+9  A: 

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

Gregoire
+3  A: 

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

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

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]
QuBaR