我怎样才能在T-SQL中这样做?

时间:2016-03-09 18:58:42

标签: sql sql-server tsql stored-procedures database-design

所以我有1张桌子

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

和另一张表

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

其视觉表现形式如

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

我需要的是一个过程,它既向Versions表插入一个新行,又将Questions表的所有当前行添加到适当增加的id和{{1} s等于刚刚创建的版本的s。

示例:

如果version_id来自

Versions

然后id | title ----------- 1 | "V1" 2 | "V2" 转到

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

这接近正确吗?需要改变什么?有没有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询

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