-1

我在 SQL Server 中创建了一个存储过程,但出现此错误,但我不确定它的含义。

以下是程序:

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Grant_Append]
    -- Add the parameters for the stored procedure here
    (@GrantNumber AS VARCHAR(50), 
     @GrantName AS VARCHAR(100),
     @ProjectCode AS VARCHAR(50),
     @LOCID AS INT,
     @Comments AS VARCHAR(100),
     @Provider AS VARCHAR(100),
     @ISAT AS NCHAR(10),
     @TypeID AS INT,
     @Slots AS FLOAT,
     @Patients AS FLOAT,
     @FundingMethod AS VARCHAR(50),
     @L1600 AS MONEY,
     @L1602 AS MONEY,
     @L1603 AS MONEY,
     @L1608 AS MONEY,
     @L1612 AS MONEY,
     @ADAAFunding AS MONEY,
     @ProgramBudget AS MONEY,
     @Jurisdiction AS INT,
     @VersionType AS VARCHAR(20),
     @VersionNum AS INT)
AS BEGIN
 INSERT INTO dbo.Grant
    (GrantNumber,
     GrantName,
     ProjectCode,
     LOCID,
     Comments,
     Provider,
     ISAT,
     TypeID,
     Slots,
     Patients,
     FundingMethod,
     [1600],
     [1602],
     [1603],
     [1608],
     [1612],
     ADAAFunding,
     ProgramBudget,
     Jurisdiction,
     VersionType,
     VersionNum, 
     SubmitDate)
      -- Insert statements for procedure here
    SELECT @GrantNumber, 
            @GrantName,
            @ProjectCode,
            @LOCID,
            @Comments,
            @Provider,
            @ISAT,
            @TypeID,
            @Slots,
            @Patients,
            @FundingMethod,
            @1600,
            @1602,
            @1603,
            @1608,
            @1612,
            @ADAAFunding,
            @ProgramBudget,
            @Jurisdiction,
            @VersionType,
            @VersionNum,
            getdate()
END

这些是错误:

消息 156,级别 15,状态 1,过程 Grant_Append,第 30 行
关键字“Grant”附近的语法不正确。
消息 137,级别 15,状态 2,过程 Grant_Append,第 65 行
必须声明变量“@1600”

4

2 回答 2

4

GRANT是保留关键字。它应该被分隔以避免错误,例如

INSERT INTO dbo.[Grant] ....

您的参数中也有拼写错误,

        @1600,
        @1602,
        @1603,
        @1608,
        @1612,

什么时候应该

        @L1600,
        @L1602,
        @L1603,
        @L1608,
        @L1612,
于 2013-03-12T16:29:11.700 回答
0

我建议将您的表名从保留关键字 [Grant] 更改为非保留关键字,即 [Grants]。另外,我建议不要以数字开头的列名。因此,例如,在 [1600] 前加上一些描述性的东西。我以“费用”为例。这是一个适合您的工作代码示例:

-- DROP TABLE dbo.Grants
CREATE TABLE dbo.Grants
(
GrantNumber VARCHAR(50), 
GrantName VARCHAR(100),
ProjectCode VARCHAR(50),
LOCID INT,
Comments VARCHAR(100),
Provider VARCHAR(100),
ISAT NCHAR(10),
TypeID INT,
Slots FLOAT,
Patients FLOAT,
FundingMethod VARCHAR(50),
Fee1600 MONEY,
Fee1602 MONEY,
Fee1603 MONEY,
Fee1608 MONEY,
Fee1612 MONEY,
ADAAFunding MONEY,
ProgramBudget MONEY,
Jurisdiction INT,
VersionType VARCHAR(20),
VersionNum INT,
SubmitDate DATETIME
);

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GrantsInsert_sp]
    -- Add the parameters for the stored procedure here
    (@GrantNumber AS VARCHAR(50), 
     @GrantName AS VARCHAR(100),
     @ProjectCode AS VARCHAR(50),
     @LOCID AS INT,
     @Comments AS VARCHAR(100),
     @Provider AS VARCHAR(100),
     @ISAT AS NCHAR(10),
     @TypeID AS INT,
     @Slots AS FLOAT,
     @Patients AS FLOAT,
     @FundingMethod AS VARCHAR(50),
     @Fee1600 AS MONEY,
     @Fee1602 AS MONEY,
     @Fee1603 AS MONEY,
     @Fee1608 AS MONEY,
     @Fee1612 AS MONEY,
     @ADAAFunding AS MONEY,
     @ProgramBudget AS MONEY,
     @Jurisdiction AS INT,
     @VersionType AS VARCHAR(20),
     @VersionNum AS INT)
AS BEGIN
 INSERT INTO dbo.Grants
    (GrantNumber,
     GrantName,
     ProjectCode,
     LOCID,
     Comments,
     Provider,
     ISAT,
     TypeID,
     Slots,
     Patients,
     FundingMethod,
     Fee1600,
     Fee1602,
     Fee1603,
     Fee1608,
     Fee1612,
     ADAAFunding,
     ProgramBudget,
     Jurisdiction,
     VersionType,
     VersionNum, 
     SubmitDate)
      -- Insert statements for procedure here
    SELECT @GrantNumber, 
            @GrantName,
            @ProjectCode,
            @LOCID,
            @Comments,
            @Provider,
            @ISAT,
            @TypeID,
            @Slots,
            @Patients,
            @FundingMethod,
            @Fee1600,
            @Fee1602,
            @Fee1603,
            @Fee1608,
            @Fee1612,
            @ADAAFunding,
            @ProgramBudget,
            @Jurisdiction,
            @VersionType,
            @VersionNum,
            GETDATE();
END;

EXECUTE [GrantsInsert_sp] 'abc', 'abc', 'abc', 1, 'abc', 'abc', 'abc', 1, 1, 1, 'abc', 1, 1, 1, 1, 1, 1, 1, 1, 'abc', 1;

SELECT * FROM dbo.Grants;
于 2013-03-12T16:47:27.137 回答