Groups | Blog | Home
all groups > sql server programming > september 2004 >

sql server programming : DYNAMIC SQL TECHNIQUE


klumsy NO[at]SPAM xtra.co.nz
9/17/2004 10:26:35 PM
with dynamic SQL i'm of all sort of quoting issues and the ugliness of
such code to maintain etc.. i've had a couple of simple ideas to deal
with quoting and inserting variables into dynamic sql, i just want to
pass them pass people just in case there is a huge glaring hole in my
logic

declare @ratdog varchar(3000)
declare @firstname varchar(20)
set @firstname = 'mark'

set @ratdog =
' select top 100 *
from employee
where em_lname like "pr%"
and em_fname = "<<FIRSTNAME>>"
'
set @ratdog = REPLACE(@ratdog,'<<FIRSTNAME>>',@firstname)
set @ratdog = REPLACE(@ratdog,'"','''')
exec (@ratdog)

basically typing " inside the text so you don't haev to to + ''' +
'bla' + ''' sort of thing all over the time (or even just the + @qt +
'bla' + @qt sort of and you don't have to break up the string for
inserting variables into the equation . i,e the <<FIRSTNAME>> , you
only really have to break up the string for if then claues for whether
code is to be added to the query (optional where clauses for
instance..

are there any holes in this solution.. should i use something other
than " for quotes and << >> for variables?
i know that for such a system, i could easily make a program to
convert any plain sql into statements like the above.

David G.
9/18/2004 1:53:15 AM
[quoted text, click to view]

Have you considered using sp_executesql instead of exec when executing
dynamic SQL. The function support parameters.

declare @ratdog nvarchar(3000)
declare @firstname nvarchar(20)
set @firstname = 'mark'

set @ratdog =
' select top 100 *
from employee
where em_lname like "pr%"
and em_fname = @firstname"
'
exec sp_executesql @ratdog, N'@firstname nvarchar(20)', @firstname



--
David Gugick
Imceda Software
www.imceda.com
John Bell
9/18/2004 8:45:09 AM
Hi

If are going to use dynamic SQL you should read:
http://www.sommarskog.se/dynamic_sql.html
and
http://www.sommarskog.se/dyn-search.html

John

[quoted text, click to view]

David Portas
9/18/2004 9:05:06 AM
Why do you want to write dynamic SQL just to pass search parameters? Use
stored procedures for this kind of functionality.

--
David Portas
SQL Server MVP
--

klumsy NO[at]SPAM xtra.co.nz
9/19/2004 3:05:29 PM
thank you all for your hints , links and replies

as for why i'd use dynamic sql in a stored procedure rather than just
a stored procedure..
the example i showed was very simplistic , not showing the need for
dynamic sql, but mostly its because of 'dyanmic where clauses' ,
namely these are reports, often complicated aggregating data from
various tables with hundreds of millions of rows..

normally i use a technique like

where
fedid = isnull(@fedid,fedid) and
customerid = isnull(@customerid,customerid) etc
etc etc
however sql optomiser doesn't optmise things that well with the above
technique, and i need an index seek in the case scanerios i am working
with, and i can only get that with imeediate sql, or dynamic sql, not
with any of the techniques i use.

i allready use a fair bit of dynamic sql for certian circumstances,
but as we are converting all our reports (with sql genreated inside
programming code) to stored procedures, we are going to have to use a
lot more, and i want to make it as easy/readable and less error prone
AddThis Social Bookmark Button