为了账号安全,请及时绑定邮箱和手机立即绑定

SQL Output子句是否可能返回未插入的列?

SQL Output子句是否可能返回未插入的列?

天涯尽头无女友 2019-11-08 10:21:03
我已经对数据库进行了一些修改,并且需要将旧数据迁移到新表中。为此,我需要填充一个表(ReportOptions),该表从原始表(Practice)中获取数据,并填充第二个中间表(PracticeReportOption)。ReportOption (ReportOptionId int PK, field1, field2...)Practice (PracticeId int PK, field1, field2...)PracticeReportOption (PracticeReportOptionId int PK, PracticeId int FK, ReportOptionId int FK, field1, field2...)我进行了查询,以获取从“练习”移至“报告选项”所需的所有数据,但是我无法填写中间表--Auxiliary tablesDECLARE @ReportOption TABLE (PracticeId int /*This field is not on the actual ReportOption table*/, field1, field2...)DECLARE @PracticeReportOption TABLE (PracticeId int, ReportOptionId int, field1, field2)--First I get all the data I need to moveINSERT INTO @ReportOptionSELECT P.practiceId, field1, field2...  FROM Practice P--I insert it into the new table, but somehow I need to have the repation PracticeId / ReportOptionIdINSERT INTO ReportOption (field1, field2...)OUTPUT @ReportOption.PracticeId, --> this is the field I don't know how to get       inserted.ReportOptionId  INTO @PracticeReportOption (PracticeId, ReportOptionId)SELECT field1, field2  FROM @ReportOption--This would insert the relationship, If I knew how to get it!INSERT INTO @PracticeReportOption (PracticeId, ReportOptionId)SELECT PracticeId, ReportOptionId  FROM @ReportOption如果我可以在OUTPUT子句上引用不在目标表上的字段,那将很棒(我认为我不能,但是我不确定)。关于如何满足我的需求的任何想法?
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 441 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信