简体   繁体   English

我如何/可以在T-SQL中做到这一点?

[英]How/can I do this in T-SQL?

So I have 1 table 所以我有一张桌子

CREATE TABLE Versions (
    id INT IDENTITY(1,1),
    title NVARCHAR(100),
    PRIMARY KEY (id)
)

and another table 和另一张桌子

CREATE TABLE Questions (
    id INT IDENTITY(1,1),
    subsection_id INT NOT NULL,
    qtext NVARCHAR(400) NOT NULL,
    version_id INT NOT NULL,
    viewtype INT NOT NULL DEFAULT 1,
    PRIMARY KEY (id),
    FOREIGN KEY (subsection_id) REFERENCES Subsections(id),
    FOREIGN KEY (version_id) REFERENCES Versions(id)
);

a visual representation of which is like 视觉表示就像

--                                            Questions
-- ============================================================================================================================
--   id  |                 qtext                                              |  subsection_id  |  version_id   |    viewtype
-- =============================================================================================================================
--    1  | 'Does Hillary Clinton look good in orange?'                        |      1          |       1       |        1
--    2  | 'How many prime numbers are there?'                                |      1          |       1       |        1
--    3  | 'What do I suck at writing SQL?'                                   |      1          |       1       |        1
--    4  | 'Would Jon Skeet beat Mark Zuckerberg in a programming contest?'   |      1          |       1       |        1

What I need is a procedure that both inserts a new row to the Versions table and adds to the Questions table all its current rows with appropriately incremented id s and version_id s equal to that of the version that was just created. 我需要的是一个既向Versions表中插入新行, Versions Questions表中添加其所有当前行的过程,并以与刚刚创建的版本相同的idversion_id适当地递增。

Example: 例:

If Versions goes from 如果Versions来自

id | title 
-----------
 1 | "V1" 
 2 | "V2" 

then Questions goes to 然后Questions转到

--                                            Questions
-- ============================================================================================================================
--   id  |                 qtext                                              |  subsection_id  |  version_id   |    viewtype
-- =============================================================================================================================
--    1  | 'Does Hillary Clinton look good in orange?'                        |      1          |       1       |        1
--    2  | 'How many prime numbers are there?'                                |      1          |       1       |        1
--    3  | 'What do I suck at writing SQL?'                                   |      1          |       1       |        1
--    4  | 'Would Jon Skeet beat Mark Zuckerberg in a programming contest?'   |      1          |       1       |        1
--    5  | 'Does Hillary Clinton look good in orange?'                        |      1          |       2       |        1
--    6  | 'How many prime numbers are there?'                                |      1          |       2       |        1
--    7  | 'What do I suck at writing SQL?'                                   |      1          |       2       |        1
--    8  | 'Would Jon Skeet beat Mark Zuckerberg in a programming contest?'   |      1          |       2       |        1

Best attempt with my limited database skills: 我有限的数据库技能的最佳尝试:

CREATE PROCEDURE OntoNewVersion
    @new_title NVARCHAR(100) 
AS  
    INSERT INTO Versions (title) VALUES (@new_title)
    SET @versid = SCOPE_IDENTITY()
    SET @qrows = SELECT COUNT(*) FROM Questions; -- this is wrong, I know
    SET @i = 1;
    WHILE @i <= @qrows
        BEGIN
            SET @thisq = SELECT * FROM Questions WHERE id=@i
            INSERT INTO Questions (qtext,subsection_id,version_id,viewtype) VALUES (@thisq.qtext,@thisq.subsection_id,@versid,@thisq.viewtype);
        END

Is that close to being correct? 这接近正确吗? What needs changed? 什么需要改变? Is there an overall better way to do this? 有整体更好的方法吗?

You may try the below query 您可以尝试以下查询

CREATE PROCEDURE OntoNewVersion
    @new_title NVARCHAR(100) 
AS 
BEGIN 
 BEGIN TRY
  BEGIN TRANSACTION T1
    DECLARE @versid INT
    INSERT INTO Versions (title) VALUES (@new_title)
    SET @versid = SCOPE_IDENTITY()


    INSERT INTO Questions 
        (qtext,subsection_id,version_id,viewtype)  
    SELECT qtext, subsection_id,@versid,viewtype 
    FROM Questions  
  COMMIT TRANSACTION T1
 END TRY

 BEGIN CATCH
  IF(@@TRANCOUNT>0)
    BEGIN 
        ROLLBACK TRANSACTION T1
    END
  ;THROW    
 END CATCH
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM