Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic where clause

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 08:32:16
Here's my sp - what's the syntax to tag an ORDER BY ReqDate asc on the end

SELECT @SQLStatement = "SELECT * FROM tblOrders WHERE "
+ @WHEREClause

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 08:34:12
SELECT @SQLStatement = 'SELECT * FROM tblOrders WHERE ' + @WHEREClause + ' ORDER BY ReqDate asc'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-24 : 08:49:08
Beware of SQL Injection
http://beyondrelational.com/blogs/madhivanan/archive/2010/05/14/derived-table-new-approach-to-avoid-sql-injection.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 08:49:34
I am now getting an error, but the column is correct and exists in the view. I had to change the ' to " marks

Invalid column name 'ORDER BY ReqDate asc'.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-24 : 08:51:55
What is the result of print @SQLStatement ?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 08:52:10
use single quotes


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 08:58:45
It goes red then
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-24 : 08:59:54
quote:
Originally posted by Pinto

It goes red then


Can you post the actual statement that caused error?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 09:02:13
SELECT @SQLStatement = "SELECT * FROM tblOrders WHERE "
+ @WHEREClause + "ORDER BY ReqDate asc"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-24 : 09:10:42
Change double quotes to single quote

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-24 : 09:11:00
What o/p does Print(@SQLStatement)give?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 09:13:13
How do I run that ?

Print(@SQLStatement)give
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 09:15:42
just do a
print @SQLStatement
or you can do also
select @SQLStatement


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-24 : 09:17:10
quote:
Originally posted by Pinto

How do I run that ?

Print(@SQLStatement)give



Have you seen my previous reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-06-24 : 09:21:07
Yes, I did and I have just done that and it compiles ok. Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 09:22:26
quote:
Originally posted by Pinto

It goes red then


Yes. Literals in single quotes are displayed in red and that is ok!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -