SQL Server执行误解数据库名称

huangapple go评论50阅读模式
英文:

SQL Server EXEC misinterpreting database name

问题

这部分的翻译如下:

This: @query = `SELECT Count(*) FROM ReportServer$DEVSVR1.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'`
executes fine if copied into new query window. But fails EXEC @query with this error:

> Database 'SELECT Count(*) FROM ReportServer$DEVSVR1' does not exist.

SQL is incorrectly parsing the phrase 'SELECT Count(*) FROM ReportServer$DEVSVR1'
    as the database name.

翻译成中文为:

这段代码:@query = `SELECT Count(*) FROM ReportServer$DEVSVR1.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'`
如果复制到新的查询窗口中执行是正常的。但是使用EXEC @query时会出现以下错误:

> 数据库 'SELECT Count(*) FROM ReportServer$DEVSVR1' 不存在。

SQL错误地将短语 'SELECT Count(*) FROM ReportServer$DEVSVR1' 解析为数据库名称。
英文:

This: @query = SELECT Count(*) FROM ReportServer$DEVSVR1.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'
executes fine if copied into new query window. But fails EXEC @query with this error:

> Database 'SELECT Count(*) FROM ReportServer$DEVSVR1' does not exist.

SQL is incorrectly parsing the phrase 'SELECT Count(*) FROM ReportServer$DEVSVR1'
as the database name.

答案1

得分: 0

EXEC @query 意味着执行存储过程,该存储过程的名称在 @query 变量中。
您可能想要使用 EXEC(@query) 来执行字符串。

英文:

EXEC @query means execute procedure with name that is in @query variable.
You probably want EXEC(@query) which executes the string

huangapple
  • 本文由 发表于 2023年3月9日 23:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定