<?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 'Tips &amp;amp; Tricks' and 'TSQL'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Tips+%26amp%3B+Tricks,TSQL&amp;orTags=0</link><description>Search results matching tags 'Tips &amp;amp; Tricks' and 'TSQL'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>sp_spaceused Alternative</title><link>http://sqlblog.com/blogs/andrew_kelly/archive/2013/05/17/sp-spaceused-alternative.aspx</link><pubDate>Fri, 17 May 2013 20:35:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:49097</guid><dc:creator>Andrew Kelly</dc:creator><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I don’t know why but for some reason I have never liked using sp_spaceused. It probably started a long time ago when the results were often inaccurate due to the meta data being out of date. I am pretty sure that was fixed somewhere along the line but the system stored procedure had some other limitations that usually prevented me from seeing the data that I really wanted in most cases. So at some point I created my own stored procedure to view the data I was most interested in almost on a daily occurrence. Now to be totally accurate I started with code that one of the SSMS reports uses to show data and index space usage.&amp;nbsp; The code was not the prettiest so I did clean it up a little and modified it to better suite my needs but the core worked so I used it. I then added some additional queries to get other data that the original query didn’t have such as the file size and growth info. I also slightly modified one of the many variations of functions available out there that format numbers with commas for better readability. In this case it is simply called dbo.commas. Normally I have a database on every instance in which I keep custom procedures / functions etc. in which I can count on certain objects to exist so I can reference them in other objects. These examples are shown with no fully qualified path to the dbo.commas UDF so the UDF and the procedure would have to exist in each database or the one that you care to use it in. While that works you should consider doing one of two alternatives. Either add a utility database like I mentioned and put the two objects there or rename the procedure to have sp_xxx and put the objects in the master database and then mark them as system objects.&amp;nbsp; That way you can call the procedure from the context of any database. &lt;/p&gt;  &lt;p&gt;OK so enough about that lets see what the procedure does and how to use it. The procedure has two parameters @DBName and @ShowFileInfo. The @DBName parameter is pretty self explanatory and will dictate which database the results are for. The second parameter is really optional as it defaults to a value = 0.&amp;nbsp; If the @ShowFileInfo parameter = 0 then only one result set will be returned which contains the relevant data for each table in the database. More on that in a bit. If a value of 1 is passed in then a 2nd result set is returned that contains information related to the size of the database, it’s files and growth. See further down for more details on both result sets.&amp;nbsp; These are all acceptable ways to call the stored procedure:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;EXEC dbo.ListDBSpaceInfo @DBName = 'Adventureworks'      &lt;br&gt;EXEC dbo.ListDBSpaceInfo @DBName = 'Adventureworks', @ShowFileInfo = 0       &lt;br&gt;EXEC dbo.ListDBSpaceInfo @DBName = 'Adventureworks', @ShowFileInfo = 1       &lt;br&gt;EXEC dbo.ListDBSpaceInfo 'Adventureworks'       &lt;br&gt;EXEC dbo.ListDBSpaceInfo 'Adventureworks', 1       &lt;br&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Here is what the results would look like if you ran it against the Adventureworks database.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/andrew_kelly/ADWorks_7FDB9BEC.png"&gt;&lt;img width="945" height="294" title="ADWorks" style="border-width:0px;padding-top:0px;padding-right:0px;padding-left:0px;display:inline;background-image:none;" alt="ADWorks" src="http://sqlblog.com/blogs/andrew_kelly/ADWorks_thumb_736D78C3.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The first result set lists each table showing the size in MB’s for the reserved, data and index space. The row counts and the Created and Modified dates. This is the information I find most useful the majority of the time. It orders the tables by Reserved MB but you may prefer by table name or something else. The code is easy enough to modify to sort however you like and you can even add an additional parameter to chose the sort by column.&amp;nbsp; Again I use it this way most often so it is what it is.&lt;/p&gt;  &lt;p&gt;The 2nd result set if you choose to return it will list certain information about the database and it’s files. It will show the current total size of all the data files which is pretty definitive by nature. However the next few columns are dependent on the max size and growth properties of each file. For instance if any of the data files are set to –1 which means unlimited growth the resultant column for Max Size will say Unlimited as will the Potential Free Space. If the files have a fixed size the total max size if then calculated and displayed along with that amount minus the current data usage to give you the potential free space. Basically that is how much free space the database can theoretically accommodate assuming there is enough space on the disks. The same holds true for the log file. These numbers are most useful if you have limits set but the information can give you a good idea of the overall condition of the data and log file usage. &lt;/p&gt;  &lt;p&gt;Now I understand that none of this is information that wasn’t available to users before but I usually had to go to several different sources to get a comprehensive view like this. The purpose of this blog post was not to wow everyone with some rocket science code it was simply to share with others who may find it useful. I use it all the time so I figure there must be others who can take advantage of it as well and hopefully customize it to suite their needs like I have. Have fun everyone, here is the code:&lt;/p&gt;  &lt;p&gt;Andy&lt;/p&gt;</description></item><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>