Sunday, February 12, 2012

Building an SQL statement in a Stored Procedure

Hi everybody!!
The title says it all!!!
Joking aside, I need to build an SQL statement by concatenating fields and
parameters and if conditions and then execute that sql statement within the
same SP.
Is this possible, and if yes, how exactly?
Thanks,
IvanA lot of text, but I strongly encourage you to do the reading so you can mak
e an informed decision:
http://www.sommarskog.se/dyn-search.html
http://www.sommarskog.se/dynamic_sql.html
Home page: http://www.sommarskog.se/
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Ivan Debono" <ivanmdeb@.hotmail.com> wrote in message news:eGYyPVBlFHA.4012@.TK2MSFTNGP10.ph
x.gbl...
> Hi everybody!!
> The title says it all!!!
> Joking aside, I need to build an SQL statement by concatenating fields and
> parameters and if conditions and then execute that sql statement within th
e
> same SP.
> Is this possible, and if yes, how exactly?
> Thanks,
> Ivan
>|||I managed to build a string but I need to use it in a sub select. It doesn't
work.
I think the problem is because it apends a ' at the beginning and at the
end.
Is there a way to solve it?
Thanks,
Ivan
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> schrieb
im Newsbeitrag news:eIsEccBlFHA.1968@.TK2MSFTNGP14.phx.gbl...
> A lot of text, but I strongly encourage you to do the reading so you can
make an informed decision:
> http://www.sommarskog.se/dyn-search.html
> http://www.sommarskog.se/dynamic_sql.html
> Home page: http://www.sommarskog.se/
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Ivan Debono" <ivanmdeb@.hotmail.com> wrote in message
news:eGYyPVBlFHA.4012@.TK2MSFTNGP10.phx.gbl...
and
the|||You need to execute the whole query, like:
SET @.sql = 'SELECT ... FROM ... WHERE ... IN (SELECT... ...) ... '
And then do a PRINT of the @.sql variable before executing it so you can see
what the problem is. And
do read those articles if you haven't already.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Ivan Debono" <ivanmdeb@.hotmail.com> wrote in message news:OmJbYtBlFHA.1148@.TK2MSFTNGP12.ph
x.gbl...
>I managed to build a string but I need to use it in a sub select. It doesn'
t
> work.
> I think the problem is because it apends a ' at the beginning and at the
> end.
> Is there a way to solve it?
> Thanks,
> Ivan
>
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> schrieb
> im Newsbeitrag news:eIsEccBlFHA.1968@.TK2MSFTNGP14.phx.gbl...
> make an informed decision:
> news:eGYyPVBlFHA.4012@.TK2MSFTNGP10.phx.gbl...
> and
> the
>|||Print your query and test it in QA before attempting to execute it. That muc
h
I can say without seeing your script.
ML|||Look up coupling and cohesion in a book on basic software engineering.
That will tell you in detail why this is a very, very poor programming
pratice that good programmers would never use.|||Could you post an example of a document explaining this? Telling other peopl
e
to go find things that are basic to you is good for your ego but useless for
anything else. You won't convince a development team to do things right by
telling them how stupid they are.
Jerry
"--CELKO--" wrote:

> Look up coupling and cohesion in a book on basic software engineering.
> That will tell you in detail why this is a very, very poor programming
> pratice that good programmers would never use.
>

No comments:

Post a Comment