SQL Server 2008 New T-SQL shorthand syntax

Source: Internet
Author: User
Tags null null

1. You can assign values directly when defining variables

DECLARE @Id int = 5

2.Insert statements can insert multiple rows of data at once

INSERT into StateList VALUES (@Id, ' WA '), (@Id + 1, ' FL '), (@Id + 2, ' NY ')

3. Support + = operator

SET Stateid + = 1

The output function in SQL Server 2005 can return the contents of both insert,update and delete, and the output in 2008 also has this function. Here's how to use the new T-SQL statement merge combination of output with 2008:

Create the following table:

[C-sharp]View Plaincopy
  1. CREATE TABLE Book (
  2. ISBN varchar (PRIMARY KEY),
  3. Price Decimal,
  4. Shelf int)
  5. CREATE TABLE Weeklychange (
  6. ISBN varchar (PRIMARY KEY),
  7. Price Decimal,
  8. Shelf int)
  9. CREATE TABLE Bookhistory (
  10. Action nvarchar (10),
  11. NEWISBN varchar (20),
  12. Newprice Decimal,
  13. newshelf int,
  14. OLDISBN varchar (20),
  15. Oldprice Decimal,
  16. oldshelf int,
  17. Archivedat datetime2)

The SQL statement is

[C-sharp]View Plaincopy
    1. MERGE Book as B
    2. USING Weeklychange as WC
    3. On B.ISBN = WC. Isbn
    4. When matched and (B.price <> WC. Price OR b.shelf <> WC. Shelf) Then
    5. UPDATE SET b.price = WC. Price, b.shelf = WC. Shelf
    6. When isn't matched then
    7. INSERT VALUES (WC. ISBN, WC. Price, WC. Shelf)
    8. OUTPUT $action, inserted.*, deleted.*, Sysdatetime ()
    9. into Bookhistory;

The result set is:

SELECT * from Bookhistory
GO

Action newisbn newprice newshelf oldisbn oldprice oldshelf archivedat
------ ------- -------- -------- ------- -------- -------- ---------------------------
UPDATE a 101 1 a 100 1 2007-11-25 14:47:23.9907552
INSERT C 3 null NULL NULL 2007-11-25 14:47:23.9907552

There are two output cases of INSERT and update. If you only need one of these, you can filter it in the following way:

[C-sharp]View Plaincopy
  1. INSERT into book (ISBN, Price, Shelf, Archivedat)
  2. SELECT ISBN, Price, Shelf, GETDATE () from
  3. (MERGE book as B
  4. USING Weeklychange as WC
  5. On B.ISBN = WC. ISBN and B.archivedat is NULL
  6. When matched and (B.price <> WC. Price OR b.shelf <> WC. Shelf) Then
  7. UPDATE SET price = WC. Price, Shelf = WC. Shelf
  8. When isn't matched then
  9. INSERT VALUES (WC. ISBN, WC. Price, WC. Shelf, NULL)
  10. OUTPUT $action, WC. ISBN, Deleted.price, deleted.shelf
  11. ) Changes (Action, ISBN, Price, Shelf)
  12. WHERE Action = ' UPDATE ';

SQL Server 2008 New T-SQL shorthand syntax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.