Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 Unique identifier

Author  Topic 

husseinj
Starting Member

8 Posts

Posted - 2004-08-16 : 01:05:30
Hi everyone,
Can you please tell me what value must i enter in a column which has the data type "Unique Identifier"
Thank you for your help.
Does sql server has autonumber?
Is there any other solution to replace autonumber?

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-08-16 : 01:13:39
You need to download Books Online. :)


NEWID
Creates a unique value of type uniqueidentifier.

Syntax
NEWID ( )

Return Types
uniqueidentifier

Examples
A. Use the NEWID function with a variable
This example uses NEWID to assign a value to a variable declared as the uniqueidentifier data type. The value of the uniqueidentifier data type variable is printed before the value is tested.

-- Creating a local variable with DECLARE/SET syntax.
DECLARE @myid uniqueidentifier
SET @myid = NEWID()
PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)

Here is the result set:

Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF



Note The value returned by NEWID is different for each computer. This number is shown only for illustration.


B. Use NEWID in a CREATE TABLE statement
This example creates cust table with a uniqueidentifier data type, and uses NEWID to fill the table with a default value. In assigning the default value of NEWID(), each new and existing row has a unique value for the cust_id column.

-- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
cust_id uniqueidentifier NOT NULL
DEFAULT newid(),
company varchar(30) NOT NULL,
contact_name varchar(60) NOT NULL,
address varchar(30) NOT NULL,
city varchar(30) NOT NULL,
state_province varchar(10) NULL,
postal_code varchar(10) NOT NULL,
country varchar(20) NOT NULL,
telephone varchar(15) NOT NULL,
fax varchar(15) NULL
)
GO
-- Inserting data into cust table.
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
'90110', 'Finland', '981-443655', '981-443655')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
'08737-363', 'Brazil', '(14) 555-8122', '')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,
'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
'8010', 'Austria', '7675-3425', '7675-3426')
INSERT cust
(cust_id, company, contact_name, address, city, state_province,
postal_code, country, telephone, fax)
VALUES
(newid(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68')
GO

C. Use uniqueidentifier and variable assignment
This example declares a local variable called @myid as a variable of uniqueidentifier data type. Then, the variable is assigned a value using the SET statement.

DECLARE @myid uniqueidentifier
SET @myid = 'A972C577-DFB0-064E-1189-0154C99310DAAC12'
GO


See Also

ALTER TABLE

CAST and CONVERT

CREATE TABLE

Data Types

Replication Overview

System Functions

uniqueidentifier

©1988-2004 Microsoft Corporation. All Rights Reserved.


The uniqueidentifier is one of many alternatives to autonumber. It depends on how you design your database and what you're trying to achieve. The SQL Server version of autonumber is the identity column. You can also find out all about it in Books Online, which is free.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

jeffreymfischer
Starting Member

10 Posts

Posted - 2009-10-07 : 11:16:13
I've written a detailed article on an enterprise-ready unique identifier solution.

http://blog.scoftware.com/post/2009/08/29/SQL-Server-UniqueIdentifier-Globally-Unique-Globally-Sequential-SOLUTION.aspx

Read it and provide feedback.

Jeff Fischer

Scoftware Achitect/Developer
http://blog.scoftware.com

Scoftware Achitect/Developer
http://blog.scoftware.com
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-10-07 : 11:55:06
"uniquity"???

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page
   

- Advertisement -