常用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
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
相关文章推荐
- 常用sql功能
- 常用几种数据库,取前10条记录的sql语句写法
- 地图常用标准功能
- DB2的一些常用SQL写法(转)
- 常用SQL功能语句
- CSS3常用功能的写法
- 常用的标准SQL 语句
- 常用sql写法
- 个人收藏SQL常用功能
- CSS3常用功能的写法
- DB2常用SQL的写法
- [DB] 数据库SQL语句标准写法
- SQL常用功能
- 常用几种数据库,取前10条记录的sql语句写法
- 常用几种数据库,取前10条记录的sql语句写法
- 标准Sql语句的写法(一)
- SQL中存储过程和函数的标准写法
- 常用数据库 取前几条数据 sql写法
- [DB] 数据库SQL语句标准写法
- 日常常用功能-tab切换-原生js和jQuery两种写法,同一思路