声明变量,但错误消息说不

时间:2015-09-10 20:00:31

标签: sql sql-server

在下面显示的存储过程中,我收到此错误,但是我声明了@Remarks,所以我看不出是什么问题:

  

消息137,级别15,状态2,过程sp_Insert_EMR_PATIENT_CHECK_TRACKING,第53行
  必须声明标量变量“@REMARKS”。

代码:

create PROCEDURE [dbo].[sp_Insert_EMR_PATIENT_CHECK_TRACKING] 
     @HOUR int,
     @TIME int,
     @MRN nchar(10),
     @NURSING_UNIT varchar(12),
     @CHECK_DATE datetime,
     @MONITOR_CODE nchar(10),
     @Remarks nvarchar(500),
     @Pain_Level int
AS
BEGIN
    SET NOCOUNT ON;

    If @Remarks = 'undefined'
    BEGIN
        SET @Remarks = null
    END

    IF NOT EXISTS (SELECT *
                   FROM [OGEN].[EMR_PATIENT_CHECK_TRACKING]
                   WHERE CHECK_DATE = @CHECK_DATE
                     AND MRN = @MRN
                     AND NURSING_UNIT = @NURSING_UNIT
                     AND HOUR = @HOUR
                     AND TIME = @TIME
                     AND MONITOR_CODE = @MONITOR_CODE)
    BEGIN
        INSERT INTO [OGEN].[EMR_PATIENT_CHECK_TRACKING] ([CHECK_DATE], [MRN], [NURSING_UNIT], [HOUR], [TIME], [MONITOR_CODE], [Pain_Level])
        VALUES (@CHECK_DATE, @MRN, @NURSING_UNIT, @HOUR, @TIME, @MONITOR_CODE, @Pain_Level)

        IF NOT EXISTS (SELECT *
                       FROM [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
                       WHERE CHECK_DATE = @CHECK_DATE
                         AND MRN = @MRN
                         AND NURSING_UNIT = @NURSING_UNIT
                         AND HOUR = @HOUR
                         AND MONITOR_CODE = @MONITOR_CODE)
        BEGIN
           INSERT INTO [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK] ([CHECK_DATE], [MRN], [NURSING_UNIT], [HOUR], [MONITOR_CODE], [REMARKS])
           VALUES (@CHECK_DATE, @MRN, @NURSING_UNIT, @HOUR, @MONITOR_CODE, ISNULL(@REMARKS,''))
        END
    ELSE
    BEGIN
        UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
        SET REMARKS = ISNULL(@Remarks,REMARKS)
        WHERE CHECK_DATE = @CHECK_DATE
          AND [MRN] = @MRN
          AND [NURSING_UNIT] = @NURSING_UNIT
          AND [HOUR] = @HOUR     
          AND [MONITOR_CODE] = @MONITOR_CODE
    END
  END
  ELSE
  BEGIN
      UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING] 
      SET [Pain_Level]=@Pain_Level
      WHERE CHECK_DATE = @CHECK_DATE
        AND MRN = @MRN
        AND NURSING_UNIT = @NURSING_UNIT
        AND HOUR = @HOUR
        AND TIME = @TIME
        AND MONITOR_CODE = @MONITOR_CODE

      UPDATE [OGEN].[EMR_PATIENT_CHECK_TRACKING_REMARK]
      SET REMARKS = Isnull(@Remarks,REMARKS)
      WHERE CHECK_DATE = @CHECK_DATE
        AND [MRN] = @MRN
        AND [NURSING_UNIT] = @NURSING_UNIT
        AND [HOUR] = @HOUR 
        AND [MONITOR_CODE] = @MONITOR_CODE
   END
END

2 个答案:

答案 0 :(得分:1)

使用camel case声明参数@Remarks,稍后在存储过程中将其引用为@REMARKS - 可能是您使用区分大小写的排序规则,这将导致您看到的错误。

如果您的数据库排序规则包含" _CI" (例如Latin1_General_CI_AS)然后它不区分大小写,如果它包含" _CS"这是一个区分大小写的排序规则。

答案 1 :(得分:1)

根据错误信息,您的服务器设置为区分大小写。这可以通过

确认
SELECT CONVERT (varchar, SERVERPROPERTY('collation'));

在区分大小写的服务器上运行上述语句将返回SQL_Latin1_General_CP1_CS_AS

要绕过该错误,您必须将@REMARKS重命名为已定义的@Remarks,或附加COLLATE语句以将敏感度更改为不区分大小写。在我的服务器上,这将是SQL_Latin1_General_CP1_CI_AS

更好的选择是保持与变量命名约定一致。

在同一区分大小写的服务器上,

DECLARE @value VARCHAR(10) = 'hello'
SELECT @VALUE                                        <<< ERROR.


SELECT @value COLLATE SQL_Latin1_General_CP1_CI_AS   <<< returns, 'hello'