C#动态SQL查询

时间:2019-03-04 16:23:55

标签: c# sql

SqlCommand s2 = new SqlCommand();
s2.CommandText = @"declare @sql nvarchar(max);

set @sql = 'select sys_RespNum from ' + quotename(@tableName) + 'where' + 'respid' + 'in' + '(' + @emails + ')';
exec (@sql);

代码有什么问题?我收到错误消息:

  

@emails附近的语法不正确

1 个答案:

答案 0 :(得分:3)

您错过了空间

set @sql = 'select sys_RespNum from ' + quotename(@tableName) + ' where ' + ' respid ' + ' in ' + '( ' + @emails + ' )';
exec (@sql);