<?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; mssql</title>
	<atom:link href="http://ericfickes.com/tag/mssql/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>What happens in EXEC, stays in EXEC. Lifespan of a MSSQL table variable</title>
		<link>http://ericfickes.com/2010/02/using-mssql-table-variables-with-exec-statements/</link>
		<comments>http://ericfickes.com/2010/02/using-mssql-table-variables-with-exec-statements/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 19:44:49 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mssql2000]]></category>
		<category><![CDATA[mssql2005]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[SELECT INTO]]></category>
		<category><![CDATA[table variable]]></category>
		<category><![CDATA[temp table]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1080</guid>
		<description><![CDATA[One of my all time favorite features of MSSQL 2005+ is being able to create table variables on the fly from SELECT statements. This isn&#8217;t a lesson in what table variables are, but here is an easy sample in case &#8230; <a href="http://ericfickes.com/2010/02/using-mssql-table-variables-with-exec-statements/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">One of my all time favorite features of MSSQL 2005+ is being able to create table variables on the fly from SELECT statements.  This isn&#8217;t a lesson in what table variables are, but here is an easy sample in case this is a new concept.</p>
<p style="text-align: left;">Running this query</p>
<pre class="brush: sql; title: ; notranslate">SELECT * INTO #myTableVar FROM YourTable</pre>
<p>Gives you a new table variable named myTableVar.  Table variables are scoped to the active connection, so running this will work.</p>
<pre class="brush: sql; title: ; notranslate">
// make table var
SELECT * INTO #myTableVar FROM YourTable
// show me the data
SELECT * FROM #myTableVar
// you can drop it if you wish
DROP TABLE #myTableVar
</pre>
<p>However, let&#8217;s say you have an aspx page or a sproc that runs this query.</p>
<pre class="brush: sql; title: ; notranslate">SELECT * INTO #myTableVar FROM YourTable</pre>
<p>You can not access myTableVar in a separate connection to the database because as soon as the first query&#8217;s connection closes, myTableVar gets dropped.     Here are a few other scenarios that also demonstrate the scoping of a table variable.</p>
<pre class="brush: sql; title: ; notranslate">
-- FAILS
EXEC ('SELECT * INTO #tmp FROM MyTable;');
-- #tmp does not exist
SELECT * FROM #tmp
</pre>
<div id="attachment_1084" class="wp-caption aligncenter" style="width: 404px"><a href="http://ericfickes.com/wp-content/uploads/2010/02/exec1-no-tmp-table.png" rel="lightbox[1080]"><img class="size-full wp-image-1084      " title="Table variable lives inside of EXEC" src="http://ericfickes.com/wp-content/uploads/2010/02/exec1-no-tmp-table.png" alt="#tmp only exists inside of EXEC" width="394" height="179" /></a><p class="wp-caption-text">Table variable #tmp lives inside of EXEC</p></div>
<p>Here we see that the table variable #tmp only lives for the life of the statement inside of EXEC.  The second SELECT * calls is outside of the EXEC statement.</p>
<pre class="brush: sql; title: ; notranslate">
-- #tmp2 works inside of EXEC statement
EXEC ('SELECT * INTO #tmp2 FROM MyTable; SELECT * FROM #tmp2');
</pre>
<div id="attachment_1086" class="wp-caption aligncenter" style="width: 570px"><a href="http://ericfickes.com/wp-content/uploads/2010/02/exec2-tmp-inside-exec.png" rel="lightbox[1080]"><img class="size-full wp-image-1086   " title="tmp2 lives inside of EXEC" src="http://ericfickes.com/wp-content/uploads/2010/02/exec2-tmp-inside-exec.png" alt="table variables in EXEC live in EXEC" width="560" height="111" /></a><p class="wp-caption-text">What happens in EXEC, stays in EXEC</p></div>
<p>Here #tmp2 works because it&#8217;s being used inside of the EXEC statement.  This is worth knowing if you work with dynamic sql statements and exec.</p>
<pre class="brush: sql; title: ; notranslate">
-- works!
SELECT * INTO #tmp FROM MyTable;
-- #tmp exists
SELECT * FROM #tmp
</pre>
<div id="attachment_1087" class="wp-caption aligncenter" style="width: 318px"><a href="http://ericfickes.com/wp-content/uploads/2010/02/exec3-normal-tmp-exists.png" rel="lightbox[1080]"><img class="size-full wp-image-1087" title="typical sample of using mssql table variable" src="http://ericfickes.com/wp-content/uploads/2010/02/exec3-normal-tmp-exists.png" alt="typical sample of using mssql table variable" width="308" height="153" /></a><p class="wp-caption-text">typical sample of using mssql table variable</p></div>
<p>This is a typical example that you may use inside a sproc, trigger, script, etc.  Both sql calls live in the same space, so #tmp exists.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/02/using-mssql-table-variables-with-exec-statements/feed/</wfw:commentRss>
		<slash:comments>1</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>
		<item>
		<title>Store your SQL database in the same location as live</title>
		<link>http://ericfickes.com/2009/03/store-your-sql-database-in-the-same-location-as-live/</link>
		<comments>http://ericfickes.com/2009/03/store-your-sql-database-in-the-same-location-as-live/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 06:25:25 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[bak]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[paths]]></category>
		<category><![CDATA[restore database]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=520</guid>
		<description><![CDATA[I do a lot of moving of databases between development and production servers.  If I&#8217;m lucky, the production server I&#8217;m working with gives me access to DTS services, or even the Database publishing wizard.  More often than not however, the &#8230; <a href="http://ericfickes.com/2009/03/store-your-sql-database-in-the-same-location-as-live/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I do a lot of moving of databases between development and production servers.  If I&#8217;m lucky, the production server I&#8217;m working with gives me access to DTS services, or even the Database publishing wizard.  More often than not however, the SQLServer I&#8217;m pushing to is locked down in a way that I am required to Remote Desktop into the server, then update the database via restore.</p>
<p>So here&#8217;s the tip.  When you&#8217;re only means of updating a remote SQLServer database is by physically restoring the database, do your best to mirror the product server&#8217;s database location on your development machine.  Check out this screenshot, and you&#8217;ll see what I mean</p>
<div id="attachment_521" class="wp-caption aligncenter" style="width: 626px"><a href="http://ericfickes.com/wp-content/uploads/2009/03/mssql-backup-tip.png" rel="lightbox[520]"><img class="size-full wp-image-521" title="mssql-backup-tip" src="http://ericfickes.com/wp-content/uploads/2009/03/mssql-backup-tip.png" alt="MSSQL : Restore Files and Filegroups" width="616" height="471" /></a><p class="wp-caption-text">MSSQL : Restore Files and Filegroups</p></div>
<p>The background of this image shows that my production server houses all of it&#8217;s databases at the path <strong>C:\DB\database.mdf</strong>.</p>
<p>The lower right box shows where I keep my databases on my development server<strong>.</strong> Since I do not store any vital data on my C: drive, I&#8217;ve changed the path to <strong>D:\DB</strong>.</p>
<p>While this isn&#8217;t an exact path match, this trick saves me a little bit of time and frustration when restoring a database remotely.  Especially when I have to do it more than a handful of times in the same day.</p>
<p>It&#8217;s assumed you know how to the following  :</p>
<ul>
<li>Back up a SQLServer database</li>
<li>Copy the BAK file to a remote server</li>
<li>Connect remotely to your SQLServer</li>
<li>Restore a Database file from a file ( BAK file )</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2009/03/store-your-sql-database-in-the-same-location-as-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I built a calendar in a tSQL SPROC</title>
		<link>http://ericfickes.com/2008/10/i-built-a-calendar-in-a-tsql-sproc/</link>
		<comments>http://ericfickes.com/2008/10/i-built-a-calendar-in-a-tsql-sproc/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 02:48:25 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[sproc]]></category>
		<category><![CDATA[stored procedure]]></category>
		<category><![CDATA[table variables]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[tsql date functions]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=20</guid>
		<description><![CDATA[Here&#8217;s a blast from the past that I recently found in my archives. It&#8217;s a novelty stored procedure I wrote during my MS SQL 2000 DBA days. Back when I wrote this sproc, I was really big into writing calendar &#8230; <a href="http://ericfickes.com/2008/10/i-built-a-calendar-in-a-tsql-sproc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a blast from the past that I recently found in my archives.  It&#8217;s a novelty stored procedure I wrote during my MS SQL 2000 DBA days.<br />
Back when I wrote this sproc, I was really big into writing calendar applications.  I have written some sort of calendar application in<br />
almost every language I know, so writing one in tSql made sense to me.</p>
<p>While I never used this sproc in an application, or had any practical use for it, I still think it&#8217;s cool.  It&#8217;s primarily an excercise using<br />
tSql&#8217;s date functions, and my all time favorite feature of MS SQL 2000+, table variables.</p>
<p>If you use MSSQL 2000 or higher and don&#8217;t use table variables, I highly recommend looking into these.  In a nutshell, it&#8217;s a type of tSql variable<br />
that is a table.  You can select, insert, update, and delete the rows in this variable just like it&#8217;s a real table.  The lifespan of a table variable<br />
is the length of your connection to your db.  So if you have a table var #myTable in sprocA, as soon as sprocA completes execution, #myTable is gone.<br />
SprocB can&#8217;t access #myTable unless is specifically creates a new table var by this name.</p>
<p>So I wrote an article about this sproc for a database site years ago and I haven&#8217;t been able to find it again.  This was web1.0 days, so I&#8217;m sure the site<br />
is gone by now.  The good thing is I still have the sproc, and now you can to.</p>
<p>So here&#8217;s the info.  The sproc efCalendar accepts a month number and year number, and spits out two recordsets.</p>
<ol>
<li>Month, Year</li>
<li>Calendar view of that month</li>
</ol>
<p>Recordset 1 is two columns, month name, and year.</p>
<p>Recordset 2 is a calendar view of the specified month.  There is a column for each weekday, starting with Sunday and ending with Saturday.<br />
Then there is a row for each week in the specified month.</p>
<p>Here is what the results look like when run in Query Analyzer.</p>
<div id="attachment_19" class="wp-caption aligncenter" style="width: 509px"><a href="http://ericfickes.com/wp-content/uploads/2008/10/sproc-efcalendar.gif" rel="lightbox[20]"><img class="size-full wp-image-19" title="sproc-efcalendar" src="http://ericfickes.com/wp-content/uploads/2008/10/sproc-efcalendar.gif" alt="tSQL calendar sproc" width="499" height="370" /></a><p class="wp-caption-text">tSQL calendar sproc</p></div>
<p><a title="eric fickes loves mssql's table variables and sprocs" href="http://ericfickes.com/wp-content/uploads/2008/10/create_procedure_efcalendar.sql" target="download tsql calendar stored procedure">Download the efCalendar sproc here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2008/10/i-built-a-calendar-in-a-tsql-sproc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create comma seperated list out of a sql query ( tsql )</title>
		<link>http://ericfickes.com/2006/06/create-comma-seperated-list-out-of-a-tsql-query-tsql/</link>
		<comments>http://ericfickes.com/2006/06/create-comma-seperated-list-out-of-a-tsql-query-tsql/#comments</comments>
		<pubDate>Thu, 22 Jun 2006 14:50:00 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[coalesce]]></category>
		<category><![CDATA[microsoft sql server]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://ericfickes.com/2006/06/22/76/</guid>
		<description><![CDATA[Surfing the net for a sql answer, I came across something really cool. Below is an example of how to create a comma separated list of values in a single query. The results should look something like this]]></description>
			<content:encoded><![CDATA[<p>Surfing the net for a sql answer, I came across something really cool. Below is an example of how to create a comma separated list of values in a single query.</p>
<pre class="brush: sql; title: ; notranslate">
–declare holder var
DECLARE @list VARCHAR(8000)

–build comma separated list
SELECT @list = COALESCE(@list + ‘, ‘, ”) + CAST(track_id AS VARCHAR(5) )
FROM webtool_tracks

–show results
SELECT @list AS ‘list’
</pre>
<p><span style="color: #000000;">The results should look something like this</span><br />
<a href="http://photos1.blogger.com/blogger/1587/58/1600/coalesce.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" rel="lightbox[76]"><img style="FLOAT: left; MARGIN: 0pt 10px 10px 0pt; CURSOR: pointer" src="http://photos1.blogger.com/blogger/1587/58/320/coalesce.jpg" border="0" alt="" /></a><br />
<span style="color: #ff0000;"><br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2006/06/create-comma-seperated-list-out-of-a-tsql-query-tsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

