<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dushyanth Jyothi &#187; MS SQL SERVER</title>
	<atom:link href="http://dushi.co.uk/tag/ms-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://dushi.co.uk</link>
	<description>Me, My Life and the Binary</description>
	<lastBuildDate>Sun, 18 Dec 2011 00:54:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Loop through database tables and columns</title>
		<link>http://dushi.co.uk/15/07/2009/loop-through-database-tables-and-columns/</link>
		<comments>http://dushi.co.uk/15/07/2009/loop-through-database-tables-and-columns/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 10:50:06 +0000</pubDate>
		<dc:creator>Dushyanth Jyothi</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[MS SQL SERVER]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://dushi.co.uk/?p=76</guid>
		<description><![CDATA[T-SQL code [Stored Procedure] to loop through all tables and all columns in a database Applies to MS SQL SERVER 2008 Change [DATABASE_NAME],[SCHEMA_NAME],[USER_NAME] accordingly Stored Procedure: USE [DATABASE_NAME] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROC [SCHEMA_NAME].[loop_Through_Tables_Columns] AS BEGIN SET NOCOUNT ON DECLARE @TableName nvarchar(256) DECLARE @ColumnName nvarchar(128) SET @TableName = '' [...]]]></description>
			<content:encoded><![CDATA[<p><strong>T-SQL code [Stored Procedure] to loop through all tables and all columns in a database</strong></p>
<p><strong>Applies to MS SQL SERVER 2008</strong></p>
<p>Change [DATABASE_NAME],[SCHEMA_NAME],[USER_NAME] accordingly</p>
<p><strong>Stored Procedure:</strong><br />
<code lang="tsql[lines]">USE [DATABASE_NAME]<br />
GO<br />
SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
CREATE PROC [SCHEMA_NAME].[loop_Through_Tables_Columns]<br />
AS<br />
BEGIN</p>
<p>	SET NOCOUNT ON<br />
	DECLARE @TableName nvarchar(256)<br />
	DECLARE @ColumnName nvarchar(128)<br />
	SET  @TableName = ''</p>
<p>	WHILE @TableName IS NOT NULL<br />
	BEGIN<br />
		SET @ColumnName = ''<br />
		SET @TableName =<br />
			(SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES<br />
				WHERE TABLE_TYPE = 'BASE TABLE' /**You can remove this condition to go thourgh with both views and tables**/<br />
					AND	 QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName<br />
					AND	OBJECTPROPERTY(<br />
						OBJECT_ID(<br />
							QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)<br />
							 ), 'IsMSShipped'<br />
						       ) = 0<br />
			)</p>
<p>		PRINT 'TableName:  '+ CAST(@TableName AS CHAR)</p>
<p>			WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)<br />
			BEGIN<br />
				SET @ColumnName =<br />
				(<br />
					SELECT MIN(QUOTENAME(COLUMN_NAME))<br />
					FROM INFORMATION_SCHEMA.COLUMNS<br />
					WHERE TABLE_SCHEMA	= PARSENAME(@TableName, 2)<br />
						AND	TABLE_NAME	= PARSENAME(@TableName, 1)<br />
						/**You you want to go thourgh with certain type of data types**/<br />
						AND	DATA_TYPE IN('char','varchar','nchar','nvarchar','text','ntext','image','xml','varbinary')<br />
						AND	QUOTENAME(COLUMN_NAME) > @ColumnName<br />
				)<br />
				PRINT 'ColumnName:  '+ CAST(@ColumnName AS CHAR)<br />
			END<br />
	END</p>
<p>END<br />
GRANT EXECUTE ON [SCHEMA_NAME].[loop_Through_Tables_Columns] to USER_NAME </code></p>
<p><strong>Execute Stored Procedure:</strong></p>
<p><code lang="tsql[lines]">EXEC [SCHEMA_NAME].[loop_Through_Tables_Columns]</code></p>
]]></content:encoded>
			<wfw:commentRss>http://dushi.co.uk/15/07/2009/loop-through-database-tables-and-columns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

