我想在下面的sql open查询语句中使用sql参数。但这使我因为语法错误而出错。我不熟悉如何使用单引号作为参数。
select * from openquery(MYSQL_FIS,'
SELECT
CASE WHEN CUSTOMER.CUSTOMERID = ''abc'' THEN ''abcd'' ELSE CASE WHEN PARTICIPANT = 1 THEN ''Market'' ELSE ''Customers'' END END as AccountNo,
''Investment'' Account_Name,
cast(SUM(FACEVALUE)as decimal(38,6)) Amount
FROM DEAL, CUSTOMER
WHERE DEAL.DEALTYPECODE IN (''R'')
AND DEAL.CUSTOMERID = CUSTOMER.CUSTOMERID AND DEAL.SUBACCOUNTID = CUSTOMER.SUBACCOUNTID
AND MATURITYDATE >= date_add(''' + @DatePara2 +''' ,Interval 1 Day)
AND STATUS != ''C''
AND STATUS != ''E''
AND STATUS != ''T''
AND STATUS != ''Q''
AND VALUEDATE < date_add(''' + @DatePara2 + ''',Interval 1 Day)
GROUP BY DEALTYPECODE,
CASE WHEN CUSTOMER.CUSTOMERID = ''C'' THEN ''C'' ELSE CASE WHEN PARTICIPANT = 1 THEN ''Market Participant'' ELSE ''Customers'' END END
)) FIS
group by AccountNo)
答案 0 :(得分:0)
尝试:
select * from openquery(MYSQL_FIS,'
SELECT
CASE WHEN CUSTOMER.CUSTOMERID = ''C'' THEN ''C'' ELSE CASE WHEN PARTICIPANT = 1 THEN ''Market'' ELSE ''Customers'' END END as AccountNo,
''Investment in Repo Agreement'' Account_Name,
cast(SUM(FACEVALUE)as decimal(38,6)) Amount
FROM DEAL, CUSTOMER
WHERE DEAL.DEALTYPECODE IN (''R'')
AND DEAL.CUSTOMERID = CUSTOMER.CUSTOMERID AND DEAL.SUBACCOUNTID = CUSTOMER.SUBACCOUNTID
AND MATURITYDATE >= date_add(''' + @DatePara2 +''' ,Interval 1 Day)
AND STATUS != ''C''
AND STATUS != ''E''
AND STATUS != ''T''
AND STATUS != ''Q''
AND VALUEDATE < date_add(''' + @DatePara2 + ''',Interval 1 Day)
GROUP BY DEALTYPECODE,
CASE WHEN CUSTOMER.CUSTOMERID = ''C'' THEN ''C'' ELSE CASE WHEN PARTICIPANT = 1 THEN ''Market Participant'' ELSE ''Customers'' END END
)) FIS
group by AccountNo')
答案 1 :(得分:0)
根据Microsoft official doc,“ OPENQUERY不接受变量作为参数。”这意味着您也不能在函数内部串联-我想它在第一个结束引号之后不会解析任何内容。
您需要使用EXEC中的整个语句才能使其正常工作:
EXEC ('select * from openquery(MYSQL_FIS,''...
')
,您需要在openquery内的所有地方加双引号,因为整个事情都需要在EXEC内的引号之间,并且您需要再次转义所有内容(请注意,我在链接服务器名称之后放置了用于查询字符串的两个引号),例如
CASE WHEN CUSTOMER.CUSTOMERID = ''CC00001'' THEN ''CBSL''
应该成为
CASE WHEN CUSTOMER.CUSTOMERID = ''''CC00001'''' THEN ''''CBSL''''