NEWID (Transact-SQL)

uniqueidentifier türünde benzersiz bir değer oluşturur.

Konu bağlantısı simgesi Transact-SQL Sözdizim Kuralları

Sözdizimi

NEWID ( )

Dönüş Türleri

uniqueidentifier

Örnekler

A.NEWID işlevini bir değişkenle kullanma

Aşağıdaki örnek, uniqueidentifier veri türünde bildirilmiş bir değişkene bir değer atamak için NEWID() işlevini kullanmaktadır. uniqueidentifier veri türünde değişkenin değeri, değer sınanmadan ekrana yazılır.

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

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

Sonuç kümesi buradadır.

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

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

[!NOT]

NEWID'nin döndürdüğü değer her bilgisayar için farklıdır. Bu sayı yalnızca örnek olarak verilmiştir.

B.NEWID'yi bir CREATE TABLE deyiminde kullanma

Aşağıdaki örnek, uniqueidentifier veri türüyle bir cust tablosu oluşturmakta ve tabloyu varsayılan bir değerle doldurmak için NEWID'yi kullanmaktadır. NEWID()'nin varsayılan değeri atanarak her yeni ve var olan satırın CustomerID sütununda benzersiz bir değeri olur.

-- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
 CustomerID uniqueidentifier NOT NULL
   DEFAULT newid(),
 Company varchar(30) NOT NULL,
 ContactName varchar(60) NOT NULL, 
 Address varchar(30) NOT NULL, 
 City varchar(30) NOT NULL,
 StateProvince varchar(10) NULL,
 PostalCode varchar(10) NOT NULL, 
 CountryRegion varchar(20) NOT NULL, 
 Telephone varchar(15) NOT NULL,
 Fax varchar(15) NULL
)
GO
-- Inserting data into cust table.
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince, 
 PostalCode, CountryRegion, Telephone, Fax)
VALUES
(NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
 '90110', 'Finland', '981-443655', '981-443655')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
PostalCode, CountryRegion, Telephone, Fax)
VALUES 
(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
 '08737-363', 'Brasil', '(14) 555-8122', '')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, 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
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, Telephone, Fax)
VALUES 
(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
 '8010', 'Austria', '7675-3425', '7675-3426')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, 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

-- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
 CustomerID uniqueidentifier NOT NULL
   DEFAULT newid(),
 Company varchar(30) NOT NULL,
 ContactName varchar(60) NOT NULL, 
 Address varchar(30) NOT NULL, 
 City varchar(30) NOT NULL,
 StateProvince varchar(10) NULL,
 PostalCode varchar(10) NOT NULL, 
 CountryRegion varchar(20) NOT NULL, 
 Telephone varchar(15) NOT NULL,
 Fax varchar(15) NULL
)
GO
-- Inserting data into cust table.
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince, 
 PostalCode, CountryRegion, Telephone, Fax)
VALUES
(NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
 '90110', 'Finland', '981-443655', '981-443655')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
PostalCode, CountryRegion, Telephone, Fax)
VALUES 
(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
 '08737-363', 'Brasil', '(14) 555-8122', '')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, 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
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, Telephone, Fax)
VALUES 
(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
 '8010', 'Austria', '7675-3425', '7675-3426')
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
 PostalCode, CountryRegion, 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.uniqueidentifier ve değişken atama kullanma

Aşağıdaki örnek, uniqueidentifier türünde bir değişken olarak @myid adlı yerel bir değişken bildirmektedir. Sonra, SET deyimi kullanılarak bu değişkene bir değer atanmaktadır.

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

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

Ayrıca bkz.

Başvuru

NEWSEQUENTIALID() (Transact-sql)

ALTER TABLE (Transact-SQL)

CAST ve CONVERT (Transact-SQL)

Tablo (Transact-sql) oluştur

Veri Türleri (Transact-SQL)

Sistem işlevler (Transact-sql)

benzersiz tanımlayıcı (Transact-SQL)

Kavramlar

Sıra numaraları