Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 这里的存储过程定义有什么问题?_Sql_Sql Server_Tsql_Stored Procedures_Database Design - Fatal编程技术网

Sql 这里的存储过程定义有什么问题?

Sql 这里的存储过程定义有什么问题?,sql,sql-server,tsql,stored-procedures,database-design,Sql,Sql Server,Tsql,Stored Procedures,Database Design,我有20个错误,但我将列出3个,也许您可以帮助我了解我在试图根据文档编写存储过程时的总体错误 完整代码: CREATE DATABASE JsPracticeDb; /* Create tables corresponding to the problems, solutions to problems, and ratings of problems or solutions */ GO USE[JsPracticeDb] Go /* Table representing Jav

我有20个错误,但我将列出3个,也许您可以帮助我了解我在试图根据文档编写存储过程时的总体错误

完整代码:

CREATE DATABASE JsPracticeDb; 
/* Create tables corresponding to the problems, solutions to 
   problems, and ratings of problems or solutions */
GO

USE[JsPracticeDb]
Go

/* Table representing JavaScript problems. The promp_code 
   is the HTML that formats the JS code for the view. */
CREATE TABLE Problems 
( 
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
    prompt_code VARCHAR(5000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP
);

/* Create sprocs for adding and deleting problems */
GO
CREATE PROC AddProblem  
    @prompt_code VARCHAR(5000) 
AS 
    INSERT INTO Problems (@prompt_code)
GO

CREATE PROC DeleteProblem
    @id INT
AS
    DELETE FROM Problems WHERE id=@id
GO  

/* Table representing JavaScript solutions (in formatted HTML),
   associated solvers and code that tests validity of solutions */
CREATE TABLE Solutions 
(
   id INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
   problem_id INT NOT NULL,
   solver VARCHAR(50),
   solution_code VARCHAR(5000),
   test_code VARCHAR(8000),
   FOREIGN KEY (problem_id) REFERENCES Problems(id) ON DELETE CASCADE,
   created DATETIME DEFAULT CURRENT_TIMESTAMP
);

/* Create PROCEDURE for adding and deleting solutions */
GO
CREATE PROC AddSolution
    @problem_id INT,
    @solver VARCHAR(50),
    @solution_code VARCHAR(5000),
    @test_code VARCHAR(8000)
AS  
    INSERT INTO Solutions (@problem_id, @solver, @solution_code, @test_code)
GO

CREATE PROC DeleteSolution 
    @id INT 
AS
    DELETE FROM Solutions WHERE id=@id

/* Table representing 0-5 star rating of associated solutions */
CREATE TABLE Ratings 
(
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    stars TINYINT NOT NULL,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);
/* Create sproc for adding ratings */
GO

CREATE PROCEDURE AddRating
    @solution_id INT,
    @stars TINYINT 
AS
    INSERT Into Ratings (@solution_id, @stars)
GO

/* Table representing comments on solutions or comments on coments, and
   the associated commenter. The association of comments on comments is 
   mapped in the next table, CommentPaths   */
CREATE TABLE Comments 
(
    id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    solution_id INT NOT NULL,
    commenter VARCHAR(50),
    cmnt VARCHAR(2000) NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (solution_id) REFERENCES Solutions(id) ON DELETE CASCADE
);

/* Create sprocs for adding and deleting comments 
CREATE PROCEDURE AddComment
    @solution_id INT NOT NULL,
    @commenter VARCHAR(50),
    @cmnt VARCHAR(2000) NOT NULL,
    @parent_id \
AS 
    INSERT INTO Comments (@solution_id, @commenter, @cmnt)
   Still implementing   
*/

CREATE PROCEDURE DeleteComment 
    @id
AS 
    DELETE FROM Comments WHERE id=@id
GO

/* Closure Table for comment tree, e.g.

                         Comments
    ==================================================
    id | solution_id | commenter | comment | created
    --------------------------------------------------
    1  |    1        | ......... | .....   | ......
    2  |    1        | ......... | .....   | ......
    3  |    1        | ......... | .....   | ......
    4  |    1        | ......... | .....   | ......    
    5  |    2        | ......... | .....   | ......    
    6  |    2        | ......... | .....   | ...... 


                      CommentPaths 
                ========================
                 ancestor | descendant
                ------------------------
                    1     |     2
                    1     |     3 
                    1     |     4
                    2     |     4
                    5     |     6

corresponds to the Comments ids being related
to each other like

                1       5
               / \      |
              2   3     6
             /
            4

 */
CREATE TABLE CommentPaths 
(
    ancestor_id INT NOT NULL,
    descendant_id INT NOT NULL,
    PRIMARY KEY (ancestor_id, descendant_id),
    FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON CASCADE DELETE,
    FOREIGN KEY (descendant_id) REFERENCES Comments(id)
);

/* sproc called on delete of a comment to delete descendant comments
   The references to the descendant comments in CommentPaths */
GO
CREATE PROC DeleteCommentDescendens  
    @AncestorId INT 
AS 
    /* http://stackoverflow.com/questions/506602/best-way-to-work-with-transactions-in-ms-sql-server-management-studio */
    BEGIN TRY
        SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId AS descs
        DELETE FROM Comments WHERE id IN descs
        DELETE FROM CommentPaths WHERE ancestor_id IN descs OR descendant_id IN descs
    END TRY
    BEGIN CATCH
        SELECT 
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
    END CATCH;

    IF @@TRANCOUNT > 0
        COMMIT TRANSACTION;
前3个错误是

Msg 102,15级,状态1,程序添加问题,第20行
“')附近的语法不正确

Msg 102,15级,状态1,程序AddSolution,第46行
“')附近的语法不正确

Msg 102,15级,状态1,程序添加,第66行
“')附近的语法不正确

指的是线路

INSERT INTO Problems (@prompt_code)


分别

您有许多语法错误:

添加问题中

INSERT INTO Problems (@prompt_code)
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

AddSolution

INSERT INTO Solutions (@problem_id, @solver, @solution_code, @test_code)
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

AddRating

INSERT INTO Ratings (@solution_id, @stars)
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

deletecoment

CREATE PROCEDURE DeleteComment
    @id
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

commentpath
表格创建中:

FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON CASCADE DELETE
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

deleteCompondens中

SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId AS descs
DELETE FROM Comments WHERE id IN descs
DELETE FROM CommentPaths WHERE ancestor_id IN descs OR descendant_id IN descs
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

您有许多语法错误:

添加问题中

INSERT INTO Problems (@prompt_code)
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

AddSolution

INSERT INTO Solutions (@problem_id, @solver, @solution_code, @test_code)
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

AddRating

INSERT INTO Ratings (@solution_id, @stars)
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

deletecoment

CREATE PROCEDURE DeleteComment
    @id
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

commentpath
表格创建中:

FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON CASCADE DELETE
应该是

INSERT INTO Ratings (solution_id, stars) VALUES (@solution_id, @stars)
CREATE PROCEDURE DeleteComment
    @id INT
FOREIGN KEY (ancestor_id) REFERENCES Comments(id) ON DELETE CASCADE

deleteCompondens中

SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId AS descs
DELETE FROM Comments WHERE id IN descs
DELETE FROM CommentPaths WHERE ancestor_id IN descs OR descendant_id IN descs
应该是:

INSERT INTO Problems (prompt_code) VALUES(@prompt_code)
INSERT INTO Soulutions (problem_id, solver, solution_code, test_code) VALUES (@problem_id, @solver, @solution_code, @test_code)
DECLARE @descs AS TABLE(descendant_id INT)

INSERT INTO @descs(descendant_id)
    SELECT descendant_id FROM CommentPaths WHERE ancestor_id=@AncestorId

DELETE FROM Comments WHERE id IN (SELECT descendant_id FROM @descs)
DELETE FROM CommentPaths WHERE ancestor_id IN (SELECT descendant_id FROM @descs) OR descendant_id IN (SELECT descendant_id FROM @descs)

您是T-Sql编程新手吗? 如果是这样的话,我建议你阅读文档或学习材料来了解语法

在您的案例中,问题是您需要提及列的列表,或者匹配应该为其插入值的列。 此外,如果向表中添加一个新列,您会遇到问题,那么您会怎么做? 它可能对您有用,但是如果其他人需要维护和增强代码,那么对他/她来说就没有任何意义了

因此,我建议您明确提及除了标识列之外的列名以及您拥有的默认值 e、 g:


您是T-Sql编程新手吗? 如果是这样的话,我建议你阅读文档或学习材料来了解语法

在您的案例中,问题是您需要提及列的列表,或者匹配应该为其插入值的列。 此外,如果向表中添加一个新列,您会遇到问题,那么您会怎么做? 它可能对您有用,但是如果其他人需要维护和增强代码,那么对他/她来说就没有任何意义了

因此,我建议您明确提及除了标识列之外的列名以及您拥有的默认值 e、 g:


谢谢现在我得到
msg111,级别15,状态1,过程DeleteComment,第100行“CREATE/ALTER Procedure”必须是查询批处理中的第一条语句。Msg 1767,第16级,状态0,第139行外键“FK__CommentPa__Encess__108B795B”引用了无效的表“Comments”。Msg 1750,级别16,状态0,第139行无法创建约束。查看以前的错误。
知道如何修复吗?请尝试在过程声明之前和之后添加
GO
。至于另一个错误,我将把它留给您作为练习谢谢现在我得到
msg111,级别15,状态1,过程DeleteComment,第100行“CREATE/ALTER Procedure”必须是查询批处理中的第一条语句。Msg 1767,第16级,状态0,第139行外键“FK__CommentPa__Encess__108B795B”引用了无效的表“Comments”。Msg 1750,级别16,状态0,第139行无法创建约束。查看以前的错误。
知道如何修复吗?请尝试在过程声明之前和之后添加
GO
。至于另一个错误,我将把它留给您作为练习