我已链接名为MMSHO
的服务器连接。在Oracle服务器中有一个参数化函数,我希望通过OPENQUERY
发送商店代码,然后过滤条件。 :
DECLARE
@OPENQUERY NVARCHAR (MAX),
@STOREID VARCHAR (2),
@ARTICLEID VARCHAR (10),
@SQL NVARCHAR (MAX)
SET @STOREID = 10;
SET @ARTICLEID = 245511;
SET @OPENQUERY = 'SELECT *
FROM OPENQUERY (MMSHO,
''SELECT MS000'
+ @STOREID
+ '77TRP.MW070K01.MW070P01_REKE@MS000'
+ @STOREID
+ '77TRP(ART_NR, VAR, GEBI_NR) AS ARTICLE_NNBP
FROM MS000'
+ @STOREID
+ '77TRP.MW_MA_VAR_GEBI_SNAP@MS000'
+ @STOREID
+ '77TRP
WHERE SUBSYS_ART_NR = '
+ @ARTICLEID
+ '''';
SET @SQL =
'SELECT STORE_NO,
SUBSYS_ART_NO,
ARTICLE_NO,
ARTICLE_DESC,
PUAR,
SUPPLIER_NO,
SORTEN_TEXT AS ARTICLE_VARIANT,
GEBI_NR,
BLOCK_CD,
PACKAGE_CONTENT AS CONTENT,
PACKAGE_TYPE AS CONTENT_TYPE,
VAT AS ARTICLE_VAT,'
+ CHAR (13)
+ '('
+ @OPENQUERY
+ ') AS ARTICLE_NNBP,
ARTICLE_NNSP,
STOCK_QTY AS ARTICLE_STOCK,
CASE WHEN DEPT_NO IS NULL THEN 0 ELSE DEPT_NO END DEPARTMENT
FROM ARTICLE
WHERE STORE_NO = 10 AND SUBSYS_ART_NO = 245511'
PRINT (@SQL)
当我使用EXEC (@SQL)
运行上述查询时,SQL Server会抛出错误:
Msg 156,Level 15,State 1,Line 20
关键字' CASE'附近的语法不正确。Msg 102,Level 15,State 1,Line 20
' DEPARTMENT'附近的语法不正确。
我尝试删除OPENQUERY
和FROM
关键字之间的界限,但没有成功。我究竟做错了什么?你能帮我吗?
打印查询
SELECT STORE_NO,
SUBSYS_ART_NO,
ARTICLE_NO,
ARTICLE_DESC,
PUAR,
SUPPLIER_NO,
SORTEN_TEXT AS ARTICLE_VARIANT,
GEBI_NR,
BLOCK_CD,
PACKAGE_CONTENT AS CONTENT,
PACKAGE_TYPE AS CONTENT_TYPE,
VAT AS ARTICLE_VAT,
(SELECT *
FROM OPENQUERY (
MMSHO,
'SELECT MS0001077TRP.MW070K01.MW070P01_REKE@MS0001077TRP(ART_NR, VAR, GEBI_NR) AS ARTICLE_NNBP
FROM MS0001077TRP.MW_MA_VAR_GEBI_SNAP@MS0001077TRP
WHERE SUBSYS_ART_NR = 245511') AS ARTICLE_NNBP,
ARTICLE_NNSP,
STOCK_QTY AS ARTICLE_STOCK,
CASE WHEN DEPT_NO IS NULL THEN 0 ELSE DEPT_NO END DEPARTMENT
FROM ARTICLE
WHERE STORE_NO = 10 AND SUBSYS_ART_NO = 245511
答案 0 :(得分:0)
问题与关闭括号有关,感谢@ marcello-miorelli的帮助。
应该是这样的:
(SELECT *
FROM OPENQUERY (
MMSHO,
'SELECT MS0001077TRP.MW070K01.MW070P01_REKE@MS0001077TRP(
ART_NR, VAR, GEBI_NR) AS ARTICLE_NNBP
FROM MS0001077TRP.MW_MA_VAR_GEBI_SNAP@MS0001077TRP
WHERE SUBSYS_ART_NR = 245511') AS ARTICLE_NNBP),