<?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>Eric Fickes &#187; #table</title>
	<atom:link href="http://ericfickes.com/tag/table/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericfickes.com</link>
	<description>Design minded Internet Programmer</description>
	<lastBuildDate>Fri, 28 Oct 2011 04:14:43 +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>Incorrect syntax near the keyword &#8216;table&#8217; in TSQL</title>
		<link>http://ericfickes.com/2010/04/incorrect-syntax-near-the-keyword-table-in-tsql/</link>
		<comments>http://ericfickes.com/2010/04/incorrect-syntax-near-the-keyword-table-in-tsql/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 22:18:21 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[#table]]></category>
		<category><![CDATA[DECLARE]]></category>
		<category><![CDATA[SQLSERVER]]></category>
		<category><![CDATA[SQLSERVER 2005]]></category>
		<category><![CDATA[table variable]]></category>

		<guid isPermaLink="false">http://ericfickes.com/2010/04/incorrect-syntax-near-the-keyword-table-in-tsql/</guid>
		<description><![CDATA[Ran into something little that I know I&#8217;m going to forget if I don&#8217;t write down. It appears that when using a TABLE variable in tsql ( SQL Server 2005 ), you must DECLARE that variable on it&#8217;s own line, &#8230; <a href="http://ericfickes.com/2010/04/incorrect-syntax-near-the-keyword-table-in-tsql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ran into something little that I know I&#8217;m going to forget if I don&#8217;t write down.  It appears that when using a TABLE variable in tsql ( SQL Server 2005 ), you must DECLARE that variable on it&#8217;s own line, as opposed to inline with your other @variables.</p>
<p>Typically in my sprocs or sql scripts I do my best to have a main DECLARE block and seperate my @variables with a comma like this.</p>
<div id="attachment_1412" class="wp-caption aligncenter" style="width: 378px"><a href="http://ericfickes.com/wp-content/uploads/2010/04/declare-bad.png" rel="lightbox[1414]"><img class="size-full wp-image-1412" title="Incorrect syntax near the keyword 'table'" src="http://ericfickes.com/wp-content/uploads/2010/04/declare-bad.png" alt="Typically I DECLARE=" width="368" height="125" /></a><p class="wp-caption-text">If you&#39;re using a TABLE variable, put it on it&#39;s own DECLARE line</p></div>
<p>After some mucking around, it turns out moving the TABLE @variable to it&#8217;s own DECLARE line fixes this issue.</p>
<div id="attachment_1413" class="wp-caption aligncenter" style="width: 304px"><a href="http://ericfickes.com/wp-content/uploads/2010/04/declare-good.png" rel="lightbox[1414]"><img class="size-full wp-image-1413" title="DECLARE TABLE @variables on their own line" src="http://ericfickes.com/wp-content/uploads/2010/04/declare-good.png" alt="DECLARE TABLE @variables on their own line" width="294" height="106" /></a><p class="wp-caption-text">DECLARE TABLE @variables on their own line</p></div>
<p>I haven&#8217;t found this info in SQL BOL, so I hope this helps somebody else.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/04/incorrect-syntax-near-the-keyword-table-in-tsql/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Select random value from a range of values</title>
		<link>http://ericfickes.com/2010/01/select-random-value-from-a-range-of-values/</link>
		<comments>http://ericfickes.com/2010/01/select-random-value-from-a-range-of-values/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 01:08:52 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[#table]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mssql2000]]></category>
		<category><![CDATA[mssql2005]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[table variable]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1020</guid>
		<description><![CDATA[Earlier I blogged about creating random numbers using tsql functions.  Here are two techniques for selecting a random value from a pre-defined range of values in a tsql script.  The first technique uses a table variable ( MSSQL 2000 + &#8230; <a href="http://ericfickes.com/2010/01/select-random-value-from-a-range-of-values/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Earlier I blogged about <a title="TSQL UDFs for generating random numbers" href="http://ericfickes.com/2009/09/generate-random-integers-using-tsql-udfs/" target="_blank">creating random numbers using tsql functions</a>.  Here are two techniques for selecting a random value from a pre-defined range of values in a tsql script.  The first technique uses a <a title="MSSQL 2000 let's you create table variables" href="http://msdn.microsoft.com/en-us/library/aa260638%28SQL.80%29.aspx" target="_blank">table variable</a> ( MSSQL 2000 + ), and the second uses a <a title="CTEs in MSSQL 2005 let you build queries a little differently" href="http://technet.microsoft.com/en-us/library/ms175972%28SQL.90%29.aspx" target="_blank">Common Table Expression</a> or CTE ( MSSQL 2005+ ).</p>
<h3>Select a random value using a table variable</h3>
<pre class="brush: sql; title: ; notranslate">

-- var to hold random integer
declare @field_val int

-- create table var to hold value range [ 0, 512, 1024, 2048, 4096 ]
-- inserting the first value sets the structure for the table variable
SELECT 0 AS 'num'
INTO #temp

-- insert data into table var
INSERT INTO #temp VALUES ( 512 )
INSERT INTO #temp VALUES ( 1024 )
INSERT INTO #temp VALUES ( 2048 )
INSERT INTO #temp VALUES ( 4096 )

-- assign random value
SELECT TOP 1 @field_val = num FROM #temp ORDER BY NEWID()

-- show value
SELECT @field_val

-- drop the table variable
DROP TABLE #temp
</pre>
<h3>Select a random value using a CTE</h3>
<pre class="brush: sql; title: ; notranslate">
-- define our data table
WITH data( car )
AS
(
	-- UNION together our range of values
	SELECT 'audi' AS 'car'
	UNION
	SELECT 'bmw' AS 'car'
	UNION
	SELECT 'infinity' AS 'car'
	UNION
	SELECT 'lexus' AS 'car'
	UNION
	SELECT 'porsche' AS 'car'
)
-- select a random value
SELECT TOP 1 car FROM data
ORDER BY NEWID()
</pre>
<p>Both of these techniques can be used with numbers or text.  Just be sure to mind your quotes, and variable datatypes.  Being able to pick a random value in data generation scripts has proven very useful.  I hope this helps somebody else out as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/01/select-random-value-from-a-range-of-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

