记录我第一次写sql server 2005的存储过程

使用了递归和临时表,传递了参数,发上来,权当纪念。

USE [UFDATA_005_2014]
GO
/****** 对象:  StoredProcedure [dbo].[PROC_PRODUCTTRACING]    脚本日期: 07/05/2017 08:37:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[PROC_PRODUCTTRACING]
-- Add the parameters for the stored procedure here


@autoid varchar(100)

AS




BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @cPB nvarchar(60)
declare @cMB nvarchar(60)
declare @cSwCode nvarchar(30)
declare @cInvCode nvarchar(30)
declare @count int
declare @cNumber nvarchar(200)
declare @cSubNumber nvarchar(30)

    -- Insert statements for procedure here



select @cInvCode=cInvCode from v_erp_CompletionOrder_sun where autoid=@autoid
if @cInvCode like 'DX%' OR @cInvCode like 'AX%' OR @cInvCode like 'NAX%' OR @cInvCode like 'CTDX%' 
begin
print'galvanization or colordraw.....'
select @count=Count(*), @cMB=cMBatch from V_app_GalvanizationCompletion_all where autoid =@autoid GROUP BY cMBatch
print @count
if (@count>0)
begin
print'into galvanization...'
select  @cSubNumber=cSubNumber,@cSwCode=cSwCode  from v_erp_CompletionOrder_sun 
where autoid=@autoid
select * into #temp_q from (select * from v_erp_CompletionOrder_sun a where @autoid=autoid) T
print @cSubNumber
select @cNumber=Value from [UFDATA_005_2014].[dbo].[BG_SplitTextToStringArray](@cSubNumber,'-') where Idx='0'
print @cNumber
select @cMB=cMBatch from V_app_GalvanizationFeeding_all where cNumber=@cNumber and cSwCode=@cSwCode
print @cMB   print @cSwCode;
while @count>0
begin
insert into #temp_q select * from v_erp_CompletionOrder_sun where cPBatch=@cMB
select @cMB=cMBatch from v_erp_CompletionOrder_sun where cPBatch=@cMB GROUP BY cMBatch
select @count=Count(*)  from v_erp_CompletionOrder_sun where cPBatch=@cMB

end
end
ELSE
begin
print'into colordraw......'
select  @cSubNumber=cSubNumber,@cSwCode=cSwCode  from v_erp_CompletionOrder_sun where autoid=@autoid 
print @cSubNumber;
select @cNumber=Value from [UFDATA_005_2014].[dbo].[BG_SplitTextToStringArray](@cSubNumber,'-') where Idx='0'
print @cNumber
select @cMB=cMBatch from V_app_ColorDrawFeeding_all where cNumber=@cNumber and cSwCode=@cSwCode
print @cMB;
while @count>0
begin
insert into #temp_q select * from v_erp_CompletionOrder_sun where cPBatch=@cMB
select @cMB=cMBatch from v_erp_CompletionOrder_sun where cPBatch=@cMB GROUP BY cMBatch
select @count=Count(*)  from v_erp_CompletionOrder_sun where cPBatch=@cMB

end
end
end
ELSE
BEGIN 

with TEST_ETC 
AS (
SELECT v_erp_CompletionOrder_sun.*  from v_erp_CompletionOrder_sun where autoid=@autoid
UNION ALL
SELECT v_erp_CompletionOrder_sun.* from v_erp_CompletionOrder_sun ,TEST_ETC where v_erp_CompletionOrder_sun. cPBatch=TEST_ETC.cMBatch
)
select * from TEST_ETC;
END
select * from #temp_q
drop table #temp_query;
END


猜你喜欢

转载自blog.csdn.net/wqztmx4/article/details/74502979