简体   繁体   English

存储过程操作数类型冲突:日期与int不兼容

[英]Stored Procedure Operand type clash: date is incompatible with int

This is my stored procedure 这是我的存储过程

ALTER PROCEDURE [dbo].[sp_ListFirmJobFilter]
    (@Status as nvarchar(10),
     @CityID as nvarchar(10),
     @DataStart as date,
     @DataEnd as date,
     @FirmID as nvarchar(10)
)
AS
BEGIN
    DECLARE @Sql NVARCHAR(MAX) 
    SET @Sql = N'SELECT * FROM Jobs WHERE 1=1'

    IF @Status IS NOT NULL
    BEGIN
        SET @Sql += ' AND Status = '+@Status+''
    END

    IF @CityID IS NOT NULL
    BEGIN
        SET @Sql += ' AND CityID = '+@CityID+''
    END

    IF @FirmID IS NOT NULL
    BEGIN
        SET @Sql += ' AND FirmID = '+@FirmID+' '
    END

    IF @DataStart IS NOT NULL
    BEGIN
        SET @Sql += ' AND (InsertedDate >= ' + CONVERT(VARCHAR(50), @DataStart)
        SET @Sql += ' AND InsertedDate <= ' + CONVERT(VARCHAR(50), @DataEnd)
        SET @Sql += ')'
    END 

    PRINT(@Sql)
    EXEC(@Sql)
END

The command completed successfully. 该命令成功完成。

But when I execute that stored procedure with that parameter 但是当我使用该参数执行该存储过程时

DECLARE @return_value int

EXEC @return_value = [dbo].[sp_ListFirmJobFilter]
                        @Status = NULL,
                        @CityID = null,
                        @DataStart = N'2016-06-26',
                        @DataEnd = N'2016-06-28',
                        @FirmID = 4

SELECT  'Return Value' = @return_value
GO

I get an error: 我收到一个错误:

Operand type clash: date is incompatible with int 操作数类型冲突:日期与int不兼容

I look for executed SQL command (Print SQL string). 我寻找执行的SQL命令(打印SQL字符串)。 This is executed command: 这是执行的命令:

SELECT * 
FROM Jobs 
WHERE 1=1 
  AND FirmID = 4  
  AND (InsertedDate >= 2016-06-26 AND InsertedDate <= 2016-06-28)

When I execute that it give same error. 当我执行它给同样的错误。 I changed it to: 我将其更改为:

SELECT * 
FROM Jobs 
WHERE 1=1 
  AND FirmID = 4  
  AND (InsertedDate >= '2016-06-26' AND InsertedDate <= '2016-06-28')

this worked. 这工作。

What should I do? 我该怎么办? InsertedDate type is date InsertedDate类型是日期

Wrap dates in single quotes 将日期用单引号引起来

SET @Sql += ' AND (InsertedDate >= ''' + CONVERT(VARCHAR(50), @DataStart) +''''
SET @Sql += ' AND InsertedDate <= ''' + CONVERT(VARCHAR(50), @DataEnd) +''''
SET @Sql += ')'

As mentioned by Squirrel, and you noticed it yourself as well, dates have to be enclosed in single quotes. 正如Squirrel所提到的,您自己也注意到了这一点,日期必须用单引号引起来。

Personally I like to use QUOTENAME for this: 我个人喜欢为此使用QUOTENAME

SET @Sql += ' AND InsertedDate <= ' + QUOTENAME(CONVERT(VARCHAR(50), @DataEnd), '''')

您需要将日期字符串括在单引号中

SET @Sql += ' AND (InsertedDate >= ==' + CONVERT(VARCHAR(50), @DataStart) + ''''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 操作数类型冲突:日期与int不兼容? - Operand type clash: date is incompatible with int? 存储过程-操作数类型冲突:smallint与uniqueidentifier不兼容 - Stored procedure - Operand type clash: smallint is incompatible with uniqueidentifier 如何修复错误 - 操作数类型冲突:日期与 int 不兼容 - How to fix the error - Operand type clash: date is incompatible with int 操作数类型clash int与sql server中的日期不兼容 - operand type clash int is incompatible with date in sql server SQL Server 2012(触发器)操作数类型冲突:int与日期不兼容 - SQL Server 2012 (triggers) Operand type clash: int is incompatible with date 操作数类型冲突:日期与用CONVERT()解决的int不兼容...为什么? - Operand type clash: date is incompatible with int solved with CONVERT() … but WHY? 如何解决操作数类型冲突:date is incompatible with int - How to resolve Operand type clash: date is incompatible with int SQL错误IS操作数类型冲突:int与日期不兼容 - SQL ERROR IS Operand type clash: int is incompatible with date SQL Server Operand类型冲突:日期与int不兼容 - Sql Server Operand type clash: date is incompatible with int SQL语句不起作用 - “操作数类型冲突:日期与int不兼容” - SQL statement not working - "Operand type clash: date is incompatible with int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM