<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: udf code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 08:34:29 GMT</pubDate>
    <description>DZone Snippets: udf code</description>
    <item>
      <title>DateOnly</title>
      <link>http://snippets.dzone.com/posts/show/1186</link>
      <description>&lt;code&gt;&lt;br /&gt;-- Gets the date part of a datetime input&lt;br /&gt;-- Created 08/05/04 by Oskar Austegard&lt;br /&gt;ALTER FUNCTION dbo.fnDateOnly&lt;br /&gt;(&lt;br /&gt;	@DateTime datetime --The input date whose date part we want&lt;br /&gt;)&lt;br /&gt;RETURNS datetime&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;	RETURN (CONVERT(datetime, CONVERT(varchar(10), @DateTime, 101)))&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:53:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1186</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>IsReallyInteger</title>
      <link>http://snippets.dzone.com/posts/show/1185</link>
      <description>&lt;code&gt;&lt;br /&gt;----------------------------------------------------------------------------&lt;br /&gt;--Purpose: Checks that the input string is really an integer of the specified type.  &lt;br /&gt;-- To be used in place of the ISNUMERIC function, as it can't be trusted.&lt;br /&gt;-- See http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=10194&lt;br /&gt;--Inspired by: http://www.aspfaq.com/show.asp?id=2390 &lt;br /&gt;--Created: 10/20/05 by Oskar Austegard.&lt;br /&gt;--Updated: 11/16/05 by Oskar Austegard - fixed bugs&lt;br /&gt;----------------------------------------------------------------------------&lt;br /&gt;ALTER FUNCTION dbo.IsReallyInteger&lt;br /&gt;(  &lt;br /&gt;  @Num varchar(64), --Input string to be checked&lt;br /&gt;  @Type varchar(8) --Type of integer: bigint, int, smallint, tinyint&lt;br /&gt;)  &lt;br /&gt;RETURNS BIT  &lt;br /&gt;BEGIN  &lt;br /&gt;  --Get the absolute value of the number by removing a leading - or +&lt;br /&gt;  DECLARE @AbsNum varchar(64), @Length tinyint&lt;br /&gt;  SET @AbsNum = CASE WHEN LEFT(@Num, 1) IN ('-', '+') THEN SUBSTRING(@Num, 2, LEN(@Num)) ELSE @Num END&lt;br /&gt;&lt;br /&gt;  --Remove leading zeros&lt;br /&gt;  WHILE LEN(@AbsNum) &gt; 1 AND LEFT(@AbsNum, 1) = '0'&lt;br /&gt;    SET @AbsNum = SUBSTRING(@AbsNum, 2, LEN(@AbsNum))&lt;br /&gt;&lt;br /&gt;  SET @Length = LEN(@AbsNum)  &lt;br /&gt;  --Reinsert the - in negative numbers&lt;br /&gt;  SET @Num = CASE WHEN LEFT(@Num, 1) = '-' THEN '-' + @AbsNum ELSE @AbsNum END&lt;br /&gt;&lt;br /&gt;  --Check for empty string or non-digits&lt;br /&gt;  IF @AbsNum = '' OR PATINDEX('%[^0-9]%', @AbsNum) &gt; 0&lt;br /&gt;    RETURN 0&lt;br /&gt;&lt;br /&gt;  --Check limits by type&lt;br /&gt;  IF (@Type = 'bigint' AND (@Length &lt; 19 OR (@Length = 19 AND (@AbsNum &lt; '9223372036854775807' OR @Num = '-9223372036854775808'))))&lt;br /&gt;    OR (@Type = 'int' AND (@Length &lt; 10 OR (@Length = 10 AND (@AbsNum &lt; '2147483648' OR @Num = '-2147483648'))))&lt;br /&gt;    OR (@Type = 'smallint' AND (@Length &lt; 5 OR (@Length = 5 AND (@AbsNum &lt; '32768' OR @Num = '-32768'))))&lt;br /&gt;    OR (@Type = 'tinyint' AND LEFT(@Num, 1) &lt;&gt; '-' AND (@Length &lt; 3 OR (@Length = 3 AND @AbsNum &lt; '256')))&lt;br /&gt;    RETURN 1 --Success&lt;br /&gt;  --Else&lt;br /&gt;  RETURN 0 --Failure&lt;br /&gt;END  &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:50:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1185</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Create a Number Table</title>
      <link>http://snippets.dzone.com/posts/show/1184</link>
      <description>Created 08/26/05 by Oskar Austegard (&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;) from article at &lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp"&gt;http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp&lt;/a&gt;&lt;br /&gt;Can be used inline in functions, or to create a standalone Numbers table (as required by &lt;a href="http://www.bigbold.com/snippets/posts/show/1183"&gt;dbo.Split&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--Creates a table of sequential numbers, useful for all sorts of things&lt;br /&gt;--Created 08/26/05 by Oskar Austegard from article at &lt;br /&gt;--http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp&lt;br /&gt;--Limits: @Min and @Max must be between -2147483647 and 2147483647, including.&lt;br /&gt;--If @Max &lt;= @Min, only a single record with @Min is created&lt;br /&gt;ALTER FUNCTION dbo.NumberTable (@Min int, @Max int)&lt;br /&gt;RETURNS @T TABLE (Number int NOT NULL PRIMARY KEY)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;  -- Seed the table with the min value&lt;br /&gt;  INSERT @T VALUES (@Min)&lt;br /&gt;  --Loop until all the rows are created, inserting ever more records for each iteration (1, 2, 4, etc)&lt;br /&gt;  WHILE @@ROWCOUNT &gt; 0&lt;br /&gt;	BEGIN&lt;br /&gt;	  INSERT @T &lt;br /&gt;	  --Get the next values by adding the current max - start value + 1 to each existing number&lt;br /&gt;	  --need to calculate increment value first to avoid arithmetic overflow near limits of int&lt;br /&gt;	  SELECT t.Number + (x.MaxNumber - @Min + 1)&lt;br /&gt;	  FROM @T t&lt;br /&gt;	    CROSS JOIN (SELECT MaxNumber = MAX(Number) FROM @T) x --Current max&lt;br /&gt;	  WHERE&lt;br /&gt;	    --Do not exceed the Max - shift the increment to the right side to take advantage of index&lt;br /&gt;	    t.Number &lt;= @Max - (x.MaxNumber - @Min + 1)&lt;br /&gt;	END&lt;br /&gt;  RETURN&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:47:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1184</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Fast(er) Split Function</title>
      <link>http://snippets.dzone.com/posts/show/1183</link>
      <description>Created 08/29/05 by Oskar Austegard (&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;) from Erland Sommarskog's code at&lt;br /&gt;&lt;a href="http://www.sommarskog.se/arrays-in-sql.html#tblnum-core"&gt;http://www.sommarskog.se/arrays-in-sql.html#tblnum-core&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Requires the presence of a Numbers table - which can be created using the &lt;a href="http://www.bigbold.com/snippets/posts/show/1184"&gt;dbo.NumberTable&lt;/a&gt; function&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--Fast(er) Split function using a sequence table (provided by the UDF dbo.NumberTable)&lt;br /&gt;--Created 08/29/05 by Oskar Austegard from Erland Sommarskog's code at&lt;br /&gt;--http://www.sommarskog.se/arrays-in-sql.html#tblnum-core&lt;br /&gt;ALTER FUNCTION Split (&lt;br /&gt;  @List varchar(7998), --The delimited list&lt;br /&gt;  @Del char(1) = ',' --The delimiter&lt;br /&gt;) &lt;br /&gt;RETURNS @T TABLE (ListID int IDENTITY, Item varchar(7998))&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;  DECLARE @WrappedList varchar(8000)&lt;br /&gt;  SELECT @WrappedList = @Del + @List + @Del&lt;br /&gt;&lt;br /&gt;  INSERT INTO @T (Item)&lt;br /&gt;  SELECT SUBSTRING(@WrappedList, n.Number + 1, CHARINDEX(@Del, @WrappedList, n.Number + 1) - n.Number - 1)&lt;br /&gt;  FROM dbo.Numbers n&lt;br /&gt;  WHERE n.Number &lt;= LEN(@WrappedList) - 1&lt;br /&gt;    AND SUBSTRING(@WrappedList, n.Number, 1) = @Del&lt;br /&gt;    AND LEN(SUBSTRING(@WrappedList, n.Number + 1, CHARINDEX(@Del, @WrappedList, n.Number + 1) - n.Number - 1)) &gt; 0&lt;br /&gt;  RETURN&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:43:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1183</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Add Variant to List</title>
      <link>http://snippets.dzone.com/posts/show/1182</link>
      <description>If both element and list are nvarchars, using &lt;a href="http://www.bigbold.com/snippets/posts/show/1181"&gt;dbo.fnAddToList&lt;/a&gt; will be faster&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--Adds a sql_variant element to the end of a sql_variant list, after first inserting a delimiter (nvarchar)&lt;br /&gt;--If both element and list are nvarchars, using dbo.fnAddToList will be faster&lt;br /&gt;ALTER FUNCTION dbo.fnAddVarToList (@VarList sql_variant, @VarNew sql_variant, @Del nvarchar(10))&lt;br /&gt;RETURNS nvarchar(4000)&lt;br /&gt;AS  &lt;br /&gt;BEGIN &lt;br /&gt;	DECLARE @List nvarchar(4000), @New nvarchar(4000)&lt;br /&gt;	SELECT @List = NULLIF(CONVERT(nvarchar(4000), @VarList), ''), &lt;br /&gt;		@New = NULLIF(CONVERT(nvarchar(4000), @VarNew), '')&lt;br /&gt;  --First try the concatened string, if null then just the list, &lt;br /&gt;	--if it too is null, just the new element&lt;br /&gt;	RETURN COALESCE(@List + @Del + @New, @List, @New)&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:39:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1182</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Add to List function</title>
      <link>http://snippets.dzone.com/posts/show/1181</link>
      <description>&lt;code&gt;&lt;br /&gt;--Adds an element (nvarchar) to the end of a list (nvarchar), after first inserting a delimiter (nvarchar)&lt;br /&gt;ALTER FUNCTION dbo.fnAddToList (@List nvarchar(4000), @New nvarchar(4000), @Del nvarchar(10))&lt;br /&gt;RETURNS nvarchar(4000)&lt;br /&gt;AS  &lt;br /&gt;BEGIN &lt;br /&gt;	--Treat ''s as NULLs&lt;br /&gt;	SELECT @List = NULLIF(@List, ''), @Del = NULLIF(@Del, ''), @New = NULLIF(@New, '')&lt;br /&gt;  --First try the concatened string, if null then just the list, &lt;br /&gt;	--if it too is null, just the new element&lt;br /&gt;	RETURN COALESCE(@List + @Del + @New, @List, @New)&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:37:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1181</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>IsSmallDate Function - Corrected</title>
      <link>http://snippets.dzone.com/posts/show/1180</link>
      <description>Courtesy of Mounir BEN HAMED, the following is &lt;a href="http://austegard.blogspot.com/2006/04/sql-corrected-issmalldate-function.html"&gt;the corrected function&lt;/a&gt;.&lt;br /&gt;Background Info: See &lt;a href="http://austegard.blogspot.com/2005/04/sql-isdatefoo-and-castfoo-as.html"&gt; SQL: ISDATE(@foo) and CAST(@foo AS smalldatetime)&lt;/a&gt; and the incorrect &lt;a href="http://austegard.blogspot.com/2005/09/sql-updated-issmalldate-function.html"&gt;Updated IsSmallDate function&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;--Checks if a string is a valid smalldatetime&lt;br /&gt;--Updated 04/03/06 by Oskar Austegard after bug find by Mounir Ben Hamed&lt;br /&gt;CREATE FUNCTION dbo.IsSmallDate&lt;br /&gt;(&lt;br /&gt;  @SmallDateString varchar(20) --The input string to check&lt;br /&gt;)&lt;br /&gt;RETURNS BIT&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;  DECLARE @Result bit&lt;br /&gt;  SET @SmallDateString = LTRIM(RTRIM(@SmallDateString))&lt;br /&gt;&lt;br /&gt;  IF ISDATE(@SmallDateString) = 1&lt;br /&gt;   AND CONVERT(datetime, @SmallDateString) BETWEEN '1900-01-01' AND '2079-06-06'&lt;br /&gt;    SET @Result = 1&lt;br /&gt;  ELSE&lt;br /&gt;    SET @Result = 0&lt;br /&gt;&lt;br /&gt;  RETURN @Result&lt;br /&gt;END&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:34:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1180</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>Split a delimited list into a table</title>
      <link>http://snippets.dzone.com/posts/show/1179</link>
      <description>&lt;code&gt;&lt;br /&gt;/*&lt;br /&gt;* Creates a table out of a delimited list&lt;br /&gt;* Input:&lt;br /&gt;* 	@List nvarchar(4000) - delimited list to be split&lt;br /&gt;*		@Del nvarchar(10) - delimiter (trailing spaces are ignored)&lt;br /&gt;* Output: 2 Column table, with columns ListID int, and ListItem nvarchar(200)&lt;br /&gt;* Usage: &lt;br /&gt;* 	SELECT * FROM Foo &lt;br /&gt;* 	WHERE FooBar IN (SELECT ListItem FROM dbo.fnSplit('Foo','Bar','FooBar'))&lt;br /&gt;* Updated 08/30/04 by Oskar Austegard&lt;br /&gt;*/&lt;br /&gt;ALTER FUNCTION dbo.fnSplit&lt;br /&gt;(&lt;br /&gt;	@List nvarchar(4000), &lt;br /&gt;	@Del nvarchar(10) = ','&lt;br /&gt;)&lt;br /&gt;RETURNS @ListTable TABLE &lt;br /&gt;(&lt;br /&gt;	ListID int IDENTITY , &lt;br /&gt;	Item nvarchar(200)&lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;	DECLARE @LenDel int&lt;br /&gt;	DECLARE @Pos int&lt;br /&gt;	DECLARE @Item nvarchar(200)&lt;br /&gt;	&lt;br /&gt;	--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue&lt;br /&gt;	SET @LenDel = LEN(@Del + '|') - 1 &lt;br /&gt;&lt;br /&gt;	SET @Pos = CHARINDEX(@Del, @List)&lt;br /&gt;	WHILE @Pos &gt; 0&lt;br /&gt;	BEGIN&lt;br /&gt;		--Get the item&lt;br /&gt;		SET @Item = SUBSTRING(@List, 1, @Pos-1)&lt;br /&gt;		--Add it to the table (if not empty string) &lt;br /&gt;		IF LEN(LTRIM(@Item)) &gt; 0&lt;br /&gt;			INSERT @ListTable (Item) VALUES (LTRIM(@Item))&lt;br /&gt;		--Remove the item from the list&lt;br /&gt;		SET @List = STUFF(@List, 1, @Pos+@LenDel-1, '')&lt;br /&gt;		--Get the position of the next delimiter&lt;br /&gt;		SET @Pos = CHARINDEX(@Del, @List)		&lt;br /&gt;	END&lt;br /&gt;	&lt;br /&gt;	--Add the last item to the table (if not empty string) &lt;br /&gt;	IF LEN(LTRIM(@List)) &gt; 0&lt;br /&gt;		INSERT @ListTable (Item) VALUES (LTRIM(@List))&lt;br /&gt;&lt;br /&gt;	RETURN &lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Oskar Austegard &lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:31:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1179</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
    <item>
      <title>system_function_schema.fn_datadictionary</title>
      <link>http://snippets.dzone.com/posts/show/1175</link>
      <description>The following lengthy SQL script will create a system function in SQL Server that returns a data dictionary for any database on the server when called like this:&lt;br /&gt;&lt;br /&gt;select * from ::fn_datadictionary()&lt;br /&gt;&lt;br /&gt;The output includes the Description of the objects.&lt;br /&gt;&lt;br /&gt;Oskar Austegard&lt;br /&gt;&lt;a href="http://mo.notono.us"&gt;http://mo.notono.us&lt;/a&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;EXEC SP_CONFIGURE 'ALLOW UPDATES', 1&lt;br /&gt;RECONFIGURE WITH OVERRIDE&lt;br /&gt;USE master&lt;br /&gt;go&lt;br /&gt;CREATE FUNCTION system_function_schema.fn_datadictionary ()&lt;br /&gt;RETURNS @DataDictionary TABLE (&lt;br /&gt; object_id int,&lt;br /&gt; object_name nvarchar(128),&lt;br /&gt; object_type nvarchar(128),&lt;br /&gt; column_order int NULL,&lt;br /&gt; column_name varchar(60) NULL,&lt;br /&gt; column_datatype varchar(20) NULL,&lt;br /&gt; column_length int NULL,&lt;br /&gt; object_description varchar(500) NULL&lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt; DECLARE @object_name nvarchar(128)&lt;br /&gt;-------------------------&lt;br /&gt;--Tables and Columns&lt;br /&gt;-------------------------&lt;br /&gt; DECLARE table_cursor CURSOR FOR &lt;br /&gt;  SELECT name FROM sysobjects &lt;br /&gt;  WHERE type IN ('U') AND status &gt; 1 ORDER BY name&lt;br /&gt;&lt;br /&gt; OPEN table_cursor&lt;br /&gt; FETCH NEXT FROM table_cursor INTO @object_name&lt;br /&gt; WHILE @@FETCH_STATUS = 0&lt;br /&gt; BEGIN&lt;br /&gt;  --Tables&lt;br /&gt;  INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Table',&lt;br /&gt;   column_order = 0, column_name = NULL, &lt;br /&gt;   column_datatype = NULL, column_length = NULL,&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o &lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'table', @object_name, null, default) e &lt;br /&gt;    ON o.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;&lt;br /&gt;  --Columns&lt;br /&gt;   INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Table Column',&lt;br /&gt;    column_order = c.colorder, column_name = c.[name], &lt;br /&gt;   column_datatype = t.[name], column_length = c.[length],&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype&lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'table', @object_name, N'column', null) e &lt;br /&gt;    ON c.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;  ORDER BY c.colorder&lt;br /&gt;&lt;br /&gt;    FETCH NEXT FROM table_cursor INTO @object_name&lt;br /&gt; END&lt;br /&gt; CLOSE table_cursor&lt;br /&gt; DEALLOCATE table_cursor&lt;br /&gt;&lt;br /&gt;-------------------------&lt;br /&gt;--Views and Columns&lt;br /&gt;-------------------------&lt;br /&gt; DECLARE view_cursor CURSOR FOR &lt;br /&gt;  SELECT name FROM sysobjects &lt;br /&gt;  WHERE type IN ('V') AND status &gt; 1 ORDER BY name&lt;br /&gt;&lt;br /&gt; OPEN view_cursor&lt;br /&gt; FETCH NEXT FROM view_cursor INTO @object_name&lt;br /&gt; WHILE @@FETCH_STATUS = 0&lt;br /&gt; BEGIN&lt;br /&gt;  --Views&lt;br /&gt;  INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'View',&lt;br /&gt;   column_order = 0, column_name = NULL, &lt;br /&gt;   column_datatype = NULL, column_length = NULL,&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o &lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'view', @object_name, null, default) e &lt;br /&gt;    ON o.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;&lt;br /&gt;  --Columns&lt;br /&gt;   INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'View Column',&lt;br /&gt;    column_order = c.colorder, column_name = c.[name], &lt;br /&gt;   column_datatype = t.[name], column_length = c.[length],&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype&lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'view', @object_name, N'column', null) e &lt;br /&gt;    ON c.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;  ORDER BY c.colorder&lt;br /&gt;&lt;br /&gt;    FETCH NEXT FROM view_cursor INTO @object_name&lt;br /&gt; END&lt;br /&gt; CLOSE view_cursor&lt;br /&gt; DEALLOCATE view_cursor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;---------------------------&lt;br /&gt;--Procedures and Parameters&lt;br /&gt;---------------------------&lt;br /&gt; DECLARE proc_cursor CURSOR FOR &lt;br /&gt;  SELECT name FROM sysobjects &lt;br /&gt;  WHERE type IN ('P') AND status &gt; 1 ORDER BY name&lt;br /&gt;&lt;br /&gt; OPEN proc_cursor&lt;br /&gt; FETCH NEXT FROM proc_cursor INTO @object_name&lt;br /&gt; WHILE @@FETCH_STATUS = 0&lt;br /&gt; BEGIN&lt;br /&gt;  --Procedures&lt;br /&gt;  INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Procedure',&lt;br /&gt;   column_order = 0, column_name = NULL, &lt;br /&gt;   column_datatype = NULL, column_length = NULL,&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o &lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'procedure', @object_name, null, default) e &lt;br /&gt;    ON o.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;&lt;br /&gt;  --Parameters&lt;br /&gt;   INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Procedure Parameter',&lt;br /&gt;    column_order = c.colorder, column_name = c.[name], &lt;br /&gt;   column_datatype = t.[name], column_length = c.[length],&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype&lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'procedure', @object_name, N'parameter', null) e &lt;br /&gt;    ON c.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;   AND t.name &lt;&gt; 'sysname'&lt;br /&gt;  ORDER BY c.colorder&lt;br /&gt;&lt;br /&gt;    FETCH NEXT FROM proc_cursor INTO @object_name&lt;br /&gt; END&lt;br /&gt; CLOSE proc_cursor&lt;br /&gt; DEALLOCATE proc_cursor&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;---------------------------&lt;br /&gt;--Functions and Parameters&lt;br /&gt;---------------------------&lt;br /&gt; DECLARE func_cursor CURSOR FOR &lt;br /&gt;  SELECT name FROM sysobjects &lt;br /&gt;  WHERE type IN ('FN', 'TF', 'IF') AND status &gt; 1 ORDER BY name&lt;br /&gt;&lt;br /&gt; OPEN func_cursor&lt;br /&gt; FETCH NEXT FROM func_cursor INTO @object_name&lt;br /&gt; WHILE @@FETCH_STATUS = 0&lt;br /&gt; BEGIN&lt;br /&gt;  --Functions&lt;br /&gt;  INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Function',&lt;br /&gt;   column_order = 0, column_name = NULL, &lt;br /&gt;   column_datatype = NULL, column_length = NULL,&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o &lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'function', @object_name, null, default) e &lt;br /&gt;    ON o.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;&lt;br /&gt;  --Parameters&lt;br /&gt;   INSERT @DataDictionary &lt;br /&gt;  SELECT object_id = o.[id], object_name = o.[name], object_type = 'Function Parameter',&lt;br /&gt;    column_order = c.colorder, &lt;br /&gt;   column_name = CASE WHEN c.[name] = '' THEN '' ELSE c.[name] END, &lt;br /&gt;   column_datatype = t.[name], column_length = c.[length],&lt;br /&gt;    object_description = LTRIM(CAST(e.value AS varchar(500)))&lt;br /&gt;  FROM sysobjects o INNER JOIN syscolumns c ON o.id = c.id INNER JOIN systypes t ON c.xtype = t.xtype&lt;br /&gt;   LEFT JOIN ::FN_LISTEXTENDEDPROPERTY(N'MS_Description', N'user',N'dbo',N'function', @object_name, N'parameter', null) e &lt;br /&gt;    ON c.name = e.objname&lt;br /&gt;  WHERE o.name = @object_name&lt;br /&gt;   AND t.name &lt;&gt; 'sysname'&lt;br /&gt;  ORDER BY c.colorder&lt;br /&gt;&lt;br /&gt;    FETCH NEXT FROM func_cursor INTO @object_name&lt;br /&gt; END&lt;br /&gt; CLOSE func_cursor&lt;br /&gt; DEALLOCATE func_cursor&lt;br /&gt; RETURN&lt;br /&gt;END&lt;br /&gt;GO&lt;br /&gt;EXEC SP_CONFIGURE 'ALLOW UPDATES', 0&lt;br /&gt;RECONFIGURE WITH OVERRIDE&lt;br /&gt;GO&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 02:19:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1175</guid>
      <author>austegard (Oskar Austegard)</author>
    </item>
  </channel>
</rss>
