必须在SQL SERVER和C中声明标量变量#
我这里有一个方法,通过代码传递sql字符串并获取值。这是失败的,我想通过查看sql资源管理器本身中传递的sql来了解原因 这就是整个方法,您可以看到它从正在创建的“new”对象获取“@”值必须在SQL SERVER和C中声明标量变量#,sql,sql-server,Sql,Sql Server,我这里有一个方法,通过代码传递sql字符串并获取值。这是失败的,我想通过查看sql资源管理器本身中传递的sql来了解原因 这就是整个方法,您可以看到它从正在创建的“new”对象获取“@”值 var rowsUpdated = await this.repository.ExecuteAsync( @"merge into MyTable WITH (HOLDLOCK) as dst using (s
var rowsUpdated = await this.repository.ExecuteAsync(
@"merge into MyTable WITH (HOLDLOCK) as dst
using (select @id as M_id,
@username as Username,
@emailVerificaton as EmailVerification,
) as src on dst.M_id = src.M_id and dst.Username = src.Username
when matched then
update set dst.EmailVerification = src.EmailVerification,
when not matched then
insert(M_id, Username, EmailVerification)
values(src.M_id, src.Username, src.EmailVerification);", new
{
id = id, //e.g 1
username = username, //e.g "dros"
emailVerificaton = emailVerificaton, //e.g true
})
当我尝试运行此sql代码时:
merge into MyTable WITH (HOLDLOCK) as dst
using (select @id as M_id,
@username as Username,
@emailVerificaton as EmailVerification,
) as src on dst.M_id = src.M_id and dst.Username = src.Username
when matched then
update set dst.EmailVerification = src.EmailVerification,
when not matched then
insert(M_id, Username, EmailVerification)
values(src.M_id, src.Username, src.EmailVerification)
我收到这样的投诉:
必须声明标量变量“@username”
不确定如何传入这些变量,我尝试的所有操作都会导致语法错误 在SSMS中,在merge语句之前声明@id int=1(etc)。或者甚至更基本的(仅用于测试)将参数替换为文字值,如EmailVerification、和
更新集dst.EmailVerification=src.EmailVerification、
,否则看起来是正确的,假设您使用的是Dapper。如果您试图在SSMS中运行它,您可以在包含值之前放置declare
语句