﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Robert Marda / Article Discussions / Article Discussions by Author  / How to Build Dynamic Stored Procedures / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 02:37:29 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>I believe that the second portion, "trying to concactinate NULL values" could be a good start in finding out why it is not working.  Thanks again.</description><pubDate>Wed, 09 Jan 2008 08:16:05 GMT</pubDate><dc:creator>markybse</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>[quote][b]markybse (1/8/2008)[/b][hr]... the first exec line fails and the second exec line works.  I am at a loss as to why.DECLARE @SELECT varchar(100)DECLARE @FROM varchar(100)DECLARE @DynamicSQL1 varchar(8000)DECLARE @DynamicSQL2 varchar(8000)DECLARE @DynamicSQL3 varchar(8000)SET @SELECT = 'SELECT 'SET @FROM = 'FROM DATA_SOURCE'EXEC (@SELECT + @DynamicSQL1 + @DynamicSQL2 + @DynamicSQL3 + @FROM)EXEC (@SELECT +''+ @DynamicSQL1 +''+ @DynamicSQL2 +''+ @DynamicSQL3 +''+ @FROM)[/quote][hr]I'm not sure of the exact problem, but I'd guess it stems from one of two likely sources.1. The @DynamicSQL# variables need an interceding space in order for their concatenation to result in syntactically correct SQL.Try:EXEC (@SELECT + ' ' + @DynamicSQL1 + ' ' + @DynamicSQL2 + ' ' + @DynamicSQL3 + ' ' + @FROM)2. You are trying to concatenate Null values.  The solution would be to recast the @DynamicSQL# as a space if the value is Null.</description><pubDate>Tue, 08 Jan 2008 16:27:37 GMT</pubDate><dc:creator>mark hutchinson</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>I made extensive use of stored procedures that created temporary stored procedures for reporting in a data mart application.  This reason I did this was because there could be literally hundreds of different variations of the same basic report summarized along different dimensions and levels.  The input parameters to the stored procedure specified the grouping/sorting levels that were required, and the stored procedure used that to generate the code.It was easier to write one fairly complex procedure than it was to create hundreds of almost identical, simpler procedures for the different variations in the reports.  The biggest advantage was that if there was a bug, I only had to fix it in one procedure instead of hundreds.</description><pubDate>Tue, 08 Jan 2008 15:31:02 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Thank you for the quick reply and yes, I tested your code and it works fine on my system so, in theory, I should have a syntax error.Taking my example below, I mad a change...all the way down, the first exec line fails and the second exec line works.  I am at a loss as to why.DECLARE @SELECT varchar(100)DECLARE @FROM varchar(100)DECLARE @DynamicSQL1 varchar(8000)DECLARE @DynamicSQL2 varchar(8000)DECLARE @DynamicSQL3 varchar(8000)SET @SELECT = 'SELECT 'SET @FROM = 'FROM DATA_SOURCE'EXEC (@SELECT + @DynamicSQL1 + @DynamicSQL2 + @DynamicSQL3 + @FROM)EXEC (@SELECT +''+ @DynamicSQL1 +''+ @DynamicSQL2 +''+ @DynamicSQL3 +''+ @FROM)</description><pubDate>Tue, 08 Jan 2008 15:00:19 GMT</pubDate><dc:creator>markybse</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Seems to work for me in both SQL 2000 and SQL 2005.  This code creates a 16,000+ character command that returns 16 996-char fields and one 3-char field as results.DECLARE @v1 VARCHAR(8000), @v2 VARCHAR(8000)SET @v1 = '''' + LEFT(REPLICATE('1234567890', 100), 996) + ''', 'SET @v2 = REPLICATE(@v1, 8)SET @v1 = @v2EXEC ('SELECT ' + @v1 + @v2 + '''xyz''')Maybe there's a syntax error in your command.</description><pubDate>Tue, 08 Jan 2008 13:10:07 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Does anyone actually test this code or is most of this stuff just rumors?According to most of the information I should be able to create multiple variables...DECLARE @SELECT varchar(100)DECLARE @FROM   varchar(100)DECLARE @DynamicSQL1 varchar(8000)DECLARE @DynamicSQL2 varchar(8000)DECLARE @DynamicSQL3 varchar(8000)SET @SELECT = 'SELECT 'SET @FROM  = 'FROM DATA_SOURCE'EXEC (@SELECT + @DynamicSQL1 + @DynamicSQL2 + @DynamicSQL3 + @FROM)The exec statement only accesses the first 8000 characters and then shuts down.What am I missing/</description><pubDate>Tue, 08 Jan 2008 11:38:04 GMT</pubDate><dc:creator>markybse</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;On a related note, we have a project in development for our internal account managers and tech support team to be able to run any select query they want against our databases to help them resolve issues. The devloper asked me how she could go about making sure they didn't inadvertantly perform some query other than a select. My reply was to create a user account that had all privelages denied except for select and execute the procedure as that user account. Bear in mind that the AM's and tech support will be using a web app that is using an account whose only rights is to execute stored procedures.&lt;/P&gt;&lt;P&gt;This is the real code that was finally used, but is the initial test code I sent to the developer for testing how it would work. Bear in mind that this is SQL 2005 code.&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Use&lt;/FONT&gt;&lt;FONT size=2&gt; master&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Create server login for test&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Create&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Login&lt;/FONT&gt;&lt;FONT size=2&gt; DBDataReaderOnly &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;With&lt;/FONT&gt;&lt;FONT size=2&gt; Password &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'PeasPorridgeCold'&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Use&lt;/FONT&gt;&lt;FONT size=2&gt; DemoDatabase&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Create database user for login&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Create&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;User&lt;/FONT&gt;&lt;FONT size=2&gt; DBDataReader &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;For&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Login&lt;/FONT&gt;&lt;FONT size=2&gt; DBDataReaderOnly &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;With&lt;/FONT&gt;&lt;FONT size=2&gt; Default_Schema &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Create databse role for easy assignment of permissions&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Create&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Role&lt;/FONT&gt;&lt;FONT size=2&gt; db_DataReaderOnly&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Grant select rights&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Grant&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Select&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;To&lt;/FONT&gt;&lt;FONT size=2&gt; db_DataReaderOnly&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Deny all other permissions&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Deny&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Execute&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Insert&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Update&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Delete&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;References&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Alter&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; Take Ownership&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;View&lt;/FONT&gt;&lt;FONT size=2&gt; Definition &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;To&lt;/FONT&gt;&lt;FONT size=2&gt; db_DataReaderOnly&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Assign user to the database role&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Exec&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;sp_addrolemember&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'db_DataReaderOnly'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'DBDataReader'&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;Go&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Create a test procedure&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Create &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Procedure&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;USP_DynamicDataTest&lt;/P&gt;&lt;P&gt;@SQL &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;nvarchar&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;max&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;With&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Execute&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;As&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'DBDataReader'&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;As&lt;/P&gt;&lt;P&gt;Exec&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;sp_executesql&lt;/FONT&gt;&lt;FONT size=2&gt; @SQL&lt;/P&gt;&lt;P&gt;Go&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;P&gt;-- Test new account, run each command individually&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Exec&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;USP_DynamicDataTest @SQL &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'Select * From TestTable with(nolock)'&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Exec&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;USP_DynamicDataTest @SQL &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'Delete Top (1) From TestTable'&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Exec&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;USP_DynamicDataTest @SQL &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'Drop Table TestTable'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#ff0000 size=2&gt; &lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Mon, 18 Dec 2006 19:37:00 GMT</pubDate><dc:creator>Robert Davis</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;The "best" solution is not to use dynamic sql unless there is absolutely no other way to accomplish the desired result.&lt;/P&gt;&lt;P&gt;Dynamic sql is a total pain to maintain. I've slogged through other people's dynamic code and many times found a better way.&lt;/P&gt;&lt;P&gt;If you &lt;STRONG&gt;must &lt;/STRONG&gt;write dynamic sql, you should embed a sample of the resulting sql statement in the sp, to help illustrate what's going on. A very simple example of that would be:&lt;/P&gt;&lt;P&gt;&lt;FONT color=#117711&gt;-- use the next invoice # as the seed, might look like:-- ALTER TABLE tblFM_MPI ADD InvoiceNumber int IDENTITY (45969, 1)&lt;/FONT&gt;SET @sql = N'ALTER TABLE tblFM_MPI DROP COLUMN InvoiceNumber'EXEC ( @sql )SET @sql = N'ALTER TABLE tblFM_MPI ADD InvoiceNumber int IDENTITY (' + CAST(@NextInv AS varchar(6)) + ', 1)'EXEC  ( @sql )&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Mon, 18 Dec 2006 18:34:00 GMT</pubDate><dc:creator>WILLIAM MITCHELL</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;&amp;gt;&amp;gt; A complex statement is difficult to read when it is pieced together with a bunch of CHAR, CAST, and CONVERT fuctions.&lt;/P&gt;&lt;P&gt;I disagree. I think it is easier to read considering I don't have to go to another part of the procedure and find the Replace statements to figure out what someone is trying to do with a particular portion of the dynamic statement.&lt;/P&gt;&lt;P&gt;Of course, the best solution to the nvarchar 4000 character limit is to use nvarchar(max) in SQL Server 2005.&lt;/P&gt;</description><pubDate>Mon, 18 Dec 2006 13:23:00 GMT</pubDate><dc:creator>Robert Davis</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;NOLOCK aka READ UNCOMITTED aka "dirty read"&lt;/P&gt;&lt;P&gt;Allows reading a table in spite of any uncommitted transactions that may be in progress on other connections.&lt;/P&gt;&lt;P&gt;Pro: It saves time by not creating locks, and by not waiting for or blocking other transactions.  It only requires a schema lock on the table, rather than creating locks for all the rows/pages/extents that are read.  It ignores exclusive locks owned by other connections instead of waiting for those transactions to complete.&lt;/P&gt;&lt;P&gt;Cons: You can read data from partially completed transactions, then those transactions may be rolled back and the data disappears.  For instance, you have a query WITH(NOLOCK) to sum the sales for each salesman to calculate commission payments.  Just before you run this, someone begins a transaction showing they sold the Brooklyn Bridge for $2 billion.  After your comission query runs and calculates a huge bonus for this guy, the transaction is rolled back and there is no trace of the data to explain what happened to the accountants.&lt;/P&gt;&lt;P&gt;It is very useful for reporting queries on production databases, if you know that either the data you're reading is static or you don't mind having counts and totals being a little off.  A history table that sees INSERTs but no UPDATEs for example, or maybe a query summarizing last week's data when only today's data might be volatile.  An inventory query that is checking whether there are less than 5000 #8 wood screws on hand to decide when to reorder is not going to notice the difference between 3875 and 3750.&lt;/P&gt;&lt;P&gt;It obviously is not recommended for accounting, or any application where exact numbers are required.  An inventory query that is a little fuzzy about whether there should be 2 or 3 Ferraris on the showroom floor is a bad idea.&lt;/P&gt;</description><pubDate>Thu, 22 Dec 2005 09:29:00 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;will someone please help and o'll wondering soul out by giving me a human version of (NOLOCK)??&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;THANKS!&lt;/P&gt;&lt;P&gt;ERIK&lt;/P&gt;</description><pubDate>Wed, 21 Dec 2005 16:30:00 GMT</pubDate><dc:creator>Mr Construction</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;will someone please help and o'll wondering soul out by giving me a human version of (NOLOCK)??&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;THANKS!&lt;/P&gt;&lt;P&gt;ERIK&lt;/P&gt;</description><pubDate>Wed, 21 Dec 2005 16:30:00 GMT</pubDate><dc:creator>Mr Construction</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;&lt;FONT face=Arial size=3&gt;One more time, SQL Server is happy to let you use multi-line strings with embedded tabs.  The end-of-line is just white space to the SQL syntax analyzer, you don't have to close the string on each line and start the next with "+".&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face=Arial size=3&gt;Instead of using all that concatenation, just use:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;set&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; @query &lt;FONT color=#808080&gt;=&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; &lt;/FONT&gt;&lt;FONT face="Courier New" color=#ff0000&gt;'select column1,     column2from table1 (nolock)'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#ff0000&gt;&lt;FONT face=Arial color=#111111 size=3&gt;I can't enter a tab in this text box so there are four spaces before 'column2' instead of a tab, but you can use the tab character in Query Analyzer.&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000&gt;&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Wed, 21 Dec 2005 16:23:00 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;Thanks for the article. I am currently re-writing a chunk of my code because, in this case, it's going to give me a huge performance gain. The format I have settled on is:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;DECLARE&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;@query &lt;SPAN style="COLOR: blue"&gt;nvarchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;4000&lt;SPAN style="COLOR: gray"&gt;), &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- // nvarchar using sp_executesql&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;@LN &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;),&lt;/SPAN&gt; &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;@TB &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;1&lt;SPAN style="COLOR: gray"&gt;),&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;@LT &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;2&lt;SPAN style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; @LN &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;10&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 4"&gt;                  &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- // Line Feed&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; @TB &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;9&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 4"&gt;                   &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- // Tab&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; @LT &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;10&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;(&lt;/SPAN&gt;9&lt;SPAN style="COLOR: gray"&gt;)&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;  &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;      &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;-- // Line feed + Tab&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; @query &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: red"&gt;''&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; @query &lt;SPAN style="COLOR: gray"&gt;=&lt;/SPAN&gt; @query&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: red"&gt;'select column1,'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; @LT&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: red"&gt;'column2'&lt;/SPAN&gt; &lt;SPAN style="COLOR: gray"&gt;+&lt;/SPAN&gt; @LN&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;+&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: red"&gt;'from table1 (nolock)'&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt; &lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;It is a little neater than having char() in lots of places.&lt;/P&gt;</description><pubDate>Wed, 21 Dec 2005 07:05:00 GMT</pubDate><dc:creator>Stuart Anderson-198975</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;I really enjoyed the article.&lt;/FONT&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt; &lt;/FONT&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;There is&lt;SPAN style="mso-spacerun: yes"&gt;  &lt;/SPAN&gt;huge benefit for dynamic stored procedures in when vb.net and sql2000 work togeather;.&lt;/FONT&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;/FONT&gt; &lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;thanks!&lt;/FONT&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;erik&lt;/FONT&gt;&lt;/P&gt;</description><pubDate>Tue, 20 Dec 2005 12:29:00 GMT</pubDate><dc:creator>Mr Construction</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;Presumably you're getting the index info from sysindexes?  Join to sysobjects to get the owner.  It might look something like this:&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;select&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'DBCC DBREINDEX ('''&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;+&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;quotename&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;user_name&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;so&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;uid&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)) +&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'.'&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; &lt;FONT color=#808080&gt;      &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;+&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;quotename&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;so&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;name&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;+&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;''','''&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt; +&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;rtrim&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;name&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;+&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#ff0000 size=2&gt;'''[, fillfactor]'&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;from&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;sysindexes&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;inner&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;join&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;sysobjects&lt;/FONT&gt;&lt;FONT size=2&gt; so &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;on&lt;/FONT&gt;&lt;FONT size=2&gt; so&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;id &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;id&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;where&lt;/FONT&gt;&lt;FONT size=2&gt; xtype &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#ff0000 size=2&gt;'U'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;      and&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;indexproperty&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;id&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;name&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'IsStatistics'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)=&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;0&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;      and&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff00ff size=2&gt;indexproperty&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;id&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; si&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;name&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'IsHypothetical'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)=&lt;/FONT&gt;&lt;FONT size=2&gt;0&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;</description><pubDate>Mon, 19 Dec 2005 15:38:00 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;In a tangential topic, I've built a process using dynamic sql which reads through all the databases on my servers to determine which indexes need reorganizing.  Works great on all our servers, except for the ones with outside apps, where there the table owners are not dbo.  I'm attempting to adapt the process to append the database owner name to the tablename -- building a variable ahead of time -- however I get a compile error whenever I have a variable in the "from" clause.&lt;/P&gt;&lt;P&gt;Is there a proper way to handle this that I'm missing?&lt;/P&gt;</description><pubDate>Mon, 19 Dec 2005 12:51:00 GMT</pubDate><dc:creator>Matt Klein-228552</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;I have learned to detest code that builds dynamic SQL by repeated concatenation, especially with char functions for line breaks and tabs.  A complex statement is difficult to read when it is pieced together with a bunch of CHAR, CAST, and CONVERT fuctions.&lt;/P&gt;&lt;P&gt;SQL is perfectly happy to interpret multiline strings with all line breaks and tabs included.  I create a template of the complete SQL statement with tags for all variable parts, then use REPLACE functions to handle the modifications.  This is especially useful when one variation requires changes in the field list, tables, and where clause.  Another advantage of REPLACE is it can do implicit conversions to string of integers and avoid '...' + CAST(x AS VARCHAR) + '...'.&lt;/P&gt;&lt;P&gt;-- Basic templateSET @sql = 'SELECT fld1, fld2&amp;lt;fields&amp;gt;      FROM tbl1      &amp;lt;joins&amp;gt;      WHERE tbl1.xyz=0 &amp;lt;filters&amp;gt;'&lt;/P&gt;&lt;P&gt;-- One logical test controls additional fields, joins, and filter clauses-- All replaceable tokens are duplicated in the replacement strings to allow further actionsIF modification1 = 1       SET @sql = REPLACE(REPLACE(REPLACE(@sql,      '&amp;lt;fields&amp;gt;', ', fld3, fld4&amp;lt;fields&amp;gt;'),      '&amp;lt;joins&amp;gt;', 'INNER JOIN tbl2 ON tbl1.fld1 = tbl2.fld1      &amp;lt;joins&amp;gt;'),      '&amp;lt;filters&amp;gt;', '            AND tbl2.pqr IS NOT NULL &amp;lt;filters&amp;gt;')-- Further modifications using the same logic-- Strip out any remaining tokens      SET @sql = REPLACE(REPLACE(REPLACE(@sql,      '&amp;lt;fields&amp;gt;', ''),      '&amp;lt;joins&amp;gt;', ''),      '&amp;lt;filters&amp;gt;', '')&lt;/P&gt;</description><pubDate>Mon, 19 Dec 2005 10:00:00 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>all, you can concat in sp_executesql so you can go beyond the 4k limit...</description><pubDate>Sat, 17 Dec 2005 17:38:00 GMT</pubDate><dc:creator>pkmccarthy</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>FYI, see the article "The Curse and Blessings of Dynamic SQL" by Erland Sommarskog, SQL Server MVP at http://www.sommarskog.se/dynamic_sql.htmlTo workaround the the size limitation, one approach is to use views. As the dynamic SQL uses the view, it is often simplier, easier to debug and is often shorter than the 4000 character length restriction of sp_executesql</description><pubDate>Fri, 16 Dec 2005 17:31:00 GMT</pubDate><dc:creator>Carl Federl</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>I like the formatting hints in your article.  I would have saved some trial and error time if I'd read this a few years ago.Personally, I've used dynamic SQL everywhere, due to the unique requirements of supporting a vendors db (each client in it's own self similar database).Dynamic SQL that gets concatenated and executed at run time always proves to be hard to debug (and tends to have bugs), and testing all possible permutations is hard to accomplish.  It's especially hard to figure out where data issues stopped the process.I've been doing something similar to JIT (Just in time compile), by dynamically creating permanent stored procedures, then executing those procedures the next time the same query is run.  It becomes a little more difficult when dealing with a large number of parameters, but as long as you keep data and metadata separate you are golden (IE: one procedure for searching with first and last name only, but not one procedure for searching for firstname = calvin and lastname = lawson).Maybe I should submit an article on that.  It answers most of the complaints about dynamic SQL.cl</description><pubDate>Fri, 16 Dec 2005 13:39:00 GMT</pubDate><dc:creator>Calvin Lawson</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>See also:http://jtds.sourceforge.net/apiCursors.html</description><pubDate>Fri, 16 Dec 2005 12:48:00 GMT</pubDate><dc:creator>guenter strubinsky</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Yes, input validation. You trap for the known mechanisms for SQL Injection.  For instance look for single quotes coming in and double 'em. Watch for the --. Most of all, do validation on variable type where possible. For instance, if you know the field is supposed to be an integer, test it. Things of that sort. Basically the same sort of validation tests you code into a web app you could put in UDFs and test the parameters coming in.</description><pubDate>Fri, 16 Dec 2005 09:55:00 GMT</pubDate><dc:creator>K. Brian Kelley</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;Actually, both are part of formatting the output (when printing the SQL) that do not affect the actual executed dynamic SQL.&lt;/P&gt;&lt;P&gt;However, I usually do this a little differently. First of all, I use CRLF and not just LF: CHAR(13) + CHAR(10)&lt;/P&gt;&lt;P&gt;Also, I assign these to a variable early in the procedure to prevent mistakes and from having to type them repeatedly, as shown here:&lt;/P&gt;&lt;PRE&gt;DECLARE @CRLF char(2), @TAB char(1)SET @CRLF = CHAR(13) + CHAR(10)SET @TAB = CHAR(9)&lt;/PRE&gt;&lt;P&gt;Now you not only have a nicer, self-documenting variable, but also, if you mistype the variable, the compiler catches it, but if you mistype the CHAR function values (9, 10, or 13) that won't get caught at compile time.&lt;/P&gt;&lt;P&gt;--Peter&lt;/P&gt;</description><pubDate>Fri, 16 Dec 2005 09:52:00 GMT</pubDate><dc:creator>Peter DeBetta</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Is there any "simple" method how to protect (how to test) the dynamic SP against SQL injection ? We stoped using dynamic SP because we was afraid of this problem.Pavel Lstiburek</description><pubDate>Fri, 16 Dec 2005 09:10:00 GMT</pubDate><dc:creator>Pavel Lstiburek</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Its just a formating thing - char(10) = newline , char(9) = tab, the article mentions using char(10)'s for formatting &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;</description><pubDate>Fri, 16 Dec 2005 08:23:00 GMT</pubDate><dc:creator>Phil.Nicholas</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Excuse my ignorance, but what are those CHAR(10) things doing? How does that work? Please explain.&lt;img src='images/emotions/blink.gif' height='20' width='20' border='0' title='Blink' align='absmiddle'&gt;</description><pubDate>Fri, 16 Dec 2005 08:14:00 GMT</pubDate><dc:creator>Scott D. Smith</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;Heres a stored procedure I wrote sometime ago to generate get, add or update and delete stored procedures so you get the performance benefit of sps. As the naming convention is common you can generate class code etc. against them. I also use an sp to generate c# or vb class code to interface with the stored procedures. Doing data access in this way is very quick and tends to be bug free first time which is nice, possibly could be extended with your application to insert code specific to tables so that more table specific logic could be included. nb it also needs a stored procedure to retrieve records based on foreign keys.&lt;/P&gt;&lt;P&gt;Phil Nicholas&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;if exists(select * from sysobjects where name = 'sp_createStoredProcedures')drop procedure sp_createStoredProceduresGOcreate procedure sp_createStoredProcedures(@tablename varchar(255), @Encryption bit = 1)AS&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;DECLARE @SQL varchar(8000)&lt;/P&gt;&lt;P&gt;---------------------------select sp  SET NOCOUNT ON&lt;/P&gt;&lt;P&gt; create table #temp(id int not null identity (1,1), txt varchar(8000))&lt;/P&gt;&lt;P&gt; insert #temp (txt) select '--stored procedure to get an individual ' + @tablename + '  record' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'if exists(select * from sysobjects where name = ''spGet_' + @tablename + ''')'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'drop procedure spGet_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'create procedure spGet_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + &lt;A href="mailto:'@'"&gt;'@'&lt;/A&gt; + replace(c.name,' ', '_') + ' ' + t.name + ',' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-1)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; if @Encryption = 1  insert #temp (txt)  select 'WITH ENCRYPTION'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'AS' &lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'SELECT '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + '[' + c.name + '],' from sysobjects o join syscolumns c on o.id = c.id where  o.name = @tablename order by colid asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-1)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'FROM '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select CHAR(9) + @tablename&lt;/P&gt;&lt;P&gt; insert #temp  (txt) select 'where '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + '[' + c.name + '] = @' + replace(c.name,' ', '_') + ' and' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-4)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; insert #temp (txt) values ('')&lt;/P&gt;&lt;P&gt;---------------------------delete sp  insert #temp (txt) select '--stored procedure to delete an individual ' + @tablename + '  record' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'if exists(select * from sysobjects where name = ''spDelete_' + @tablename + ''')'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'drop procedure spDelete_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'create procedure spDelete_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + &lt;A href="mailto:'@'"&gt;'@'&lt;/A&gt; + replace(c.name,' ', '_') + ' ' + t.name + ',' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-1)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; if @Encryption = 1  insert #temp (txt)  select 'WITH ENCRYPTION'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'AS' &lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'DELETE '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'FROM '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select CHAR(9) + @tablename&lt;/P&gt;&lt;P&gt; insert #temp  (txt) select 'WHERE '&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + '[' + c.name + '] = @' + replace(c.name,' ', '_') + ' AND' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-4)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; insert #temp (txt) values ('')&lt;/P&gt;&lt;P&gt;--sp_help tblpupil_key_indicators---------------------------update/insert sp  insert #temp (txt) select '--stored procedure to insert/add an individual ' + @tablename + '  record' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'if exists(select * from sysobjects where name = ''spAddUpdate_' + @tablename + ''')'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'drop procedure spAddUpdate_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'create procedure spAddUpdate_' + @tablename&lt;/P&gt;&lt;P&gt; insert #temp (txt) select char(9) + &lt;A href="mailto:'@'"&gt;'@'&lt;/A&gt; + replace(c.name,' ', '_') + ' ' + t.name +   case     when t.name in ('varchar', 'nvarchar', 'char', 'nchar') then ' (' + cast(c.length as varchar(20)) + ')'   when t.name in ('decimal') then  ' (' + cast(c.prec as varchar(20)) + ','  + cast(c.scale as varchar(20)) + ')'   else '' end + ',' from  sysobjects o join syscolumns c on c.id = o.id join systypes t on c.xtype = t.xtype where o.name = @tablename and NOT c.name LIKE '%_MOD_USER' and NOT c.name like '%_MOD_DATE' order by c.colid asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-1)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; if @Encryption = 1  insert #temp (txt)  select 'WITH ENCRYPTION'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'AS' &lt;/P&gt;&lt;P&gt; --empty sql string set @SQL = ''&lt;/P&gt;&lt;P&gt;--------do check for keys&lt;/P&gt;&lt;P&gt;--change to autoval key or primary keyif exists(select * from sysobjects o join syscolumns c on c.id = o.id where not autoval is null and o.name = @tablename) select @SQL = @SQL + 'coalesce(@' + replace(c.name,' ', '_') + ',0) = 0 and ' from  sysobjects o join syscolumns c on c.id = o.id where not c.autoval is null and o.name = @tablenameelse select @SQL = @SQL + 'coalesce(@' + replace(c.name,' ', '_') + ',0) = 0 and ' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0 begin  set @SQL = 'IF ' + substring(@SQL,1,len(@SQL)-4)&lt;/P&gt;&lt;P&gt;  insert #temp (txt)  select @SQL end&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'BEGIN'&lt;/P&gt;&lt;P&gt; --empty sql string set @SQL = ''&lt;/P&gt;&lt;P&gt; --do insert col list select @SQL = @SQL + '[' + c.name + '],' from  sysobjects o join syscolumns c on c.id = o.id join systypes t on c.xtype = t.xtype where o.name = @tablename and NOT c.name LIKE '%_MOD_USER' and NOT c.name like '%_MOD_DATE' AND COALESCE(AUTOVAL,0)=0 order by c.colid asc&lt;/P&gt;&lt;P&gt; --insert param list if @SQL&amp;lt;&amp;gt;'' INSERT #TEMP SELECT char(9) + 'INSERT ' + @TableName + '(' + substring(@SQL,1, len(@SQL)-1) + ')'&lt;/P&gt;&lt;P&gt; --empty sql string set @SQL = ''&lt;/P&gt;&lt;P&gt; --do insert col list select @SQL = @SQL + &lt;A href="mailto:'@'"&gt;'@'&lt;/A&gt; + replace(c.name,' ', '_') + ',' from  sysobjects o join syscolumns c on c.id = o.id join systypes t on c.xtype = t.xtype where o.name = @TableName and NOT c.name LIKE '%_MOD_USER' and NOT c.name like '%_MOD_DATE' AND COALESCE(AUTOVAL,0)=0 order by c.colid asc&lt;/P&gt;&lt;P&gt; --insert param list if @SQL&amp;lt;&amp;gt;'' INSERT #TEMP (txt) SELECT char(9) + 'VALUES (' + substring(@SQL,1, len(@SQL)-1) + ')'&lt;/P&gt;&lt;P&gt; if EXISTS( select * from  sysobjects o join syscolumns c on c.id = o.id  where o.name = @tablename AND COALESCE(AUTOVAL,0)&amp;gt;=1) insert #temp select 'SELECT @@IDENTITY'&lt;/P&gt;&lt;P&gt;  insert #temp (txt) select 'END'&lt;/P&gt;&lt;P&gt; INSERT #TEMP (txt) SELECT 'ELSE'&lt;/P&gt;&lt;P&gt;  INSERT #TEMP (txt) SELECT 'UPDATE ' + @tablename + ' SET ' &lt;/P&gt;&lt;P&gt; INSERT #TEMP (txt) select char(9) + '[' + c.name + '] = @' + replace(c.name,' ', '_') + ',' from  sysobjects o join syscolumns c on c.id = o.id where o.name = @TableName and NOT c.name LIKE '%_MOD_USER' and NOT c.name like '%_MOD_DATE' and not exists (select * from sysindexes i join sysindexkeys ik on ik.id = i.id  and ik.indid = i.indid where  (i.status &amp;amp; 2048) = 2048 and ik.colid = c.colid and i.id = o.id) and c.autoval is null order by c.colid asc--select * from sysindexkeys if @@rowcount &amp;gt; 0  if @SQL&amp;lt;&amp;gt;''   update #temp   set txt = substring(txt,1,len(txt)-1)   where [id] = @@identity&lt;/P&gt;&lt;P&gt; INSERT #TEMP (txt) SELECT 'WHERE'&lt;/P&gt;&lt;P&gt; insert #temp (txt) select  char(9) + '[' + c.name + '] = coalesce(@' + replace(c.name,' ', '_') + ',0) and ' from sysindexes i join sysobjects o on o.ID = i.id  join sysindexkeys ik on ik.indid = i.indid  and ik.id = i.id join syscolumns c on c.id = ik.id  and ik.colid = c.colid join systypes t on c.xtype = t.xtype where o.name = @tablename AND (I.STATUS &amp;amp; 2048) = 2048 order by ik.keyno asc&lt;/P&gt;&lt;P&gt; if @@rowcount &amp;gt; 0  update #temp  set txt = substring(txt,1,len(txt)-4)  where [id] = @@identity&lt;/P&gt;&lt;P&gt; insert #temp (txt) select 'GO'&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; insert #temp (txt) values ('')&lt;/P&gt;&lt;P&gt; select txt from #temp order by id asc&lt;/P&gt;&lt;P&gt; drop table #temp&lt;/P&gt;&lt;P&gt; SET NOCOUNT OFFENDGOexec sp_createStoredProcedures 'sysobjects'&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Fri, 16 Dec 2005 08:01:00 GMT</pubDate><dc:creator>Phil.Nicholas</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;I used a lot dynamic stored procedure.I had to, because they aren't really my first choice- I find it hard to read and to debug.&lt;/P&gt;&lt;P&gt;Anyway it was a good article.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Fri, 16 Dec 2005 07:28:00 GMT</pubDate><dc:creator>Daniela-267581</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;I don't see any advantage to using dynamic store procedures. Also, it would be more difficult to tune and troubleshoot dynamic stored procedures if they constantly change.  In addition, data validation should be done at the client side and not on the server side to save yourself a round trip.&lt;/P&gt;</description><pubDate>Fri, 16 Dec 2005 07:09:00 GMT</pubDate><dc:creator>JL Dominguez-246595</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;When printing out the SQL statements, CHAR(10) does indeed make it easier to read. Maintenance on any sproc that uses dynamic sql however, depends on the complexity of the code.&lt;/P&gt;&lt;P&gt;From the examples given, I see no reason to use dynamic sql. There is no top clause, you do not need to dynamically determine a table name. If your production stored procedurees are setup like this, I would redesign them to take advantage of the sql engine.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Fri, 16 Dec 2005 05:51:00 GMT</pubDate><dc:creator>cliffb</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>As someone who's had to maintain code embedded with char(10)s everywhere I can honestly say that the advice in the first section of this article is absolute tripe. It does NOT make it easier to read, nor does it make it easier to maintain.</description><pubDate>Fri, 16 Dec 2005 03:37:00 GMT</pubDate><dc:creator>Oli Wilkinson</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>&lt;P&gt;1. There is a redundant condition check in your example.  Once we start building the WHERE clause, we've already passed the first check, which would have prevented our getting down to the second check.&lt;/P&gt;&lt;PRE&gt;IF @LastName &amp;lt;&amp;gt; '' AND @NameID &amp;lt;&amp;gt; 0   ...   RETURNEND...--Begin building WHERE clauseIF @LastName &amp;lt;&amp;gt; '' OR @NameID &amp;lt;&amp;gt; 0...&lt;/PRE&gt;&lt;P&gt;2. As has already been mentioned, performance of a dynamic SP will be poorer than a static SP.  It would make more sense to generate the SP externally and then import them in a SQL stream, or to hook directly into SQL Server and do it programmatically.  There's been a lot of work done in the realm of code generation (heck, entire system generation) that could serve as a model for this.  As with most things, we trade performance for flexibilty and vice versa.&lt;/P&gt;&lt;P&gt;3. It should be noted that SQLServer2005 removes the 8000 character limit.  This alone should be worth the migration.&lt;/P&gt;&lt;P&gt;4. How does the following statement provide SQL injection attack protection?SET @LastName = REPLACE (@LastName, '''', '''''')&lt;/P&gt;</description><pubDate>Thu, 15 Dec 2005 22:48:00 GMT</pubDate><dc:creator>mark hutchinson</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Unless you manage to get an execution plan that can be reused (and usually you don't) then you can say there is no performance benifit.For me, the benefit is that no changes have to be made to the web site or internal applications for most changes in the stored procedure.  Particularly useful when I find a quicker way to get the same result set.You could realize a slight performance benifit because you would not have to send thousands of characters from the client to the server.  With the dynamic SP you simply send the call and the code is built server side and then immediately executed.Robert W. MardaSQL Programmerbigdough.comThe world’s leading capital markets contact database and software platform.</description><pubDate>Fri, 25 Apr 2003 10:30:00 GMT</pubDate><dc:creator>Robert W Marda</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Is there a significant performance benefit to building dynamic SQL in a stored procedure instead of from within code?</description><pubDate>Thu, 24 Apr 2003 09:41:00 GMT</pubDate><dc:creator>kevkaz</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>One more difference between EXEC and sp_executesql is that first one does not allow you to run parameterized queries.sp_executesql allows you to parameterize the sql and inturn helps in caching and re-use of execution plans.The issue related to dynamic sqls, parameterized sqls and memory is discussed in following linkhttp://www.sqlservercentral.com/forum/link.asp?TOPIC_ID=11407 </description><pubDate>Thu, 24 Apr 2003 09:38:00 GMT</pubDate><dc:creator>Rajesh Patavardhan</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>I don't think we currently use sp_executeSQL and I have used it only a few times.  So I am not an expert with its use.  We have many dynamic stored procedures in production that have more than 4,000 characters in the query built and some even exceed 8,000 characters.  So being limited to 4,000 is not an option.  I tried to use the same technique I showed in example 2 of my article but couldn't get it to work so it appears there is no way to extend the max beyond 4,000 characters, where as with the EXEC command I don't know of a limit to how many varchar(8000) variables you can add together at the time you execute the code and so your code is theoretically not limited.  The trade off is that you lose benefit of most if not all caching done by SQL Server.  For us this hasn't been a problem since most of what we have in production completes within a few seconds of execution with or without caching.  It will depend on your individual situation if you can accept this type of performance or not.Robert W. MardaSQL Programmerbigdough.comThe world’s leading capital markets contact database and software platform.</description><pubDate>Thu, 24 Apr 2003 08:39:00 GMT</pubDate><dc:creator>Robert W Marda</dc:creator></item><item><title>RE: How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Just a quick note: when building dynamic SQL in a sproc, SQL Server will not usually cache the query plan unless you use one of the built-in stored procedures to execute the code.  So, instead of simply writing this:exec (@strSQL)It is actually better over the long run to write this:exec sp_executeSQL @strSQLThe one drawback here is that the sproc, sp_executeSQL, expects its parameter as nVarchar, so you will be limited to 4000 characters instead of 8000.  See more than you need to know in BOL.Thanks,DH. </description><pubDate>Thu, 24 Apr 2003 08:14:00 GMT</pubDate><dc:creator>bumbler</dc:creator></item><item><title>How to Build Dynamic Stored Procedures</title><link>http://www.sqlservercentral.com/Forums/Topic11502-76-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/rmarda/howtobuilddynamicstoredprocedures.asp&gt;http://www.sqlservercentral.com/columnists/rmarda/howtobuilddynamicstoredprocedures.asp&lt;/A&gt;</description><pubDate>Fri, 18 Apr 2003 00:00:00 GMT</pubDate><dc:creator>Robert W Marda</dc:creator></item></channel></rss>