带参数和游标的存储过程

Posted ZhaoMengmeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带参数和游标的存储过程相关的知识,希望对你有一定的参考价值。

 1 USE [ChinaMDM]
 2 GO
 3 /****** Object:  StoredProcedure [dbo].[MDMTools_SequenceContainsMoreThanOneEement_Dispose_Material]    Script Date: 2016/6/28 10:01:42 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO
 8 ALTER PROCEDURE [dbo].[MDMTools_SequenceContainsMoreThanOneEement_Dispose_Material]
 9  @MaterialCode Nvarchar(50)
10 as
11 BEGIN 
12     declare @Materialid int;
13     declare @Unitid int;
14     declare @Message Nvarchar(max);
15     set xact_abort on
16 
17     begin tran 
18     set @Message=‘‘
19     declare _cur cursor for
20     select  id  from Materialheaders where code=@MaterialCode and isactive=1
21     open _cur
22     fetch next from  _cur into @Materialid
23     while @@fetch_status=0
24         begin 
25             begin try
26                 select @Unitid=id from MaterialMeasuringUnits where  AlternativeUoMId = ( select AlternativeUoMId  from MaterialMeasuringUnits where materialheaderid=@Materialid group by AlternativeUoMId  having count(AlternativeUoMId)>1  )  and  materialheaderid=@Materialid  and  (IsEAN  =0 or IsEAN is null)
27                 
28                 update MaterialMeasuringUnits set AlternativeUoMId=1 where id=@Unitid
29                 
30                 if (@Unitid is null)
31                     begin
32                         select @Message=N@Unitid为空
33                     end
34                 else
35                     begin
36                         select @Message=N执行成功:物料代码: +@MaterialCode+N 物料测量单位表ID: +convert (nvarchar,@Unitid)
37                     end
38 
39                 print @Message
40             end try 
41             begin catch
42                 select @Message= ERROR_MESSAGE() 
43                 print @Message
44                 rollback tran
45                 close _cur 
46                 deallocate _cur
47                 return
48             end catch
49 
50             fetch next from  _cur into @Materialid
51         end
52 
53             close _cur 
54            deallocate _cur
55 
56     commit tran
57 END

 

以上是关于带参数和游标的存储过程的主要内容,如果未能解决你的问题,请参考以下文章

带游标的Mysql存储过程

Oracle存储过程游标for循环怎么写

oracle存储过程中循环for in是如何使用的

spring-data-jpa 1.11.16 带游标的存储过程

数据库基础详解:存储过程、视图、游标、SQL语句优化以及索引

oracle存储过程输入参数能否为空