SqlServer cursor generating primary foreign key table insert statement

declare @RR nvarchar(max)
declare @PCR nvarchar(max)
declare @RequestRecordId bigint
declare my_cursor cursor
for (select a.RequestRecordId from [dbo].[CUP_RequestRecord] a inner join [dbo].[CUP_POSCreditResult] b on b.RequestRecordId=a.RequestRecordId)
open my_cursor
fetch next from my_cursor into @RequestRecordId
while @@fetch_status = 0
    begin
        select @RR='insert into [dbo].[CUP_RequestRecord] select '''+SourceRequest+''',N'''+ApplicationNumber+''',N'''+CONVERT(varchar(100), ApplicationDatetime, 25)+''',N'''+CustomerName+''',N'''+IdentifyType+''',N'''+IdentifyNumber+''',N'''+BankCardNo+''',N'''+MobilePhone+''',N'''+IssueCountry+''',N'''+[Role]+''',N'''+QueryEmployee+''',N'''+QueryReason+''',N'''+ReqDataJson+''',N'''+isnull(ReqSensDataJson,'')+''',N'''+''',N'''+cast(ReqDate as varchar(MAX))+''',N'''+RespCode+''',N'''+RespMsg+''',N'''+''',N'''+''',N'''+case when RespCombiLabelsJson is null then '' else cast(RespCombiLabelsJson as varchar(max)) end+''',N'''+CONVERT(varchar(100), RespDate, 25)+''',N'''+CONVERT(varchar(100), CreateDate, 25)+''',N'''+isnull(EcmStorageStatus,'')+''',N'''+isnull(EcmStorageUrl,'')+'''' from [dbo].[CUP_RequestRecord] where RequestRecordId=@RequestRecordId
        select @PCR='insert into [dbo].[CUP_POSCreditResult] select @@IDENTITY'+','+cast(TotalExpenditureAmt as varchar(MAX))+','+cast(VIPCardNumber as varchar(MAX))+','+cast(HaveHouse as varchar(MAX))+','+cast(HaveCar as varchar(MAX))+','+cast(HaveChild as varchar(MAX))+',N'''+isnull(OneYearExpenditureFirstCity,'')+''',N'''+isnull(OneYearExpenditureSecondCity,'')+''',N'''+isnull(OneYearExpenditureThirdCity,'')+''','+cast(TotallFinancialAmt as varchar(MAX))+','+cast(GinkgoScore as varchar(MAX))+','+cast(AntiFraudScore as varchar(MAX))+','+cast(GoldCardCount as varchar(MAX))+','+cast(TitaniumCardCount as varchar(MAX))+','+cast(PlatinumCardCount as varchar(MAX))+','+cast(DiamondCardCount as varchar(MAX)) from [dbo].[CUP_POSCreditResult] where RequestRecordId=@RequestRecordId
        print @RR
        print @PCR
        set @RR=''
        set @PCR=''
        fetch next from my_cursor into @RequestRecordId
    end
close my_cursor
deallocate my_cursor
go

 

Guess you like

Origin www.cnblogs.com/hofmann/p/12205033.html