0

我有一个程序,我试图根据高于规定参数的度量值来选择结果。但是,我希望大于可以更改为等于或小于。

我尝试将 > 替换为参数,但它给了我错误:sql 语法错误:“PARAM_OP”附近的语法不正确(其中 PARAM_OP 是应替换为操作符的参数的名称。

任何想法都会受到欢迎。

下面的代码:

var_out = 
SELECT
    "Col1" AS "Col1",
    "Col2" AS "Col2",
    "Col3" AS "Col3",
    SUM(Col4) AS "Col4"

FROM <schema>.<view>
WHERE "Col2" = PARAM_1
GROUP BY Col1, Col2, Col3
HAVING SUM(Col4) > PARAM_2
ORDER BY SUM(Col4);
4

1 回答 1

0

我在想象类似的东西。

DECLARE @Code VARCHAR(MAX)
SELECT @Code = 
'
var_out = 
SELECT
    "Col1" AS "Col1",
    "Col2" AS "Col2",
    "Col3" AS "Col3",
    SUM(Col4) AS "Col4"
FROM <schema>.<view>
WHERE "Col2" = ' + PARAM_1 + 
'GROUP BY Col1, Col2, Col3
HAVING SUM(Col4) ' + PARAM_OP + ' ' + PARAM_2 
+ 'ORDER BY SUM(Col4);'


EXEC sp_executesql @Code

因此,您构建了一个看起来像您想要执行的 SQL 的字符串,然后使用您的 RDBMS 提供的任何方式运行 SQL 字符串。在 MSSQL 中,这将是 sp_executesql

于 2012-07-31T14:00:23.570 回答