<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Developer', 'Tips &amp;amp; Tricks', and 'TSQL'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Developer,Tips+%26amp%3B+Tricks,TSQL&amp;orTags=0</link><description>Search results matching tags 'Developer', 'Tips &amp;amp; Tricks', and 'TSQL'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Cleaning up Un-Named Defaults</title><link>http://sqlblog.com/blogs/andrew_kelly/archive/2009/06/26/cleaning-up-un-named-defaults.aspx</link><pubDate>Fri, 26 Jun 2009 16:07:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:14944</guid><dc:creator>Andrew Kelly</dc:creator><description>&lt;P&gt;I just had a situation in which we are automating the comparing of databases and I came across a bunch of Defaults that were never explicitly named. This causes them to get a new name each time you create them and makes it much more difficult to write future update scripts when you don’t know the actual name it will be. In any case I decided to clean them up and since there were about a hundred I didn’t want to create all the cleanup code by hand and wrote a little script to generate the DROP &amp;amp; ADD’s which I figured others may find useful as well. The code would look much better if Windows Live Writer would keep the rich text when pasting into it but it doesn’t by default. There are some plug in’s to allow this but I have had little success so far in getting ones to install that actually do what I want. I will just have to deal with that later when I have more time. &lt;/P&gt;
&lt;P&gt;The code below simply adds the necessary meta-data to the temp table that I need to Drop and Add the default constraints. I then run a select statement that dynamically builds each Drop or Add using that data and the resulting text can be used as a script to do the cleanup.&amp;nbsp; It’s pretty simple stuff but may save some typing for anyone else wishing to cleanup messes like this that lazy developers :) and SSMS create. In my opinion ALL objects should be explicitly named at the time of initial creation and nothing should be left to be auto-generated by the tool or the engine. It will only lead to heartache down the road.&amp;nbsp; And as always, test any code you find here first…&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;SET NOCOUNT ON ; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;CREATE TABLE #DF ([Table Name] NVARCHAR(128), [Column Name] NVARCHAR(128), &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Default Name] NVARCHAR(128), [definition] NVARCHAR(200)) ; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;INSERT INTO #DF ([Table Name], [Column Name], [Default Name], [definition] ) &lt;BR&gt;SELECT&amp;nbsp; object_name(d.[parent_object_id]) AS [Table Name], c.[name] AS [Column Name], &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d.[name] AS [Default Name], d.[definition] &lt;BR&gt;&amp;nbsp; FROM sys.columns AS c INNER JOIN sys.default_constraints AS d &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON c.[object_id] = d.[parent_object_id] AND c.[column_id] = d.[parent_column_id] &lt;BR&gt;AND d.is_system_named = 1 &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;--&amp;nbsp;&amp;nbsp; Drop the existing constraints &lt;BR&gt;SELECT 'DECLARE @DF_Name NVARCHAR(128) ;' &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;SELECT N'SELECT @DF_Name = d.[name]&amp;nbsp; from sys.columns AS c INNER JOIN sys.default_constraints AS d &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON c.[object_id] = d.[parent_object_id] AND c.[column_id] = d.[parent_column_id] &lt;BR&gt;WHERE c.[object_id] = object_id(''' + [Table Name] + N''') AND c.[name] = N''' + [Column Name] + N''' ; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;IF @DF_Name IS NOT NULL &lt;BR&gt;BEGIN &lt;BR&gt;&amp;nbsp;&amp;nbsp; EXEC(''ALTER TABLE [dbo].[' + [Table Name] + N'] DROP CONSTRAINT ['' + @DF_Name + '']'') ; &lt;BR&gt;END ; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;' &lt;BR&gt;FROM #DF &lt;BR&gt;ORDER BY [Table Name]; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;"&gt;--&amp;nbsp;&amp;nbsp; Create the New constraints &lt;BR&gt;SELECT N'IF NOT EXISTS(SELECT * FROM sys.columns AS c INNER JOIN sys.default_constraints AS d &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON c.[object_id] = d.[parent_object_id] AND c.[column_id] = d.[parent_column_id] &lt;BR&gt;WHERE c.[name] = ''' + [Column Name] + N''' AND OBJECT_NAME(d.[parent_object_id]) = ''' + [Table Name] + N''') &lt;BR&gt;BEGIN &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ALTER TABLE [' + [Table Name] + N'] ADD&amp;nbsp; CONSTRAINT [DF_' + [Table Name] + N'_' &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + [Column Name] + N']&amp;nbsp; DEFAULT ' + [definition] +&amp;nbsp; N'FOR [' + [Column Name] + N'] ; &lt;BR&gt;END ; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="LINE-HEIGHT:115%;FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;' &lt;BR&gt;FROM #DF &lt;BR&gt;ORDER &lt;SPAN style="LINE-HEIGHT:115%;FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;BY [&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT:115%;FONT-FAMILY:'Comic Sans MS';FONT-SIZE:7.5pt;mso-bidi-font-family:Arial;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;Table Name];&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="LINE-HEIGHT:115%;FONT-FAMILY:'Calibri','sans-serif';FONT-SIZE:10pt;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:Arial;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;&lt;SPAN style="LINE-HEIGHT:115%;FONT-FAMILY:'Comic Sans MS';FONT-SIZE:7.5pt;mso-bidi-font-family:Arial;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;</description></item></channel></rss>