Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet305/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   301   302   303   304   305   306   307   308   ...   443
Bog'liq
BookSQL

 

CHAPTER 8

 

Data Modification



 

 259

If you don’t need to generate the new sequence value before using it, you can specify the NEXT 



VALUE FOR function directly as part of your INSERT statement, like this.

INSERT INTO dbo.T1(keycol, datacol) 

  VALUES(NEXT VALUE FOR dbo.SeqOrderIDs, 'b'); 

 

SELECT * FROM dbo.T1;



This code returns the following output.

keycol      datacol 

----------- ---------- 

2           a 

3           b

Unlike with identity, you can generate new sequence values in an UPDATE statement, like this.

UPDATE dbo.T1 

  SET keycol = NEXT VALUE FOR dbo.SeqOrderIDs; 

 

SELECT * FROM dbo.T1;



This code returns the following output.

keycol      datacol 

----------- ---------- 

4           a 

5           b

To get information about your sequences, query a view called sys.sequences. For example, to find 

the current sequence value in the SeqOrderIDs sequence, you would use the following code.

SELECT current_value 

FROM sys.sequences 

WHERE OBJECT_ID = OBJECT_ID('dbo.SeqOrderIDs');

This code generates the following output.

current_value 

-------------- 

5

SQL Server extends its support for the sequence option with capabilities beyond what the com-



petitors and the standard currently support. One of the extensions enables you to control the order of 

the assigned sequence values in a multi-row insert by using an OVER clause similar to the one window 

functions use. Here’s an example.

INSERT INTO dbo.T1(keycol, datacol) 

  SELECT 

    NEXT VALUE FOR dbo.SeqOrderIDs OVER(ORDER BY hiredate), 

    LEFT(firstname, 1) + LEFT(lastname, 1) 

  FROM HR.Employees; 

 

SELECT * FROM dbo.T1;



www.it-ebooks.info


260  

Microsoft SQL Server 2012 T-SQL Fundamentals

This code returns the following output.

keycol      datacol 

----------- ---------- 

4           a 

5           b 

6           JL 

7           SD 

8           DF 

9           YP 

10          SB 

11          PS 

12          RK 

13          MC 

14          ZD

Another extension allows the use of the NEXT VALUE FOR function in a default constraint. Here’s 

an example.

ALTER TABLE dbo.T1 

  ADD CONSTRAINT DFT_T1_keycol 

    DEFAULT (NEXT VALUE FOR dbo.SeqOrderIDs) 

    FOR keycol;

Now when you insert rows into the table, you don’t have to indicate a value for keycol.

INSERT INTO dbo.T1(datacol) VALUES('c'); 

 

SELECT * FROM dbo.T1;



This code returns the following output.

keycol      datacol 

----------- ---------- 

4           a 

5           b 

6           JL 

7           SD 

8           DF 

9           YP 

10          SB 

11          PS 

12          RK 

13          MC 

14          ZD 

15          C

This is a great advantage over identity—you can add a default constraint to an existing table and 

remove it from an existing table as well.

Finally, another extension allows you to allocate a whole range of sequence values at once by using 

a stored procedure called sp_sequence_get_range. The idea is that if the application needs to assign a 

range of sequence values, it is easiest to update the sequence only once, incrementing it by the size of 

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   301   302   303   304   305   306   307   308   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish