c# - 如何修复 'The data types datetime and uniqueidentifier are incompatible in the add operator.'

标签 c# sql .net sql-server database

DECLARE @caseId UNIQUEIDENTIFIER,
    @fieldId UNIQUEIDENTIFIER,
    @columnName NVARCHAR(MAX),
    @dateValue DATETIME,
    @tableName nvarchar(MAX);

set @command = 'update ' + @tableName + ' set ' + @columnName + ' = ' + @dateValue + ' where Id = ''' + @caseId + '''';
exec sp_executesql @command;

运行更新命令时出现错误。 - 数据类型 datetime 和 uniqueidentifier 在 add 运算符中不兼容。 有人可以帮忙吗?

最佳答案

我认为您应该使用日期和 id 值的查询参数,而不是将它们混杂在查询字符串中。

看起来像:

declare 
    @pCaseId UNIQUEIDENTIFIER,
    @pFieldId UNIQUEIDENTIFIER,
    @pColumnName NVARCHAR(MAX),
    @pDateValue DATETIME,
    @pTableName nvarchar(MAX);

set @command = 'update ' + @pTableName + ' set ' + @pColumnName + ' = @dateValue where Id = @caseId';
exec sp_executesql 
    @command,
    N'@dateValue DATETIME, @caseId UNIQUEIDENTIFIER',
    @pDateValue, 
    @pCaseId
;

关于c# - 如何修复 'The data types datetime and uniqueidentifier are incompatible in the add operator.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485799/

相关文章:

.net - 为什么 Console.ReadKey() 会阻止在另一个线程中调用的 Console.WriteLine 的输出?

c# - 如何在 ASP.Net Core 中注入(inject) WCF 服务客户端?

c# - 将 Winform 应用程序转换为 WPF 应用程序

php - 如果我想从两个表中获取数据,最佳做法是什么?

mysql - 从上个月的价格变化表中获取最后 2 条更新的unit_price

c# - Ioc 中繁忙的构造函数——它们是代码的味道吗?

c# - 如何将 'where not in' 转换为子查询表不是实体的 NHibernate 查询?

c# - streamreader 读取 xml 文件,然后 file.move 移动 xml 文件 - c#

c# - 需要一些关于引用类型的说明

mysql - 如何在mysql中获取下一个自动增量ID