简体   繁体   中英

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.

Example:

If Versions goes from

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

then Questions goes to

--                                            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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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