November 28, 2011 at 5:55 pm
i have the following table
table1
customer inventory item billingzip
tom, furniture, chair, 17603,
tom, electronics, TV, 91605,
john, furniture, table 75038,
john, sport, ball, 75055,
tim, cosmetics deospray, 17569,
customer inventory item billingzip are the columns and below them are the records
i would like a system generated guid and the there must be only one guid per customer
for example
for both the records for tom the guid should be "30FDB27E-6ADF-4374-A37C-D242C3FF5C0F"
for both the records for john the guid should be "1235ADD4-F60F-4B75-A951-24AE8ED7C2AF"
and for tim guid should be "F7050364-EB8E-4312-8B6A-F27EC009F5AE"
so total 3 distinct guids for 5 records
can any one help me on this one ?
thanks
regards
greg
November 28, 2011 at 6:18 pm
the newid() function and the datatype uniqueidentifier for the column data type is what you are looking for;
you can make it the default value of a table, but i would recommend integer and identity over uniqueidentifer columns.
you need to find the preexisting guid for "tom" before you can use it in a different row... name is not at all unique..i'd consider using an email address instead for the PK, and skip the uniqueidentifier altogether
select newid(),newid(),othervalues
Lowell
November 28, 2011 at 6:19 pm
can you give me as example !!
November 28, 2011 at 6:25 pm
example of what? an insert with newid? the way i would do it?
you'll need to provide a lot more details, i think..what table DDL do you have so far? what are you trying to do? it seems like you really wanted to insert into three different tables...so i need more on what you are trying to accomplsish if you want a solid example.
Lowell
November 29, 2011 at 4:00 am
table with the customers which has a uniqueidentifer column which has a default of newid so each customer has a GUID
then have a foreign key constraint to table 1 which references the GUID, when you do the insert into table 1 select the guid from the customers table and feed it through
November 29, 2011 at 4:47 am
koolme_85 (11/28/2011)
so total 3 distinct guids for 5 recordscan any one help me on this one ?
thanks
regards
greg
Guess based on your data.
BEGIN TRAN
--Sample data as show by the OP
DECLARE @TABLE AS TABLE (customer VARCHAR(4), inventory VARCHAR(11), item VARCHAR(9), billingzip INT,
--Added ID as the place for the GUID to go
ID UNIQUEIDENTIFIER NULL)
INSERT INTO @TABLE
SELECT 'tom', 'furniture', 'chair', 17603, NULL
UNION ALL SELECT 'tom', 'electronics', 'TV', 91605, NULL
UNION ALL SELECT 'john', 'furniture', 'table', 75038, NULL
UNION ALL SELECT 'john', 'sport', 'ball', 75055, NULL
UNION ALL SELECT 'tim', 'cosmetics', 'deospray', 17569, NULL
--First, let's generate GUIDs for each unqiue "customer"
SELECT customer AS cust, NEWID() AS ID
INTO #Temp
FROM @TABLE
GROUP BY customer
--Now update the sample data with the newly generated GUIDs
UPDATE a
SET a.ID = b.ID
FROM @TABLE a
INNER JOIN #Temp b ON cust = customer
--Check the result-set
SELECT * FROM @TABLE
ROLLBACK
December 5, 2011 at 2:59 pm
thanks for all the help guys , i figured it out ,
i took all the distinct values of one column and put them in a temp table and added a ref column to that temp table and gave it a value "NEWID()" i this way i have distinct guid for each record and then i did a inner join with this temp table to my main table ... and it seemed i got what i needed
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply