加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlServer游标、存储过程及数据块执行

发布时间:2020-12-12 14:06:01 所属栏目:MsSql教程 来源:网络整理
导读:数据块游标事例如下: begindeclare @item_code varchar(32)--定义变量declare @item_name varchar(32)declare @invest_money_sum float--定义游标declare my_cursor cursor forselect item_code,item_name,invest_money_sum from zftz_project_sheji_result

数据块游标事例如下:

begin
	declare @item_code varchar(32)--定义变量
	declare @item_name varchar(32)
	declare @invest_money_sum float
	--定义游标
	declare my_cursor cursor 
	for
	select item_code,item_name,invest_money_sum from zftz_project_sheji_result
	--打开游标
	open my_cursor
	fetch next from my_cursor into @item_code,@item_name,@invest_money_sum
	while(@@fetch_status=0)
	begin
		update zftz_project_item_info1 set mainamount=@invest_money_sum where code=@item_code 
		fetch next from my_cursor into @item_code,@invest_money_sum
	end
	--关闭游标
	close my_cursor		
	deallocate my_cursor --卸载游标
end

存储过程实例如下:

drop procedure test_test;
go
create procedure test_test
@parm1 varchar(20),@parm2 varchar(20)
as
begin transaction 
	declare @item_code varchar(32)--定义变量
	declare @item_name varchar(32)
	declare @invest_money_sum float
	--定义游标
	declare my_cursor cursor 
	for
	select item_code,@invest_money_sum
	end
	set @parm2=@parm1+','+@parm2
	print @parm2
	--关闭游标
	close my_cursor		
	deallocate my_cursor --卸载游标
	--可以添加返回值与添加事务控制
	if(@@error>0)
		begin
			rollback tran
			select 0
			return
		end
    else	
		begin
			commit tran
			select 1
			return
		end
--end

exec test_test 'ab','cd'  --执行存储过程
查询sqlserver数据库的所有数据表:

select name from sysobjects where xtype='U'----查询数据库的所有数据表

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读