AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 187091
Accepted
Darrell
Darrell
Asked: 2017-09-28 11:59:44 +0800 CST2017-09-28 11:59:44 +0800 CST 2017-09-28 11:59:44 +0800 CST

如何根据旧记录添加多个新记录

  • 772

好的,所有聪明的人,帮我解决这个问题。

我有一组我们正在进行的调查的相关表格:

dbo.Surveys
------------
| SurveyID |
------------
| 1        |
------------

dbo.Questions
-------------------------
| SurveyID | QuestionID |
-------------------------
| 1        | 1          |
-------------------------
| 1        | 2          |
-------------------------
| 1        | 3          |
-------------------------

dbo.Offerings
--------------------------------------
| SurveyID | QuestionID | OfferingID |
--------------------------------------
| 1        | 1          | 1          |
--------------------------------------
| 1        | 3          | 2          |
--------------------------------------

事实:

dbo.Surveys.SurveyID 是 IDENTITY

dbo.Questions.QuestionID 是 IDENTITY

dbo.Offerings.OfferingID 是 IDENTITY

我需要根据现有调查的副本创建一个新调查。

我得到一份 SurveyID 1 的副本并将其插入 dbo.Surveys,然后获得新的 SurveyID。

我得到一份与 SurveyID 1 相关的所有问题的副本,并将它们插入 dbo.Questions 并将 SurveyID 更改为新的。

这当然会为每条记录生成一个新的 QuestionID。

我获得了与 SurveyID 1 相关的所有产品的副本,并将它们插入 dbo.Offerings 并将 SurveyID 更改为新的。

现在的诀窍是,我如何知道哪个新的 OfferingID 与哪个新的 QuestionID 对应,以便我可以更新 dbo.Offerings 表中的 QuestionID?

所以,为了充实这一点,这就是我最终得到的:

dbo.Surveys
------------
| SurveyID |
------------
| 1        |
------------
| 2        |
------------

dbo.Questions
-------------------------
| SurveyID | QuestionID |
-------------------------
| 1        | 1          |
-------------------------
| 1        | 2          |
-------------------------
| 1        | 3          |
-------------------------
| 2        | 4          |
-------------------------
| 2        | 5          |
-------------------------
| 2        | 6          |
-------------------------

dbo.Offerings
--------------------------------------
| SurveyID | QuestionID | OfferingID |
--------------------------------------
| 1        | 1          | 1          |
--------------------------------------
| 1        | 3          | 2          |
--------------------------------------
| 2        | 1          | 3          | (QuestionID needs to be 4)
--------------------------------------
| 2        | 3          | 4          | (QuestionID needs to be 6)
--------------------------------------

编辑:好的。为了澄清起见:

CREATE TABLE Survey (SurveyID int identity, sname varchar(30));
CREATE TABLE Questions (QuestionID int IDENTITY, SurveyID int, qtext varchar(30), qtype varchar(20));
CREATE TABLE Offerings (OfferingID int IDENTITY, QuestionID int, SurveyID int, ovalues varchar(30));

INSERT INTO Survey VALUES ('New Survey');
INSERT INTO Questions VALUES (1,'Enter text','SingleAnswer');
INSERT INTO Questions VALUES (1,'Pick one','MultipleChoice');
INSERT INTO Offerings VALUES (2,1,'Choice 1');
INSERT INTO Offerings VALUES (2,1,'Choice 2');
INSERT INTO Offerings VALUES (2,1,'Choice 3');

SELECT * FROM Survey;
SELECT * FROM Questions;
SELECT * FROM Offerings;

    SurveyID | sname
    ---------------------
           1 | New Survey

    QuestionID | SurveyID | qtext      | qtype
    ---------------------------------------------------
             1 |        1 | Enter text | SingleAnswer
             2 |        1 | Pick one   | MultipleChoice

    OfferingID | QuestionID | SurveyID | ovalues
    ---------------------------------------------
             1 |          2 |        1 | Choice 1
             2 |          2 |        1 | Choice 2
             3 |          2 |        1 | Choice 3

所以,现在我需要一份 SurveyID 1 的副本,这样:

    SurveyID | sname
    -----------------------
           1 | New Survey
           2 | New Survey 2

    QuestionID | SurveyID | qtext      | qtype
    ---------------------------------------------------
             1 |        1 | Enter text | SingleAnswer
             2 |        1 | Pick one   | MultipleChoice
             3 |        2 | Enter text | SingleAnswer
             4 |        2 | Pick one   | MultipleChoice

    OfferingID | QuestionID | SurveyID | ovalues
    ---------------------------------------------
             1 |          2 |        1 | Choice 1
             2 |          2 |        1 | Choice 2
             3 |          2 |        1 | Choice 3
             4 |          4 |        2 | Choice 1
             5 |          4 |        2 | Choice 2
             6 |          4 |        2 | Choice 3
t-sql
  • 2 2 个回答
  • 70 Views

2 个回答

  • Voted
  1. markp-fuso
    2017-09-28T16:35:17+08:002017-09-28T16:35:17+08:00

    对原始问题的回应


    暂时搁置我们 a) 不知道get a copy of ...需要什么和 b) 没有相关表的 DDL(例如,PK 定义)……我们将不得不稍微考虑一下……

    我们将从一些示例表和数据开始:

    drop table if exists Surveys
    drop table if exists Questions
    drop table if exists Offerings;
    
    create table Surveys
    (SurveyID    int           identity
    ,sname       varchar(30)
    );
    
    create table Questions
    (SurveyID    int
    ,QuestionID  int           identity
    ,qtext       varchar(max)
    );
    
    create table Offerings
    (SurveyID    int
    ,QuestionID  int
    ,OfferingID  int            identity
    ,otext       varchar(max)
    );
    
    -- populate the tables
    
    declare @new_sid int
    
    insert into Surveys (sname) select 'Survey ABC'
    
    select @new_sid = @@identity
    
    insert into Questions (SurveyID, qtext) values 
    (@new_sid, 'Quest_#1'), 
    (@new_sid, 'Quest_#2'), 
    (@new_sid, 'Quest_#3')
    
    -- just pick 2 questions to work with; min()/max() will suffice
    
    insert into Offerings (SurveyID, QuestionID, otext) 
    select @new_sid,min(QuestionID),'Off_#1' from Questions where SurveyID = @new_sid
    union all
    select @new_sid,max(QuestionID),'Off_#2' from Questions where SurveyID = @new_sid;
    
    -- review our new data
    
    select * from Surveys   order by SurveyID
    select * from Questions order by SurveyID, QuestionID
    select * from Offerings order by SurveyID, QuestionID, OfferingID;
    
     SurveyID | sname     
     -------- | ----------
            1 | Survey ABC
    
     SurveyID | QuestionID | qtext    
     -------- | ---------- | ---------
            1 |          1 | Quest_#1 
            1 |          2 | Quest_#2 
            1 |          3 | Quest_#3
    
     SurveyID | QuestionID | OfferingID | otext 
     -------- | ---------- | ---------- | ------
            1 |          1 |          1 | Off_#1
            1 |          3 |          2 | Off_#2
    

    为了这个例子,我们将断言这copy意味着复制相同数量的行(调查、问题、产品)并将我们所有的文本值附加“(version_#2)”:

    declare @old_sid int,
            @new_sid int
    
    -- find our old SurveyID
    
    select @old_sid = SurveyID from Surveys where sname = 'Survey ABC'
    
    -- create our new Survey, appending '(version_#2)' to stext
    
    insert into Surveys (sname) 
    select sname + ' (version_#2)' from Surveys where SurveyID = @old_sid
    
    -- grab the new SurveyID
    
    select @new_sid = @@identity
    
    -- copy our old Survey's Questions, replacing the old SurveyID with the new SurveyID,
    -- and append ' (version_#2)' to the qtext field
    
    insert into Questions (SurveyID, qtext) 
    select @new_sid, 
           q.qtext + ' (version_#2)' 
    from   Questions q
    where  q.SurveyID = @old_sid;
    
    -- use a couple CTEs to pull our old Questions/Offerings and new Questions;
    -- without any input on how to map the old and new data sets we'll generate
    -- a row_number() column with each data set, with the objective being to 
    -- join the old/new data sets by matching row numbers
    
    with
    oldtab as
    (select  row_number() over(order by q.QuestionID) as rnum, 
             o.otext
     from    Questions q 
     left 
     join    Offerings o
     on      q.SurveyID   = o.SurveyID
     and     q.QuestionID = o.QuestionID
     where   q.SurveyID   = @old_sid),
    
    newtab as
    (select  row_number() over(order by q.QuestionID) as rnum, 
             q.SurveyID, 
             q.QuestionID
     from    Questions q 
     where   q.SurveyID   = @new_sid)
    
    -- insert our new SurveyID/QuestionID pairs, and the old otext values
    
    insert into Offerings (SurveyID, QuestionID, otext) 
    
    select   n.SurveyID,
             n.QuestionID,
             o.otext + ' (version_#2)'
    from     oldtab o
    join     newtab n
    on       o.rnum = n.rnum       -- join by row_number()
    where    o.otext is NOT NULL   -- skip (old) Questions that didn't have a matching row in Offerings
    order by o.rnum
    

    结果:

    select * from Surveys   order by SurveyID
    select * from Questions order by SurveyID, QuestionID
    select * from Offerings order by SurveyID, QuestionID, OfferingID
    
     SurveyID | sname                 
     -------- | ----------------------
            1 | Survey ABC            
            2 | Survey ABC (version_#2)
    
     SurveyID | QuestionID | qtext               
     -------- | ---------- | --------------------
            1 |          1 | Quest_#1            
            1 |          2 | Quest_#2            
            1 |          3 | Quest_#3            
            2 |          4 | Quest_#1 (version_#2)
            2 |          5 | Quest_#2 (version_#2)
            2 |          6 | Quest_#3 (version_#2)
    
     SurveyID | QuestionID | OfferingID | otext              
     -------- | ---------- | ---------- | -------------------
            1 |          1 |          1 | Off_#1             
            1 |          3 |          2 | Off_#2             
            2 |          4 |          3 | Off_#1 (version_#2)
            2 |          6 |          4 | Off_#2 (version_#2)
    

    这是上面的dbfiddle。

    • 2
  2. Best Answer
    markp-fuso
    2017-09-29T13:26:33+08:002017-09-29T13:26:33+08:00

    对更新/编辑问题的回应(添加 qtext/qtype/ovalues 列)


    使用问题编辑部分中的相同命令create table和insert命令,我们将重新使用我原始答案中的一些代码......

    declare @old_sid int,
            @new_sid int
    
    select @old_sid = SurveyID from Survey where sname = 'New Survey'
    
    -- create new Survey record
    
    insert into Survey (sname) 
    select sname + ' 2' from Survey where SurveyID = @old_sid
    
    select @new_sid = @@identity
    
    -- create new Questions records
    
    insert into Questions (SurveyID, qtext, qtype) 
    select @new_sid, 
           q.qtext,
           q.qtype
    from   Questions q
    where  q.SurveyID = @old_sid;
    
    -- create new Offerings records
    
    with
    qmap as
    (-- build a mapping between old and new QuestionID/SurveyID pairs
     select o.QuestionID as oldQID,
            o.SurveyID   as oldSID,
            n.QuestionID as newQID,
            n.SurveyID   as newSID,
            n.qtext,
            n.qtype
    
    from    Questions o
    join    Questions n
    
    on      o.qtext = n.qtext
    and     o.qtype = n.qtype
    and     o.SurveyID = @old_sid
    and     n.SurveyID = @new_sid)
    
    insert into Offerings (QuestionID, SurveyID, ovalues)
    
    select  m.newQID,
            m.newSID,
            o.ovalues
    
    from    Offerings o
    join    qmap m
    
    on      o.QuestionID = m.oldQID
    and     o.SurveyID   = m.oldSID
    and     o.SurveyID   = @old_sid;
    

    注意:可能会将此qmap解决方案纳入我对原始问题的回答,但现在选择保留原始答案。

    结果:

    select * from Survey    order by SurveyID
    select * from Questions order by SurveyID, QuestionID
    select * from Offerings order by SurveyID, QuestionID, OfferingID
    
     SurveyID | sname       
     -------- | ------------
            1 | New Survey  
            2 | New Survey 2
    
     QuestionID | SurveyID | qtext      | qtype         
     ---------- | -------- | ---------- | --------------
              1 |        1 | Enter text | SingleAnswer  
              2 |        1 | Pick one   | MultipleChoice
              3 |        2 | Enter text | SingleAnswer  
              4 |        2 | Pick one   | MultipleChoice
    
     OfferingID | QuestionID | SurveyID | ovalues 
     ---------- | ---------- | -------- | --------
              1 |          2 |        1 | Choice 1
              2 |          2 |        1 | Choice 2
              3 |          2 |        1 | Choice 3
              4 |          4 |        2 | Choice 1
              5 |          4 |        2 | Choice 2
              6 |          4 |        2 | Choice 3
    

    这是上面的dbfiddle。

    • 0

相关问题

  • 如何使用 TSQL 更改 SQL 服务器配置管理器设置?

  • 如何从结果集中获取列名和类型的列表?

  • MS SQL:使用计算值计算其他值

  • 如何判断 SQL Server 数据库是否仍在使用?

  • 实施 PIVOT 查询

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve