﻿<?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 Deepthi Viswanathan Nair / Article Discussions / Article Discussions by Author  / Dynamic creation of  Insert, Update, Delete Stored procedure / 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>Wed, 19 Jun 2013 17:16:29 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I just want to thank you very much for relieving HOURS of tedium! Even if I have to tweak it a bit (for example, on my updates I add ISNULL(@NewValue,[OldValue]) to the statements so I can do a partial update if I like) I will benefit greatly. I was growing old copying and pasting and typing just to get the procs inplace so I could get to the coding, which is using Entity framework. This really needed doing.:-)Lisa Z. Morgan</description><pubDate>Fri, 03 Jun 2011 13:08:06 GMT</pubDate><dc:creator>Joseph M. Morgan</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Hi,For download and information on the code generating software for Sql Server 2005/2008, you can now visit [url=http://www.raisintech.com/SSCodeGen/tabid/87/Default.aspx]http://www.raisintech.com/SSCodeGen/tabid/87/Default.aspx[/url]. The link for the version to work with Sql Server 7 and 2000 is at this URL as well.Regards</description><pubDate>Mon, 26 Jul 2010 03:49:11 GMT</pubDate><dc:creator>anthonyh</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>[quote][b]Eric_AZ (7/14/2010)[/b][hr]This is AWESOME! Thank you![/quote]Hi,Just wondering if you mean the SSCodeGen software to generate code, from the forum link before your reply, or this article for dynamic sql?</description><pubDate>Sat, 24 Jul 2010 09:46:13 GMT</pubDate><dc:creator>anthonyh</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>to check your decimal(p,s) you can do this :[code="sql"] Case	When  systypes.xusertype =106 Then '(' + Convert(varchar(10),syscolumns.xprec) +','+Convert(varchar(10),syscolumns.xscale)+')' [/code]</description><pubDate>Thu, 15 Jul 2010 07:10:28 GMT</pubDate><dc:creator>R-CHERIF Ammar</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>This is AWESOME! Thank you!</description><pubDate>Wed, 14 Jul 2010 12:36:37 GMT</pubDate><dc:creator>Eric_AZ</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>You could try this forum link:[url=http://www.sqlservercentral.com/Forums/Topic892846-391-1.aspx#bm901179]http://www.sqlservercentral.com/Forums/Topic892846-391-1.aspx#bm901179[/url]Regards</description><pubDate>Sun, 11 Jul 2010 12:22:55 GMT</pubDate><dc:creator>anthonyh</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Hi Great effort in getting out this procedure but what about decimal(p,s) that doesn't seem to reflect either.</description><pubDate>Sun, 11 Jul 2010 11:00:56 GMT</pubDate><dc:creator>krishnavamshi24</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>The other thing that is missing is any code to handle decimal and numeric datatypes with Precision and Scale.</description><pubDate>Sat, 10 Jul 2010 14:07:37 GMT</pubDate><dc:creator>Andy Reilly</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Should look into using n-hibernate tool such as CSLA, LLBLGen Pro, etc...  The CRUD procedures should reside in the DataAccessLayer.dll.</description><pubDate>Fri, 09 Jul 2010 11:10:05 GMT</pubDate><dc:creator>James Tran</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I understand why people write these utilities, but Microsoft changes system tables and relations between objects with every release.  Utilities like this can become quickly out of date, and if deployed into some sort of production automation could cause problems on an upgrade.</description><pubDate>Fri, 09 Jul 2010 10:54:14 GMT</pubDate><dc:creator>steve.simpson</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I appreciate the thought and effort, but this is technology from 8+ yrs ago.. use an ORM and focus on value-added customer functionality</description><pubDate>Fri, 09 Jul 2010 08:31:26 GMT</pubDate><dc:creator>dg-453058</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Yall should check out MyGeneration.  They have a huge repository of scripts to generate anything for any language.http://www.mygenerationsoftware.com/templatelibrary/default.aspx</description><pubDate>Fri, 09 Jul 2010 08:24:34 GMT</pubDate><dc:creator>mike.mahon</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Very ingenious, but honestly, the proliferation of procs isn't a good thing.  This is better handled as others have said in client code where one class can dynamically generate all the needed code at run time. This avoids all types of issues and allows you to keep all the code in the same place - business logic, data validation, etc.  Still, it is very ingenious, I just think less effort would be involved in the long run by keeping such code out of the database.</description><pubDate>Fri, 09 Jul 2010 07:27:34 GMT</pubDate><dc:creator>jcraddock</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>[quote][b]Deepthi Viswanathan Nair (7/22/2009)[/b][hr]Comments posted to this topic are about the item [B]&amp;lt;A HREF="/articles/Stored+Procedures/67055/"&amp;gt;Dynamic creation of  Insert, Update, Delete Stored procedure&amp;lt;/A&amp;gt;[/B][/quote]I found an error when column type is NVARCHAR.ex. nvarchar(255)     ----&amp;gt; used your store procedure the datatype become nvarchar(510)</description><pubDate>Fri, 09 Jul 2010 04:04:49 GMT</pubDate><dc:creator>firmwu</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Thank u.it's very very useful &amp; time saving for the developers.</description><pubDate>Fri, 09 Jul 2010 02:14:25 GMT</pubDate><dc:creator>sukhendass</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I use CodeSmith templates for this task, and much more. You can find my article about [url=http://mkdot.net/blogs/boban/archive/2009/11/09/codesmith-a-tool-for-every-developer.aspx]here[/url]. In the [url=http://mkdot.net/community/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.01.04.56/CodeSmith_2D00_Boban_2D00_Templates.zip]zip [/url]file beside template for procedures, there is also template for data layer class for table and bussiness object class.</description><pubDate>Fri, 09 Jul 2010 01:17:34 GMT</pubDate><dc:creator>Boban Stojanovski-455883</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I use the T4 template processor provided freely in VisualSudio for this. It is much more flexible and easier to read. Also, I can point it to a whole database of tables or a set of tables and generate a whole wad of stored procs. Here is an article about it: [url]http://www.olegsych.com/2008/01/how-to-use-t4-to-generate-crud-stored-procedures [/url]</description><pubDate>Thu, 08 Jul 2010 22:32:06 GMT</pubDate><dc:creator>midgard</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>It fails to create the create or insert procs. The delete proc is successfully created.</description><pubDate>Tue, 23 Mar 2010 06:17:15 GMT</pubDate><dc:creator>brosspaxedi</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Can you post the SP which is created and giving error?</description><pubDate>Mon, 22 Mar 2010 23:42:41 GMT</pubDate><dc:creator>SAMARDEEP</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I only modified this to use the prefix "PEC_" rather than sp_, and my table is PEC_edi_834The proc gets created with no error, but when I try to run the proc, I get:Msg 156, Level 15, State 1, Procedure PEC_pec_edi_834_insert, Line 1Incorrect syntax near the keyword 'BEGIN'.Msg 137, Level 15, State 2, Procedure PEC_pec_edi_834_insert, Line 1Must declare the scalar variable "@Payer_ID_Code".Msg 137, Level 15, State 2, Procedure PEC_pec_edi_834_update, Line 1Must declare the scalar variable "@Payer_ID_Code".Procedure [dbo].[PEC_pec_edi_834_delete] Created Successfully Any ideas?Thanks.</description><pubDate>Mon, 22 Mar 2010 11:32:24 GMT</pubDate><dc:creator>brosspaxedi</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Hi,See &lt;a style="text-decoration:underline;" href="http://www.sqlservercentral.com/FindForumThread/67630 "&gt;updated script&lt;/a&gt;for this and many other modifications..in this script..Regards.</description><pubDate>Thu, 27 Aug 2009 23:11:26 GMT</pubDate><dc:creator>SAMARDEEP</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Need to do replace('(' + Convert(varchar(10),syscolumns.length) +')','(-1)','(MAX)') to handle the varchar(max) datatype, wich shows as (-1) and screws the code...</description><pubDate>Thu, 27 Aug 2009 13:26:50 GMT</pubDate><dc:creator>blopezya</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I never think about that.. it's a nice catch.. i would incorporate this ans update the sp.. thanks for ur comment..</description><pubDate>Tue, 28 Jul 2009 22:43:52 GMT</pubDate><dc:creator>Deepthi Viswanathan Nair</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>There's also an issue when column names contain spaces, the stored procedure parameter names will also have the space in them and causes a syntax error. Eg. Where a table has a column named 'Product Name' the following is generated:[code="sql"]CREATE PROCEDURE sp_something_insert   @Product Name varchar(50)AS...[/code]I realise it's not good practice to use spaces in column names but somone will.Other odd characters are also potential hazards here.</description><pubDate>Tue, 28 Jul 2009 04:41:34 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I work a system with hundreds of tables and we did have two stored procedures for each that were dynamically generated using our code generator.  We've now moved away from this since the code foot print was huge and simple insert, update and delete statements could be better achieved in our c# code.We have a code generator that creates a c# class (in our data layer) for evert table and then calling methods (that were dynamically generated based on table indexes and columns etc) this dynamically generates the sql and executes using sp_executesql.  This change in how we execute these statements has been really well received, leaving the stored procedures for more complex tasks, reducing the vast no. of sp's we had and making it far more manageable.Nice article though :)</description><pubDate>Fri, 24 Jul 2009 01:30:58 GMT</pubDate><dc:creator>dungimon</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Nice effort, and thanks for sharing it across.</description><pubDate>Fri, 24 Jul 2009 00:51:17 GMT</pubDate><dc:creator>Sudarsan Srinivasan</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>thanks for ur comment.. i would change it..regardsdeepthi</description><pubDate>Thu, 23 Jul 2009 21:35:17 GMT</pubDate><dc:creator>Deepthi Viswanathan Nair</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>[quote][b]ddunn (7/23/2009)[/b][hr]I use SQL Admin Studio as it has a customizable template engine (I used it to write Delphi code to access stored procedures).  It adds the following code to the top of the generated scripts so you don't have to worry about whether the stored procedure exists or not.  Note that it creates the script in an editor so you can work with it before executing it.[code]IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[MyStoredProcedure]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)DROP PROCEDURE [dbo].[MyStoredProcedure]GO[/code][/quote]You may even want to try something along these lines for the script.  It avoids the drop which can cause auditing and/or permission issues.  This method basically lets it run every time whether it exists or not.:[code]IF OBJECT_ID(N'[dbo].[Proc_Name]') IS NULL BEGIN    EXEC ('CREATE PROCEDURE           [dbo].[Proc_Name]           AS BEGIN SELECT ''STUB'' END');END;GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[Proc_Name]  ASrest of code....[/code]Also as said before the procs should NOT be named with an "sp_".  Will cause a performance hit.</description><pubDate>Thu, 23 Jul 2009 15:15:42 GMT</pubDate><dc:creator>Adam Gojdas</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>The current version of the script needs an identity column in the table.</description><pubDate>Thu, 23 Jul 2009 15:09:38 GMT</pubDate><dc:creator>Mark-527630</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Hi there,I tried to execute your stored procedure, no error, but didn't create stored procedures for Insert, update, and delete. From your first version of your stored procedure, I got an error for insert and update. The errors said 'Must declare the scalar variable "@VariableName". ' The deleted sp was created sucessfully. Any idea? thanksAK</description><pubDate>Thu, 23 Jul 2009 13:29:10 GMT</pubDate><dc:creator>lexter_jj_lia</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>OrIF OBJECT_ID('dbo.SPName', 'P') IS NOT NULLBEGIN   DROP PROCEDURE [dbo].[SPName]END</description><pubDate>Thu, 23 Jul 2009 12:34:59 GMT</pubDate><dc:creator>Douglas Osborne-456728</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>there are places for improvement, and the code in the article was updated in an earlier comment. I have updated the article to reflect that.I think SELECT is a good thing to add. I have added 2 SELECT procs in my own generation procs before. One to return all values, and one to return a specific value. I've had the need for both those procs in many tables.</description><pubDate>Thu, 23 Jul 2009 09:01:45 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>... or see http://www.ssmstoolspack.com/Main.aspx</description><pubDate>Thu, 23 Jul 2009 08:15:46 GMT</pubDate><dc:creator>david.wright-948385</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>There is an issue with your proc.  You use varchar(1000) and varchar(2000), the problem with this is with large tables this is not large enough.  I changed all of those to varchar(max) and it works fine.One more thing missing is a select.  You have the insert, update and delete, but select is also needed.Ben</description><pubDate>Thu, 23 Jul 2009 08:03:01 GMT</pubDate><dc:creator>bkubicek</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>We have a very similar set of dynamic sproc creation sprocs at my work. I find them useful in a general sense but (1) hard to look at and (2) hard to conditionally customize. I think this is a good example of something that can be implemented with greater clarity in c#. You would want similar metadata queries to get the table information, but I think applying these to a template would look tons better in app code. It could be a clr UDF if you wanted to keep it in the database, but I think the ideal solution would be to query metadata with FOR XML and then pass that XML into a standalone app. I definitely have servers that I'm prohibited from enabling CLR on, but I can obviously still run the FOR XML query.</description><pubDate>Thu, 23 Jul 2009 07:35:02 GMT</pubDate><dc:creator>Adrian Hains</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>truth, it does not work well with any table!this is the message:Procedure [dbo].[sp_iv00103_insert] Created Successfully Msg 8102, Level 16, State 1, Procedure sp_et_create_sps_for_table, Line 189Cannot update identity column 'DEX_ROW_ID'.Procedure [dbo].[sp_iv00103_delete] Created Successfully thanks!</description><pubDate>Thu, 23 Jul 2009 07:21:26 GMT</pubDate><dc:creator>megasoft2</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>Nice article.Wouldn't you rather use MERGE instead?</description><pubDate>Thu, 23 Jul 2009 07:07:48 GMT</pubDate><dc:creator>adi - doccolabs.com</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>I use SQL Admin Studio as it has a customizable template engine (I used it to write Delphi code to access stored procedures).  It adds the following code to the top of the generated scripts so you don't have to worry about whether the stored procedure exists or not.  Note that it creates the script in an editor so you can work with it before executing it.[code]IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[MyStoredProcedure]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)DROP PROCEDURE [dbo].[MyStoredProcedure]GO[/code]</description><pubDate>Thu, 23 Jul 2009 06:49:59 GMT</pubDate><dc:creator>ddunn-1115368</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>[quote][b]dbaker (7/23/2009)[/b][hr]would be better NOT to use the sp_ prefix ![/quote]Strongly agreed. If you do not intend to put a stored procedure in the master database (which should not be done unless you have a good reason for doing so and know the consequences) then do not use the "sp_" prefix. On the rare occasion when you want to place a stored procedure in the master database (usually your home-grown administrative procedures) then you can use "sp__" with two underscores to avoid colliding with current or future Microsoft stored procedures.</description><pubDate>Thu, 23 Jul 2009 06:03:54 GMT</pubDate><dc:creator>Noel McKinney</dc:creator></item><item><title>RE: Dynamic creation of  Insert, Update, Delete Stored procedure</title><link>http://www.sqlservercentral.com/Forums/Topic757979-444-1.aspx</link><description>would be better NOT to use the sp_ prefix !this is clear in BOL[font="Courier New"]We strongly recommend that you not use the prefix sp_ in the procedure name. This prefix is used by SQL Server to designate system stored procedures.We recommend that you do not create any stored procedures using sp_ as a prefix. SQL Server uses the sp_ prefix to designate system stored procedures. The name you choose may conflict with some future system procedure. If your application uses nonschema qualified name references and your own procedure name conflicts with a system procedure name, your application will break because the name binds to the system procedure, not your own.[/font]regards,Dick</description><pubDate>Thu, 23 Jul 2009 05:38:22 GMT</pubDate><dc:creator>dbaker-620086</dc:creator></item></channel></rss>