랜덤하게 값을 불러올때 - NEWID()

uniqueidentifier 형식의 고유 값을 만듭니다.

Transact-SQL 구문 표기 규칙

구문

NEWID ( )


반환 형식
uniqueidentifier


1. 변수가 있는 NEWID 함수 사용
다음 예에서는 NEWID()를 사용하여 uniqueidentifier 데이터 형식으로 선언된 변수에 값을 할당합니다. uniqueidentifier 데이터 형식 변수의 값은 테스트하기 전에 인쇄됩니다.

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


결과 집합은 다음과 같습니다.

코드 복사
Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF

참고:
NEWID에서 반환된 값은 컴퓨터마다 다릅니다. 설명을 돕기 위해 이 숫자가 표시됩니다.



2. CREATE TABLE 문에서 NEWID 사용
다음 예에서는 uniqueidentifier 데이터 형식으로 cust 테이블을 만들고 NEWID를 사용하여 테이블을 기본값으로 채웁니다. NEWID()의 기본값을 할당할 때 새 행과 기존 행마다 CustomerID 열에 고유 값이 있습니다.

코드 복사
-- 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


3. uniqueidentifier 및 변수 할당 사용
다음 예에서는 @myid라는 로컬 변수를 uniqueidentifier 데이터 형식의 변수로 선언합니다. 그런 다음 SET 문을 사용하여 변수에 값을 할당합니다.

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


참고 항목
참조
NEWSEQUENTIALID()
ALTER TABLE(Transact-SQL)
CAST 및 CONVERT(Transact-SQL)
CREATE TABLE(Transact-SQL)
데이터 형식(Transact-SQL)
시스템 함수(Transact-SQL)
uniqueidentifier(Transact-SQL)
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2007/08/24 10:09 2007/08/24 10:09
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/2624

Trackback URL : http://tcbs17.cafe24.com/tc/trackback/2624

« Previous : 1 : ... 3793 : 3794 : 3795 : 3796 : 3797 : 3798 : 3799 : 3800 : 3801 : ... 6391 : Next »

블로그 이미지

- 홍반장

Archives

Recent Trackbacks

Calendar

«   2024/05   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Statistics Graph

Site Stats

Total hits:
197543
Today:
52
Yesterday:
379