sql server存儲過程使用實例
作者:佚名
sql server存儲過程在SQL數(shù)據(jù)庫中非常重要,可以使用sql server存儲過程實現(xiàn)一些特有的功能,下面就以一個實例的形式為您介紹其中一種使用方法,供您參考。
使用sql server存儲過程,可以在數(shù)據(jù)庫中實現(xiàn)多種功能,下面就為您介紹其中的一種,供您參考,希望對您學(xué)習(xí)sql server存儲過程的使用有所幫助。
如果需要同時插入N條數(shù)據(jù),不想在程序里控制,但是SQL Sever又不支持數(shù)組參數(shù).所以只能用變通的辦法了.利用SQL Server強大的字符串處理傳把數(shù)組格式化為類似"1,2,3,4,5,6",然后在sql server存儲過程中用SubString配合CharIndex把分割開來。
詳細的sql server存儲過程:
- CREATE PROCEDURE dbo.ProductListUpdateSpecialList
- @ProductId_Array varChar(800),
- @ModuleId int
- AS
- DECLARE @PointerPrev int
- DECLARE @PointerCurr int
- DECLARE @TId int
- Set @PointerPrev=1
- set @PointerCurr=1
- begin transaction
- Set NoCount ON
- delete from ProductListSpecial where ModuleId=@ModuleId
- Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev,@PointerCurr-@PointerPrev) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- SET @PointerPrev = @PointerCurr
- while (@PointerPrev+1 < LEN(@ProductId_Array))
- Begin
- Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
- if(@PointerCurr>0)
- Begin
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,@PointerCurr-@PointerPrev-1) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- SET @PointerPrev = @PointerCurr
- End
- else
- Break
- End
- set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,LEN(@ProductId_Array)-@PointerPrev) as int)
- Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
- Set NoCount OFF
- if @@error=0
- begin
- commit transaction
- end
- else
- begin
- rollback transaction
- end
- GO
【編輯推薦】
sql server還原數(shù)據(jù)庫的方法
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)