举几个例子来简单说明 这些例子我引用了northwind库。 1. top 表达式 特别,通过表达式还支持递归。 web service就发布好了,敲入http://localhost/sql/orders?wsdl得到wsdl 新闻热点 疑难解答
sql server 2000的top是个固定值,是不是觉得不爽,现在改进了。
declare @n int
set @n = 10
select top(@n) * from orders
2. 分页
不知各位过去用sql server 2000是怎么分页的,大多都用到了临时表。sql server 2005一句话就支持分页,性能据说也非常不错。
select * from(
select orderid, freight, row_number() over(order by freight) as row from orders
) a
where row between 20 and 30
3. 排名
select orderid, freight, rank() over(order by freight) as rank from orders
) a
where rank between 20 and 30
4. try ... catch
sql server 2000没有异常,t-sql必须逐行检查错误代码,对于习惯了try catch程序员,2005是不是更加亲切:
begin try
begin tran
insert into orders(customerid) values(-1)
commit tran
print 'commited'
end try
begin catch
rollback
print 'rolled back'
end catch
5. 通用表达式cte
通过表达式可免除你过去创建临时表的麻烦。
with orderfreight as(
select orderid, freight, row_number() over(order by freight) as row from orders
)
select orderid, freight from orderfreight where row between 10 and 20
6. 直接发布web service
想要把store procedure变成web service就用这个吧,.net, iis都不需要,通过windows 2003的http protocol stack直接发布webservice,用这个功能需要windows 2003 sp1
create endpoint orders_endpoint
state=started
as http(
path='/sql/orders',
authentication=(integrated),
ports=(clear)
)
for soap(
webmethod 'custordersorders'(
name='northwind.dbo.custordersorders'
),
wsdl=default,
database='northwind',
namespace='http://mysite.org/'
)
给个客户端例子。
{
localhost.orders_endpoint endp = new localhost.orders_endpoint();
endp.credentials = system.net.credentialcache.defaultcredentials;
object[] r = endp.custordersorders("vinet");
dataset ds = new dataset();
foreach(object o in r)
if (o is dataset) ds = (dataset)o;
datagridview1.datasource = ds.tables[0];
}