|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Building an SQL statement in a Stored ProcedureHi 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 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 quoteTibor 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 > > 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 make an informed decision:im Newsbeitrag news:eIsEccBlFHA.1968@TK2MSFTNGP14.phx.gbl... > A lot of text, but I strongly encourage you to do the reading so you can 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 > > > > 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 quoteTibor 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: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 >> > >> > > > Print your query and test it in QA before attempting to execute it. That much
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 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. > > |
|||||||||||||||||||||||