您的位置:首页 > 数据库

常用sql功能标准写法

2008-12-02 15:36 344 查看
函数标准写法
create Function [dbo].[FunctionName]
(@parem1 Varchar(50),
@parem2 datetime,
@parem3 int)
ReturnS nVarchar(4000)
Begin
declare @eventstr nVarchar(4000)
set @eventstr=''
return @eventstr
End
go
存储过程标准写法
CREATE PROCEDURE dbo.procName
@parem1 int,
@parem2 varchar(50),
@parem3 varchar(50),
@parem4 varchar(50) output
AS
Begin

End
GO
赋权语句
GRANT EXECUTE/select/delete/update/insert ON [dbo].objectname TO [public]

游标写法
声明游标:
declare titprice CURSOR Local FAST_FORWARD for
select title, price from dbo.titles where price<15

打开游标:
open titprice

循环取质
fetch next from titprice into @strtitle, @strprice
while @@fetch_status=0
begin
set @str=@str+@strtitle+': '+Convert(varchar(20),@strprice)
fetch next from titprice into @strtitle,@strprice
end

关闭游标
close titprice
释放游标
DEALLOCATE titprice

print @str

游标其它
--定位到当前记录相对位置记录
fetch relative -88 from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到当前记录前一条
fetch prior from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到当前记录后一条
fetch next from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到首记录
fetch first from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address
--定位到尾记录
fetch last from my_cursor into @id,@name,@address
select @id as id,@name as name,@address as address

触发器
答案1:
use northwind
go
create trigger cust_orders_del1
on Cust_test
after delete
as
delete from order_test
where CustomerID in
(select CustomerID from deleted)
go

答案2:
use northwind
go
create trigger cust_orders_ins2
on order_test
after insert
as
if (select cstatus from cust_test,inserted where

cust_test.customerid=inserted.customerid)=1
begin
print 'The Goods is being processed'
rollback transaction
end
go
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: