【发布时间】:2017-08-21 10:21:55
【问题描述】:
使用这段代码我得到了这个错误:
“操作数类型冲突:日期与 int 不兼容”
declare
@StrSql nvarchar(max),
@TableName nvarchar(50),
@Month datetime
set
@TableName = 'dbo.OneTransfer'
set
@month ='2016-02-02'
set
@StrSql ='select * from ' + @TableName + ' where OperateDate >=' + CONVERT(char(10),@Month,20)
execute (@StrSql)
这是我的 SQL 表:
【问题讨论】:
-
不要写这样的代码。使用参数化查询。连接字符串会使您面临 SQL 注入攻击,像这样的转换错误 和 会阻止您传递 Unicode 数据——比如汉字。使用
select * from my table where OperateDate>=@date并使用`sp_executesql传递日期参数
标签: sql-server