34,590
社区成员
发帖
与我相关
我的任务
分享
MSSQL2008新增功能
用一条语句添加多条记录:
create table t1
(
id int
)
insert into t1 (id) values (1),(3),(5),(7)
可以通过元素数组方式。
定义变量的同时可以赋值:
declare @max int=(select MAX(id) from t1)
select @max
使用VALUE语句生成表值函数
select * from
(
values
(10003,'20090213',4,'B'),
(10004,'20090214',1,'A'),
(10005,'20090213',1,'C'),
(10006,'20090215',3,'C')
) AS t1(orderid,orderdate,empid,custid)
结果:
其中t1是虚拟表的别名。
支持复合赋值运算符:
+=(相加并赋值);
-=(相减并赋值);
*=(相乘并赋值);
/=(相除并赋值)。
示例:
qty=qty+5可以写成qty+=5
MERGE语句:
示例:
merge into dbo.temporders a
using TSQLFundamentals2008.sales.orders b
on a.orderid=b.orderid
when matched then
update set a.empid=b.empid
when not matched then
insert (orderid,orderdate,empid,custid)
values (b.orderid,b.orderdate,b.empid,b.custid)
when not matched by source
delete;
在这个示例中,dbo.temporders是要操作的目标表,TSQLFundamentals2008.sales.orders是来源表。
when matched then子句中定义当目标表中的一行和来源表中的一行能够匹配时进行的操作;
when not matched then子句中定义当目标表中没有行能够与来源表中的行进行匹配时进行的操作。
when not matched by source子句中定义当目标表中的某一行在来源表中找不到匹配时进行的操作。
还可以使用AND选项在以上子句后面增加条件。
表类型:
创建一个表类型,以后在定义表变量的时候,就可以直接使用该表类型。
创建表类型:
create type dbo.OrderTotalsByYear as table
(
orderyear int not null primary key,
qty int not null
)
定义一个表变量直接使用表类型:
declare @MyOrderTotalsByYear as dbo.OrderTotalsByYear
insert into @MyOrderTotalsByYear (orderyear,qty)
select YEAR(a.orderdate) as orderyear,SUM(b.qty) as qty from sales.Orders a inner join sales.OrderDetails b on a.orderid=b.orderid group by YEAR(a.orderdate)
select * from @MyOrderTotalsByYear
MSSQL2008新增功能
用一条语句添加多条记录:
create table t1
(
id int
)
insert into t1 (id) values (1),(3),(5),(7)
可以通过元素数组方式。
定义变量的同时可以赋值:
declare @max int=(select MAX(id) from t1)
select @max
使用VALUE语句生成表值函数
select * from
(
values
(10003,'20090213',4,'B'),
(10004,'20090214',1,'A'),
(10005,'20090213',1,'C'),
(10006,'20090215',3,'C')
) AS t1(orderid,orderdate,empid,custid)
结果:
其中t1是虚拟表的别名。
支持复合赋值运算符:
+=(相加并赋值);
-=(相减并赋值);
*=(相乘并赋值);
/=(相除并赋值)。
示例:
qty=qty+5可以写成qty+=5
MERGE语句:
示例:
merge into dbo.temporders a
using TSQLFundamentals2008.sales.orders b
on a.orderid=b.orderid
when matched then
update set a.empid=b.empid
when not matched then
insert (orderid,orderdate,empid,custid)
values (b.orderid,b.orderdate,b.empid,b.custid)
when not matched by source
delete;
在这个示例中,dbo.temporders是要操作的目标表,TSQLFundamentals2008.sales.orders是来源表。
when matched then子句中定义当目标表中的一行和来源表中的一行能够匹配时进行的操作;
when not matched then子句中定义当目标表中没有行能够与来源表中的行进行匹配时进行的操作。
when not matched by source子句中定义当目标表中的某一行在来源表中找不到匹配时进行的操作。
还可以使用AND选项在以上子句后面增加条件。
表类型:
创建一个表类型,以后在定义表变量的时候,就可以直接使用该表类型。
创建表类型:
create type dbo.OrderTotalsByYear as table
(
orderyear int not null primary key,
qty int not null
)
定义一个表变量直接使用表类型:
declare @MyOrderTotalsByYear as dbo.OrderTotalsByYear
insert into @MyOrderTotalsByYear (orderyear,qty)
select YEAR(a.orderdate) as orderyear,SUM(b.qty) as qty from sales.Orders a inner join sales.OrderDetails b on a.orderid=b.orderid group by YEAR(a.orderdate)
select * from @MyOrderTotalsByYear