[英]Invalid Column name error during execution of stored procedure
I am not able to execute the stored procedure. 我无法执行存储过程。 It is throwing an error
它抛出一个错误
Invalid Column name 'DW201401'
无效的列名“ DW201401”
Command used to execute the stored procedure: 用于执行存储过程的命令:
exec RM_UTIL_MODE server,'DW201401'
Stored procedure code: 存储过程代码:
ALTER Procedure [dbo].[RM_UTIL_MODE]
@ServerName varchar(50),
@Value varchar(50)
As
Begin
declare @query nvarchar(max)
set @query = N'SELECT mode FROM ' + @ServerName +
N'.master.dbo.sysdatabases WHERE name =' + @Value
exec sp_executesql @query
End
But when I tried to run the query alone as shown below it is giving me result. 但是,当我尝试单独运行查询时,如下所示,它给了我结果。
select mode, name
from server.master.dbo.sysdatabases
where name = 'DW201401'
Presumably, the issue is quotes around @Value
: 大概问题是
@Value
周围的@Value
:
declare @query nvarchar(max)
set @query = N'SELECT mode FROM '
+ @ServerName
+ N'.master.dbo.sysdatabases
WHERE name = '''+@Value+'''';
However, I would use parameter substitution instead: 但是,我改用参数替换:
declare @query nvarchar(max) ;
set @query = N'SELECT mode
FROM ' + @ServerName + N'.master.dbo.sysdatabases
WHERE name = @value';
exec sp_executesql @query, N'@value varchar(50)', @value = @value;
You are already using sp_executesql
, so you might as well use it properly. 您已经在使用
sp_executesql
,因此不妨适当使用它。 Note: you cannot substitute the server name. 注意:您不能替换服务器名称。
EDIT: 编辑:
To elaborate on the comment, I would write the code this way: 为了详细说明评论,我将以这种方式编写代码:
declare @sql nvarchar(max) ;
set @sql = N'
SELECT mode
FROM @ServerName.master.dbo.sysdatabases
WHERE name = @value';
set @sql = replace(@sql, '@ServerName', quotename(@ServerName));
exec sp_executesql @sql, N'@value varchar(50)', @value = @value;
When using dynamic SQL, I no longer piece together the query using string concatenation. 当使用动态SQL时,我不再使用字符串串联来组合查询。 Instead, I put in place holders and use
replace()
. 相反,我放置了占位符并使用
replace()
。 I find that concatenation is hard to maintain and often obscures what the SQL is doing. 我发现连接很难维护,并且常常使SQL正在做什么。 Although there is a bit more overhead in using
replace()
(and I often do it multiple times), it is worth it for preventing errors and maintaining the code (plus, my queries tend to run for a while anyway, so the overhead is minimal compared to the query time). 尽管使用
replace()
会有更多的开销(而且我经常多次执行),但值得避免错误和维护代码(此外,无论如何我的查询都会运行一段时间,所以开销是与查询时间相比最少)。
Your select looks like: 您的选择如下所示:
select mode, name from server.master.dbo.sysdatabases where name = DW201401
so you need to add escaped quotes in your dynamic query: 因此您需要在动态查询中添加转义引号:
exec RM_UTIL_MODE cefmtqcfindv3,'DW201401'
ALTER Procedure [dbo].[RM_UTIL_MODE]
@ServerName varchar(50),@Value varchar(50)
As
Begin
declare @query nvarchar(max)
set @query = N'SELECT mode FROM '
+ @ServerName
+ N'.master.dbo.sysdatabases
WHERE name ='''+@Value+''''
exec sp_executesql @query
End
Just as a suggestion, when you are building a dynamic sql, try using PRINT
instead of EXEC
, then get what is printed and try it out. 就像一个建议一样,在构建动态sql时,请尝试使用
PRINT
而不是EXEC
,然后获取打印的内容并进行尝试。 Most of the times you will know what went wrong. 大多数时候,您会知道出了什么问题。
Just as an example: 举个例子:
ALTER Procedure [dbo].[RM_UTIL_MODE]
@ServerName varchar(50),@Value varchar(50)
As
Begin
declare @query nvarchar(max)
set @query = N'SELECT mode FROM '
+ @ServerName
+ N'.master.dbo.sysdatabases
WHERE name ='''+@Value+''''
PRINT @query
--exec sp_executesql @query
End
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.