Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Eliminate Dynamic SQL - WHERE Clause Expand / Collapse
Author
Message
Posted Tuesday, October 5, 2010 9:10 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, December 30, 2013 2:21 PM
Points: 552, Visits: 197
I have code using dynamic SQL that has dynamic SQL because of changing WHERE Clause. I want to eliminate dynamic SQL and use static SQL instead. But my new code does not return any rows. I am unable to set column name on left side of WHERE clause. Is what I am doing even possible?

Thanks in advance!


----- Table DDL script with data
CREATE TABLE dbo.TEST
(
CUSTOMER_ID VARCHAR(10)
, FIRST_NAME VARCHAR(60)
, LAST_NAME VARCHAR(60)
)

INSERT INTO dbo.TEST (CUSTOMER_ID, FIRST_NAME, LAST_NAME)
VALUES
('0000000001', 'JOHN', 'SMITH')
, ('0000000002', 'JANE', 'SMITH')
, ('0000000003', 'JACK', 'FROST')
, ('0000000004', 'KEVIN', 'SMITH')
, ('0000000005', 'DAN', 'BATES')
, ('0000000006', 'RICHARD', 'WANG')
, ('0000000007', 'REBECCA', 'BLUE')
, ('0000000008', 'LAUREN', 'LOCKS')
, ('0000000009', 'NANCY', 'GOLDEN')
, ('0000000010', 'TONY', 'DANZA')


----- Existing Dynamic SQL - works
DECLARE
@SQL VARCHAR(1000)
, @WHERE_CLAUSE VARCHAR(200) = 'CUSTOMER_ID = ''0000000008'''

SELECT
@SQL = '
SELECT
*
FROM
dbo.TEST'

----- WHERE Clasue
IF @WHERE_CLAUSE IS NOT NULL
SELECT
@SQL = @SQL
+ '
WHERE
' + @WHERE_CLAUSE


EXECUTE (@SQL)

GO


----- Want to do
-- This works
DECLARE
@SQL VARCHAR(1000)
, @WHERE_CLAUSE VARCHAR(200) = 'CUSTOMER_ID = 0000000008'


SELECT
*
FROM
dbo.TEST AS C
WHERE
CUSTOMER_ID = (
CASE
WHEN @WHERE_CLAUSE IS NOT NULL
THEN LTRIM(RTRIM(SUBSTRING(@WHERE_CLAUSE, PATINDEX('%=%', @WHERE_CLAUSE)+1, LEN(@WHERE_CLAUSE))))
ELSE '1'
END)


-- Not working
SELECT
*
FROM
dbo.TEST AS C
WHERE
(CASE
WHEN @WHERE_CLAUSE IS NOT NULL
THEN LTRIM(RTRIM(SUBSTRING(@WHERE_CLAUSE, 1, PATINDEX('%=%', @WHERE_CLAUSE)-1)))
ELSE '1'
END
) = (
CASE
WHEN @WHERE_CLAUSE IS NOT NULL
THEN LTRIM(RTRIM(SUBSTRING(@WHERE_CLAUSE, PATINDEX('%=%', @WHERE_CLAUSE)+1, LEN(@WHERE_CLAUSE))))
ELSE '1'
END)



Post #998466
Posted Tuesday, October 5, 2010 10:18 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
You can't "set a column name" without using dynamic SQL. You can use a CASE expression to choose between two column VALUES, but this could very well cause performance problems. Why do you want to avoid dynamic SQL ??

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #998550
Posted Tuesday, October 5, 2010 11:33 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, December 30, 2013 2:21 PM
Points: 552, Visits: 197
Thanks for your response. This particular bit of dynamic sql code was part of a much bigger part of dynamic sql infested code. I was able to get rid of the rest of the dynamic sql logic except this.

This has made the code I am working on much easier to read and comprehend.

Oh well, I guess I will have to live with this one bit of dynamic sql code!




Post #998607
Posted Tuesday, October 5, 2010 11:37 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 3, 2014 10:16 AM
Points: 340, Visits: 411
dynamic sql is also more susceptible to sql injection. you might find it cleaner to have 2 queries with different formatted where clauses and an if statement that chooses which sql statement to run.
Post #998610
Posted Tuesday, October 5, 2010 12:16 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:28 AM
Points: 3,113, Visits: 11,537
getoffmyfoot (10/5/2010)
dynamic sql is also more susceptible to sql injection. you might find it cleaner to have 2 queries with different formatted where clauses and an if statement that chooses which sql statement to run.


If correctly done, dynamic SQL is not susceptible to SQL injection.

You just have to make sure the code is correctly parameterized, and that input from the application is not used directly to build SQL statements.





Post #998646
Posted Tuesday, October 5, 2010 1:02 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
Agreed. Parameterized dynamic SQL using sp_executesql not only prevents SQL injection attacks, it also provides for execution plan caching.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #998693
Posted Tuesday, October 5, 2010 1:04 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, December 30, 2013 2:21 PM
Points: 552, Visits: 197
Thanks all. That is what I will do...


Post #998694
Posted Tuesday, October 5, 2010 1:14 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
You're welcome Vishal. But looking at your "working" example, what you are doing isn't really dynamic SQL at all. You are assigning a long string value to your @WHERE_CLAUSE variable and then just eliminating the equal sign and everything to the left. The " Customer_ID = " isn't necessary at all in the value of @WHERE_CLAUSE. The actual test against the customer column is being done expressly in your where clause, not dynamic at all. You might as well just start out assigning it a value of 8.

DECLARE

@CUSTID VARCHAR(20) = '0000000008'

SELECT *
FROM dbo.TEST AS C
WHERE CUSTOMER_ID = isnull(@CUSTID,1)




__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #998705
Posted Tuesday, October 5, 2010 1:16 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, December 30, 2013 2:21 PM
Points: 552, Visits: 197
Well using CUSTOMER_ID is one column they will use in the where clause. In actual scenario, the existing code may have a where clause for some other column - that's how the existing code is! it may as well be FIRST_NAME = 'xxx'


Post #998707
Posted Tuesday, October 5, 2010 1:38 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
Perhaps I'm not explaining clearly enough.

The comparison to the customer ID is hard coded in the WHERE clause. It has nothing to do with the "Customer_ID = " that you initially put in the @WHERE_CLAUSE variable, because you are parsing that out.

You can change the value of @WHERE_CLAUSE to start with "ANYCOLUMN = 0000000008" and the comparison will still be done against the Customer_ID column.

Try altering the second line of your code and see.


-- This works and will always work, but only as a test against the customer_id
DECLARE
@SQL VARCHAR(1000)
, @WHERE_CLAUSE VARCHAR(1000) = 'NOTHING TO THE LEFT OF THE EQUAL SIGN MATTERS = 0000000008'


SELECT
*
FROM
dbo.TEST AS C
WHERE
CUSTOMER_ID =
(
CASE
WHEN @WHERE_CLAUSE IS NOT NULL
THEN LTRIM(RTRIM(SUBSTRING(@WHERE_CLAUSE, PATINDEX('%=%', @WHERE_CLAUSE)+1, LEN(@WHERE_CLAUSE))))
ELSE '1'
END)




__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #998731
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse