|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sql Uniqueidentifier GUIDS
Hi all,
I posted a few threads earlier, but have since discovered my problem relates to "uniqueidentifiers". I have a Users table which has a uniqueidentifier called "UserID" as the PK. I have created a new table and I want to use the "UserID" from my Users table as the my FK. Through many hours of searching the net, it seems these are called GUIDs................... does anyone know how to reference this "UserID" for my new table??????? It does not insert automatically!!!!!! Thanks |
|
#2
|
|||
|
|||
|
The SQL Server function you are looking for to generate a uniqueidentifier is NEWID(). Use this as the default for that column or in an insert statement, that is VALUE (NEWID()). The data type to specify when using this as a foreign key is uniqueidentifier but just don't specify a default.
Hope this helps. |
|
#3
|
|||
|
|||
|
Hi Trubollota
That is exactly what I have done, but for some reason I keep getting the error that a NULL value cannot be inserted into this column. My thinking was that SQL handles the inserting of this column based on the PK and FK setup! Am I correct in thinking this is the right way or am I still missing a step ie do I have to ALTER the procedure manually or something????? It's driving me nuts!!! Smile!
Thanks for the reply! Quote:
|
|
#4
|
|||
|
|||
|
You may want to examine the following scripts. I created both tables manually setting the default value of UserID in MyUserTBL to NEWID().
Code:
/****** Object: Table [dbo].[MyUserTBL] Script Date: 5/15/05 5:34:27 PM ******/ CREATE TABLE [dbo].[MyUserTBL] ( [UserID] [uniqueidentifier] NOT NULL , [UserName] [varchar] (32) NOT NULL ) ON [PRIMARY] GO /****** Object: Table [dbo].[MyOtherTBL] Script Date: 5/15/05 5:34:28 PM ******/ CREATE TABLE [dbo].[MyOtherTBL] ( [MOTPKID] [int] IDENTITY (1, 1) NOT NULL , [UserFKID] [uniqueidentifier] NOT NULL , [SomeVarCharData] [varchar] (32) NOT NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[MyUserTBL] WITH NOCHECK ADD CONSTRAINT [DF_MyUserTBL_UserID] DEFAULT (newid()) FOR [UserID], CONSTRAINT [PK_MyUserTBL] PRIMARY KEY NONCLUSTERED ( [UserID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[MyOtherTBL] WITH NOCHECK ADD CONSTRAINT [PK_MyOtherTBL] PRIMARY KEY NONCLUSTERED ( [MOTPKID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[MyOtherTBL] ADD CONSTRAINT [FK_MyOtherTBL_MyUserTBL] FOREIGN KEY ( [UserFKID] ) REFERENCES [dbo].[MyUserTBL] ( [UserID] ) GO The tables are very simple and you may want to compare them to yours. I added sample data to MyUserTBL just adding some names and then ran the following insert to move the data to MyOtherTBL: Code:
INSERT INTO dbo.MyOtherTBL ( UserFKID, SomeVarCharData ) SELECT dbo.MyUserTBL.UserID, dbo.MyUserTBL.UserName FROM dbo.MyUserTBL and it worked, correctly inserting the UserFKID foreign key from UserID and creating the new primary key. This at least establishes it can be done. |
|
#5
|
|||
|
|||
|
Hi Trubollota
Thanks a lot for that extremely detailed solution!
I am currently trying to implement a variation of it! Will post a victory or failure cry! Thanks again Quote:
|
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > Sql Uniqueidentifier GUIDS |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|