SQL Server 2008 某些新语法

一,如何创建排序表

1.T-SQL创建一个排序表。

CREATE TABLE OrderTable(
      [OrderId]    [int] IDENTITY(1,1) NOT NULL,
      [CustomerId] [int] NOT NULL,
      [OrderTotal] [decimal](18, 0) NOT NULL
)

2.T-SQL插入数据到排序表。

Insert into OrderTable (CustomerId, OrderTotal)
Values      (1,90),
            (2,180),
            (6,540)
3.查看结果,如果是在MSSQL2005中插入数据,会报 ',' 附近有语法错误。
app_img1 
            

 

二,新语句之MERGE,请参考:http://tech.it168.com/db/2007-07-24/200707242111781.shtml

据IT168技术文档上是说当要对2张表进行信息同步时(合并2张表),有三步操作要进行。首先要处理任何需要插入目标数据表的新行。其次是处理需要更新的已存在的行。最后要删除不再使用的旧行。 一个模板如下:

CREATE TABLE [dbo].[CustomerTable](
    [CustomerId] [int] IDENTITY(1,1) NOT NULL,
    [CustomerTotal] [decimal](18, 0) NULL,
    [CustomerName] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[CustomerTable] ADD  DEFAULT ((0)) FOR [CustomerTotal]
GO

ALTER TABLE [dbo].[CustomerTable] ADD  DEFAULT ('') FOR [CustomerName]
GO

现在我们使用MERGE同步数据:

  Merge CustomerTable
  Using ( Select CustomerId, OrderTotal From OrderTable ) As OrderSrc (CustId, Amount)
  On CustomerTable.CustomerId = OrderSrc.CustId
  When MATCHED AND OrderSrc.CustId = 0 THEN 
      DELETE
  When Matched Then
      Update Set CustomerTotal = CustomerTotal + OrderSrc.Amount
  When Not Matched Then
      Insert (CustomerName,CustomerTotal) values (GetDate(), OrderSrc.Amount);

我也没理解,,还是说说目前对次语句的理解:

目标表(需要作用的表)是CustomerTable,源表(参照表)是OrderTable

1.When Matched Then  表示 当2张表有一些共同点,就是说CustomerTable.CustomerId = OrderTable.CustomerId 的时候,OrderTable。CustomerTotal 中的值是 更新到CustomerTable。CustomerTotal 。 其实IT168 也提过,,MERGE就是针对join的。

2.When Not Matched Then  表示 当 OrderTable 中的记录在 CustomerTable 中没有,,就添加新行。

3.When MATCHED AND OrderSrc.CustId = 0 THEN 其实应该是 When Source Not Matched Then, 我不知道为什么 When Source Not Matched Then 会报错,,因此改成了

When MATCHED AND OrderSrc.CustId = 0 THEN, 这个逻辑其实因该是要表示当源表,,也就是OrderTable中一条数据都没有的话,就DELETE CustomerTable….,我这里没写出来这个逻辑。

MERGE虽然强大,但会在目标表中产生无关的数据…..

 

三,内置初始化变量

以前的是:

DECLARE @i int 
SET @i = 10
现在是:
DECLARE @i int = 10

 

 

四,C#数学语法
现在是:
--DECLARE @i int 
--SET @i = 10
SET @i += 10

 

五,微软新引进的 表值参数 Table-Value-Parameters (TVP)

我看到微软ADO的博客写了一个例子:

Create Type Songs_TableType as Table
(Title nvarchar(120) not null,
TrackNumber int)

I can now use this type in a stored procedure to pass a table as a parameter. The following T-Sql shows how to define a stored procedure that takes this type as a parameter. Note that I have skipped error handling for brevity.

create procedure AddSongs(
 @ArtistName nvarchar(120), 
 @AlbumName nvarchar(120), 
 @Songs Songs_TableType READONLY)
as
begin
 -- Add the Artist
 Declare @ArtistID int
 insert into Artists values (@ArtistName)
 select @ArtistID = SCOPE_IDENTITY()

 -- Add the Album
 Declare @AlbumID int
 insert into Albums values (@AlbumName, @ArtistID)
 select @AlbumID = SCOPE_IDENTITY()

 -- Insert songs
 insert into Songs 
 select title, trackNumber, @AlbumID, @ArtistID
 from @Songs
end

但是看得不是很明白,下面是我的简写:

CREATE TYPE MyTableType AS TABLE (CustomerId int, OrderTotal int) 

DECLARE @myTableType  MyTableType  INSERT @myTableType SELECT 6, 7 

Insert into OrderTable  Select CustomerId, OrderTotal from @myTableType

 

完了,另外,有兴趣的可以看看MSDN的网站http://msdn.microsoft.com/zh-cn/library/ms144275.aspx

 

Happy 共享此文 :
posted @ 2008-08-25 13:12  真见  阅读(4172)  评论(10编辑  收藏  举报