SqlServer 游标生成主外键关联表insert语句

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值