本文介绍了SQL:OPENROWSET,无法为请求字符串构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想构造与 OPENROWSET 方法一起使用的查询.
I want to construct the query used with the OPENROWSET method.
示例:
SELECT *
FROM
OPENROWSET
('SQLOLEDB', 'srv'; 'login'; 'mdp';
'SELECT *
FROM Case
WHERE ID = ' + @caseID)
但是当我这样做时,我收到错误:'+' 附近的语法不正确
But when I do that I get the error: Incorrect Syntax near '+'
如何构建查询?谢谢
推荐答案
虽然 OPENROWSET
中的查询被指定为一个字符串,这意味着它看起来非常像一个动态查询,但语法并不允许它以同样的方式构建,由部分组成.
Although the query in OPENROWSET
is specified as a string and by that means looks very much like a dynamic query, the syntax does not allow it to be constructed likewise, out of parts.
恐怕您必须构建一个动态查询,它将调用 OPENROWSET
,如下所示:
I'm afraid, you'll have to build a dynamic query, which will call OPENROWSET
, something like this:
SET @sql = '
SELECT *
FROM
OPENROWSET
(''SQLOLEDB'', ''srv''; ''login''; ''mdp'';
''SELECT *
FROM Case
WHERE ID = ' + @caseID + ''')';
EXEC(@sql);
这篇关于SQL:OPENROWSET,无法为请求字符串构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!