<?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>Serge&#039;s Technology View &#187; table</title>
	<atom:link href="http://blog.dragonsoft.us/tag/table/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dragonsoft.us</link>
	<description>Talk about Technologies, Software Architecture and Management</description>
	<lastBuildDate>Tue, 31 Jan 2012 01:43:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tip: Get list of columns for the table</title>
		<link>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/</link>
		<comments>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/#comments</comments>
		<pubDate>Mon, 12 May 2008 20:26:47 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[list of columns]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[table]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/?p=180</guid>
		<description><![CDATA[When you dealing with dynamic SQL or unknown table structure, SQL meta data could help recover the missing information. There are two main system tables which give you such information: dbo.sysobjects dbo.syscolumns Example below shows how to gather column list &#8230; <a href="http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you dealing with dynamic SQL or unknown table structure, SQL meta data could help recover the missing information.</p>
<p>There are two main system tables which give you such information:</p>
<ul>
<li>dbo.sysobjects</li>
<li>dbo.syscolumns</li>
</ul>
<p>Example below shows how to gather column list for specified table.</p>
<pre class="brush: sql; title: ; notranslate">DECLARE
  @TABLENAME varchar(256),
  @COLUMNS varchar(4000)
SET @TABLENAME = 'mytable'
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + c.name + ', '
FROM syscolumns c
INNER JOIN sysobjects o ON o.id = c.id
WHERE o.name = @TABLENAME AND o.xtype='U'
ORDER BY colid
SET @COLUMNS = SUBSTRING(@COLUMNS, 1, Datalength(@COLUMNS) - 2)
SELECT @COLUMNS</pre>
<p>or if you prefer more generic approach</p>
<pre class="brush: sql; title: ; notranslate">DECLARE
  @TABLENAME varchar(256),
  @COLUMNS varchar(4000)
SET @TABLENAME = 'mytable'
SET @COLUMNS = ''
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TABLENAME
ORDER BY ORDINAL_POSITION
SET @COLUMNS = SUBSTRING(@COLUMNS, 1, Datalength(@COLUMNS) - 2)
SELECT @COLUMNS</pre>
<p>Result is a coma-separated list of columns in the table in order they appear.</p>
<p><strong>Note</strong>. Check for xtype in the first example could be useful but not required, since you probably do not mix table name with any other object names. Otherwise, it would be necessary to ensure that you are looking at the table and not something else.<br />
Other possible values for this column are:</p>
<ul>
<li><strong>S</strong> &#8211; System tables</li>
<li><strong>U</strong> &#8211; User table</li>
<li><strong>TR</strong> &#8211; Triggers</li>
<li><strong>P</strong> &#8211; Stored procedure</li>
<li><strong>RF</strong> &#8211; Replication filter stored procedure</li>
<li><strong>X</strong> &#8211; Extended stored procedure</li>
<li><strong>V</strong> &#8211; View</li>
<li><strong>TF</strong> &#8211; Functions</li>
<li><strong>C</strong> &#8211; CHECK constraint</li>
<li><strong>D</strong> &#8211; DEFAULT constraint</li>
<li><strong>F</strong> &#8211; FOREIGN KEY constraint</li>
<li><strong>PK</strong> &#8211; PRIMARY KEY constraint (type is K)</li>
<li><strong>UQ</strong> &#8211; UNIQUE constraint (type is K)</li>
<li><strong>L</strong> &#8211; Log</li>
</ul>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2008/05/12/tip-get-list-of-columns-for-the-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

