SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Eliminate Dynamic SQL - WHERE Clause


Eliminate Dynamic SQL - WHERE Clause

Author
Message
Vishal Sinha
Vishal Sinha
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 253
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)



The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5690 Visits: 6900
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? Everybody look what's going down. -- Stephen Stills
Vishal Sinha
Vishal Sinha
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 253
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!



getoffmyfoot
getoffmyfoot
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 412
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.
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6058 Visits: 11771
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.
The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5690 Visits: 6900
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? Everybody look what's going down. -- Stephen Stills
Vishal Sinha
Vishal Sinha
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 253
Thanks all. That is what I will do...



The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5690 Visits: 6900
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? Everybody look what's going down. -- Stephen Stills
Vishal Sinha
Vishal Sinha
Mr or Mrs. 500
Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)Mr or Mrs. 500 (572 reputation)

Group: General Forum Members
Points: 572 Visits: 253
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'



The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5690 Visits: 6900
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? Everybody look what's going down. -- Stephen Stills
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search