SqlServer遊標、儲存過程及資料塊執行
阿新 • • 發佈:2019-02-20
資料塊遊標事例如下:
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,@item_name,@invest_money_sum end --關閉遊標 close my_cursor deallocate my_cursor --解除安裝遊標 end
儲存過程例項如下:
查詢sqlserver資料庫的所有資料表: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,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,@item_name,@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' --執行儲存過程
select name from sysobjects where xtype='U'----查詢資料庫的所有資料表