插入存储过程传递参数

时间:2012-09-28 14:25:53

标签: sql database stored-procedures

我有这个SQL语句

INSERT housingSurvey ([nameID], [houseID], [jobID], [Active], [comment], [needsMaintenance], [lastUpdateDate) 
VALUES (@NAMEID, @HOUSEID, @JOBID, 1, @DUEDATE, @COMMENT, NULL, @LASTUPDATEDATE)

我尝试了这个存储过程,但是我没有得到当前的日期。

CREATE PROCEDURE housingSurveyS(
                    @NAMEID INT,
                    @HOUSEID INT,
                    @JOBID INT, 
                    @COMMENT BIT,
                    @DUEDATE NVARCHAR,
                    CURRENTTIME)
AS
BEGIN
INSERT INTO housingSurvey(
    [nameID],
    [houseID],
    [jobID],
    [Active],
    [comment],
    [dueDate],
    [needsMaintenance],
    [lastupdatedate])
VALUES (
    @NAMEID,
    @HOUSEID,
    @JOBID,
    1,
    @COMMENT,
    @DUEDATE,
    NULL,
    @LASTUPDATEDATE)
END

请你看看我做错了什么。感谢

更新 我将CURRENTTIME更改为DATETIME作为参数,将当前日期插入[lastupdatedate] ...它仍然不起作用...

1 个答案:

答案 0 :(得分:5)

VALUES (
    ...
    @DUEDATE,
    @COMMENT 

INSERT语句字段列表中定义的字段顺序相关时,顺序错误,切换它们。

修改

CREATE PROCEDURE housingSurveyS(
    @NAMEID INT,
    @HOUSEID INT,
    @JOBID INT, 
    @COMMENT BIT,
    @DUEDATE NVARCHAR(32) --add a size, should this be DATETIME
)
AS
BEGIN
INSERT INTO housingSurvey(
    [nameID],
    [houseID],
    [jobID],
    [Active],
    [comment],
    [dueDate],
    [needsMaintenance],
    [lastupdatedate]
) VALUES (
    @NAMEID,
    @HOUSEID,
    @JOBID,
    1,
    @COMMENT,
    @DUEDATE,
    NULL,
    GETDATE()
)
END