Dynamic SQL into SP

Post your queries regarding SQL here

Dynamic SQL into SP

Postby admin » Mon Jan 11, 2010 9:21 am

If we have a query like these, we can just use by gate SP logic.

SELECT * from MyTable
where col1 = @val1
<cfif some condition>
AND col2 = @val2
<cfelseif condition2>
AND col3 = @val3
<cfelse>
AND col4 like @ val4
</cfif>


In CF part:

<cfset orderby =3>
<cfif some condition>
<cfset orderby =1>
<cfelseif condition2>
<cfset orderby =2>
<cfelse>
<cfset orderby =3>
</cfif>
pass this orderby as a parameter to the sp.
SELECT *
FROM MyTable
WHERE col1 = @val1
AND (
(@ orderby = 1 AND col2 = @val2)
OR
(@ orderby = 2 AND col3 = @val3)
OR
(@ orderby = 3 AND col4 like @val4)
)
admin
Site Admin
 
Posts: 71
Joined: Tue Jun 16, 2009 4:08 am

Return to SQL Forum

Who is online

Users browsing this forum: No registered users and 1 guest

cron