在我以前的ColdFusion文件中,我使用的是内联查询,如
SELECT *
FROM XYZTABLE
WHERE S.price > 0
AND O.ORIGQUOTEDATE >= <cfqueryparam cfsqltype="cf_sql_date" value="#getParam('startDate')#">
AND O.ORIGQUOTEDATE <= <cfqueryparam cfsqltype="cf_sql_date" value="#getParam('endDate')#">
<cfif len(getParam("lowerPriceLimit")) neq 0 and len(getParam("upperPriceLimit")) neq 0 >
AND O.order_total BETWEEN #getParam("lowerPriceLimit")# AND #getParam("upperPriceLimit")#
<cfelseif len(getParam("lowerPriceLimit")) neq 0 and len(getParam("upperPriceLimit")) eq 0 >
AND O.order_total >= #getParam("lowerPriceLimit")#
<cfelseif len(getParam("lowerPriceLimit")) eq 0 and len(getParam("upperPriceLimit")) neq 0 >
AND O.order_total <= #getParam("upperPriceLimit")#
</cfif>
现在我想将此查询转换为存储过程。
我写得像这样
CREATE PROCEDURE quoteConversionByCategory(
@startDate date,
@endDate date,
@lowerPriceLimit numeric(18),
@upperPriceLimit numeric(18)
)
AS
BEGIN
SELECT *
FROM XYZTABLE
WHERE /*Not sure how to write*/
END
唯一的方法是创建动态sql字符串并执行它,还是有其他方法 存在?
如何处理这些条件?
<cfif len(getParam("bool")) eq 1>
AND (O.wcs_status_id = 9 or (O.wcs_status_id = 1 and datediff(hour,O.origquotedate,O.order_date) > 4) )
<cfelse>
AND O.wcs_status_id IN (9,1)
</cfif>
答案 0 :(得分:3)
只需在OR
条件中使用WHERE
语句即可完成此操作:
CREATE PROCEDURE quoteConversionByCategory(
@startDate date,
@endDate date,
@lowerPriceLimit numeric(18),
@upperPriceLimit numeric(18)
)
AS
BEGIN
SELECT *
FROM XYZTABLE
WHERE price > 0
AND ORIGQUOTEDATE >= @startDate
AND ORIGQUOTEDATE <= @endDate
AND (Order_Total >= @lowerPriceLimit OR @lowerPriceLimit = 0)
AND (Order_Total <= @upperPriceLimit OR @upperPriceLimit = 0)
根据您通过下限和上限的方式,您可能需要更改上面的OR
条件。例如:
(Order_Total <= @upperPriceLimit OR @upperPriceLimit IS NULL)