最近写的一个存储过程
declare @tid varchar(50)
declare @did varchar(50)
declare @sellorder varchar(20)
declare @Type varchar(20)
declare @SourceCode varchar(20)
declare @speccode varchar(50)
declare @spcode varchar(50)
declare @SPNum int
declare @str nvarchar(200)
declare @Count int
declare @i int
declare @spguid nvarchar(50)
declare @spSkuguid nvarchar(50)
declare @paipaickguid nvarchar(50)
declare @Account nvarchar(50)
declare @AccountGuid varchar(50)
declare @Guid varchar(50)
declare mycur cursor dynamic for select tid from BPF_ODS.dbo.T_GYSyncStore where Synchstatus <>1
begin
open mycur
FETCH NEXT FROM mycur into @tid
WHILE @@FETCH_STATUS = 0
begin
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..##TMP'))
truncate table ##Tmp
else
create table ##TMP
(
spcode varchar(50),
speccode varchar(50),
Num int
)
insert into ##TMP select spcode,speccode ,num from BPF_3_0_0.dbo.T_sales_ddspmx where tid=@tid and status>0
Set @Account='库存同步账号'
Set @AccountGuid='40F552AB-59A4-4994-ACE2-2D8E71244483'
Set @paipaickguid='c0c82d9f-b9e4-4248-851f-7b7842ca5dc1'
Set @Guid= newID()
Set @i = 0
Select @Count = Count(Speccode) from ##TMP
While @i < @Count
Begin
Set @str ='Select top 1 @speccode=speccode, @SPNum=num ,@spcode=spcode from ##TMP Where speccode not in(Select top ' + Str(@i) + ' speccode from ##TMP)'
Exec Sp_ExecuteSql @str,N'@speccode Varchar(50) OutPut,@SPNum int OutPut,@spcode varchar(50) OutPut',@speccode Output,@SPNum OutPut,@spcode OutPut
select @spSkuguid=GUID from [192.168.15.223].[ec-erp20].dbo.SPSKU where SKUDM=@speccode
select @spguid=GUID from [192.168.15.223].[ec-erp20].dbo.SHANGPIN where SPDM = @spcode
-- print @speccode+'='+@spSkuguid
--print @spcode +'='+@spguid
--print @SPNum
Exec [192.168.15.223].[ec-erp20].[dbo].[p_TraceStock] @GUID=@Guid,@YG_GUID=@AccountGuid,@YGMC=@Account,@DJLX='销售订单',@CZMC='发货',
@DJBH=@tid,@CK_GUID=@paipaickguid,@SP_GUID=@spguid,@SKU_GUID=@spSkuguid,@TZS=@SPNum,@TZLX=0,@KYTZS=NULL,@ZTTZS=NULL
set @i=@i+1
End
fetch next from mycur into @tid
end
close mycur
deallocate mycur
end
GO