1

您好我正在尝试创建一个存储过程,但每次我尝试保存它时都会出现错误

Msg 102, Level 15, State 1, Procedure forum_Insert_Post, Line 3
Incorrect syntax near '@description'.
Msg 137, Level 15, State 2, Procedure forum_Insert_Post, Line 9
Must declare the scalar variable "@title"

这是我的代码:

CREATE PROCEDURE forum_Insert_Post
     @title nvarchar
     @description nvarchar
     @subcategoryId int
     @date dateTime
     @usernameId uniqueidentifier
AS
    INSERT INTO forum_posts (PostTitle , PostContent , PostDate , UserId , SubcategoryId)
    VALUES (@title , @description , @date , @usernameId , @subcategoryId)

我在这里做错了什么?

4

2 回答 2

4

参数后需要逗号:

CREATE PROCEDURE forum_Insert_Post
 @title nvarchar,
 @description nvarchar,
 @subcategoryId int,
 @date dateTime,
 @usernameId uniqueidentifier
AS
INSERT INTO forum_posts (PostTitle , PostContent , 
 PostDate , UserId , SubcategoryId)
VALUES (@title , @description , @date , @usernameId , @subcategoryId)
于 2013-01-03T15:22:50.237 回答
4

只需添加一些逗号,并给nvarchars 一个适当的大小,否则它将截断为 1 个字符 :)

CREATE PROCEDURE forum_Insert_Post
     @title nvarchar(20),
     @description nvarchar(20),
     @subcategoryId int,
     @date dateTime,
     @usernameId uniqueidentifier
AS
    INSERT INTO forum_posts (PostTitle , PostContent , PostDate , UserId , SubcategoryId)
    VALUES (@title , @description , @date , @usernameId , @subcategoryId)

此处NVARCHAR截断的 SQLFiddle

于 2013-01-03T15:23:37.990 回答