質問

In my ColdFusion file previously i was using inline query like

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>

Now i want to convert this query to stored procedure.

I have written like this

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

Is the only way is to create a dynamic sql string and execute it or is there any other way exist?

How to handle these conditons?

<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> 
役に立ちましたか?

解決

You can do this with just using an OR statement in your WHERE criteria:

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)

Depending on how your passing the lower and upper limits, you may need to alter the OR criteria above. For example:

(Order_Total <= @upperPriceLimit OR @upperPriceLimit IS NULL)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top