Home All Groups Group Topic Archive Search About

Building an SQL statement in a Stored Procedure

Author
29 Jul 2005 8:18 AM
Ivan Debono
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,
Ivan

Author
29 Jul 2005 8:25 AM
Tibor Karaszi
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/
Show quote
"Ivan Debono" <ivanm***@hotmail.com> wrote in message news:eGYyPVBlFHA.4012@TK2MSFTNGP10.phx.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 the
> same SP.
>
> Is this possible, and if yes, how exactly?
>
> Thanks,
> Ivan
>
>
Author
29 Jul 2005 9:01 AM
Ivan Debono
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_kara***@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:
Show quote
>
> 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" <ivanm***@hotmail.com> wrote in message
news:eGYyPVBlFHA.4012@TK2MSFTNGP10.phx.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
the
> > same SP.
> >
> > Is this possible, and if yes, how exactly?
> >
> > Thanks,
> > Ivan
> >
> >
Author
29 Jul 2005 9:17 AM
Tibor Karaszi
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.

Show quote
"Ivan Debono" <ivanm***@hotmail.com> wrote in message news:OmJbYtBlFHA.1148@TK2MSFTNGP12.phx.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_kara***@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" <ivanm***@hotmail.com> wrote in message
> news:eGYyPVBlFHA.4012@TK2MSFTNGP10.phx.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
> the
>> > same SP.
>> >
>> > Is this possible, and if yes, how exactly?
>> >
>> > Thanks,
>> > Ivan
>> >
>> >
>
>
Author
29 Jul 2005 9:19 AM
ML
Print your query and test it in QA before attempting to execute it. That much
I can say without seeing your script.


ML
Author
29 Jul 2005 4:28 PM
--CELKO--
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.
Author
2 Aug 2005 8:11 PM
Jerry Pisk
Could you post an example of a document explaining this? Telling other people
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

Show quote
"--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.
>
>

AddThis Social Bookmark Button