<?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>SQL Slayer &#187; SQL SERVER 2008</title>
	<atom:link href="http://www.sqlslayer.com/wp/tag/sql-server-2008/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlslayer.com/wp</link>
	<description>Making SQL do what we want it to do.</description>
	<lastBuildDate>Mon, 12 Dec 2011 13:53:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GROUP BY CUBE example</title>
		<link>http://www.sqlslayer.com/wp/2009/10/08/group-by-cube-example/</link>
		<comments>http://www.sqlslayer.com/wp/2009/10/08/group-by-cube-example/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:27:10 +0000</pubDate>
		<dc:creator>mlakarj</dc:creator>
				<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[GROUP BY CUBE]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false">http://www.sqlslayer.com/wp/?p=114</guid>
		<description><![CDATA[<p>The GROUP BY CUBE statement is pretty sweet.  Here’s an example.</p>
<p>First, let’s create our table and add data:</p>
<p>IF  EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[GroupByCubeExample]')
AND type in (N'U'))
	DROP TABLE [dbo].[GroupByCubeExample]
GO</p>
<p>CREATE TABLE [dbo].[GroupByCubeExample](
	[Region] [nvarchar](50) NOT NULL,
	[Product] [nvarchar](50) NOT NULL,
	[Salesman] [nvarchar](50) NOT NULL,
	[Amount] [money] NOT NULL
)
GO</p>
<p>INSERT INTO dbo.GroupByCubeExample
(Region,Product,Salesman,Amount)
SELECT 'America','Bike','Bean, Adam',10
UNION ALL SELECT 'America','Bike','Stanford, Matt',20
UNION [...]]]></description>
			<content:encoded><![CDATA[<p>The GROUP BY CUBE statement is pretty sweet.  Here’s an example.</p>
<p>First, let’s create our table and add data:</p>
<blockquote><p><code>IF  EXISTS (SELECT * FROM sys.objects<br />
WHERE object_id = OBJECT_ID(N'[dbo].[GroupByCubeExample]')<br />
AND type in (N'U'))<br />
	DROP TABLE [dbo].[GroupByCubeExample]<br />
GO</p>
<p>CREATE TABLE [dbo].[GroupByCubeExample](<br />
	[Region] [nvarchar](50) NOT NULL,<br />
	[Product] [nvarchar](50) NOT NULL,<br />
	[Salesman] [nvarchar](50) NOT NULL,<br />
	[Amount] [money] NOT NULL<br />
)<br />
GO</p>
<p>INSERT INTO dbo.GroupByCubeExample<br />
(Region,Product,Salesman,Amount)<br />
SELECT 'America','Bike','Bean, Adam',10<br />
UNION ALL SELECT 'America','Bike','Stanford, Matt',20<br />
UNION ALL SELECT 'America','Car','Bean, Adam',10000.0000<br />
UNION ALL SELECT 'America','Car','Stanford, Matt',20000.0000<br />
UNION ALL SELECT 'Canada','Bike','Bean, Adam',10<br />
UNION ALL SELECT 'Canada','Bike','Stanford, Matt',20<br />
UNION ALL SELECT 'Canada','Car','Bean, Adam',10000.0000<br />
UNION ALL SELECT 'Canada','Car','Stanford, Matt',20000.0000</code></p></blockquote>
<p>Then, we can SUM using GROUP BY CUBE for some nice automatic rollups.</p>
<blockquote><p><code>SELECT<br />
	 Region<br />
	,Product<br />
	,Salesman<br />
	,SUM(Amount) AS Amount<br />
FROM dbo.GroupByCubeExample<br />
GROUP BY CUBE(<br />
	 Region<br />
	,Product<br />
	,Salesman<br />
)<br />
ORDER BY Region,Product,Salesman</code></p></blockquote>
<p>Results (NULLs mean ALL):</p>
<blockquote><p><code>Region        Product    Salesman          Amount<br />
------------- ---------- ----------------- ----------<br />
NULL          NULL       NULL              60060.00<br />
NULL          NULL       Bean, Adam        20020.00<br />
NULL          NULL       Stanford, Matt    40040.00<br />
NULL          Bike       NULL              60.00<br />
NULL          Bike       Bean, Adam        20.00<br />
NULL          Bike       Stanford, Matt    40.00<br />
NULL          Car        NULL              60000.00<br />
NULL          Car        Bean, Adam        20000.00<br />
NULL          Car        Stanford, Matt    40000.00<br />
America       NULL       NULL              30030.00<br />
America       NULL       Bean, Adam        10010.00<br />
America       NULL       Stanford, Matt    20020.00<br />
America       Bike       NULL              30.00<br />
America       Bike       Bean, Adam        10.00<br />
America       Bike       Stanford, Matt    20.00<br />
America       Car        NULL              30000.00<br />
America       Car        Bean, Adam        10000.00<br />
America       Car        Stanford, Matt    20000.00<br />
Canada        NULL       NULL              30030.00<br />
Canada        NULL       Bean, Adam        10010.00<br />
Canada        NULL       Stanford, Matt    20020.00<br />
Canada        Bike       NULL              30.00<br />
Canada        Bike       Bean, Adam        10.00<br />
Canada        Bike       Stanford, Matt    20.00<br />
Canada        Car        NULL              30000.00<br />
Canada        Car        Bean, Adam        10000.00<br />
Canada        Car        Stanford, Matt    20000.00</p>
<p>(27 row(s) affected)</code></p></blockquote>
<p>And if you don’t like the NULLs, you can use a WHEN GROUPING() statement to better format:</p>
<blockquote><p><code>SELECT<br />
 CASE<br />
	WHEN GROUPING(Region) = 1<br />
		THEN 'ALL REGIONS'<br />
	ELSE Region<br />
 END AS Region<br />
,CASE<br />
	WHEN GROUPING(Product) = 1<br />
		THEN 'ALL PRODUCTS'<br />
	ELSE Product<br />
 END AS Product<br />
,CASE<br />
	WHEN GROUPING(Salesman) = 1<br />
		THEN 'ALL SALESMAN'<br />
	ELSE Salesman<br />
 END AS Salesman<br />
,SUM(Amount) AS Amount<br />
FROM dbo.GroupByCubeExample<br />
GROUP BY CUBE(<br />
	 Region<br />
	,Product<br />
	,Salesman<br />
)<br />
ORDER BY Region,Product,Salesman</code></p></blockquote>
<p>Results:</p>
<blockquote><p><code>Region         Product        Salesman        Amount<br />
-------------- -------------- --------------- ----------<br />
ALL REGIONS    ALL PRODUCTS   ALL SALESMAN    60060.00<br />
ALL REGIONS    ALL PRODUCTS   Bean, Adam      20020.00<br />
ALL REGIONS    ALL PRODUCTS   Stanford, Matt  40040.00<br />
ALL REGIONS    Bike           ALL SALESMAN    60.00<br />
ALL REGIONS    Bike           Bean, Adam      20.00<br />
ALL REGIONS    Bike           Stanford, Matt  40.00<br />
ALL REGIONS    Car            ALL SALESMAN    60000.00<br />
ALL REGIONS    Car            Bean, Adam      20000.00<br />
ALL REGIONS    Car            Stanford, Matt  40000.00<br />
America        ALL PRODUCTS   ALL SALESMAN    30030.00<br />
America        ALL PRODUCTS   Bean, Adam      10010.00<br />
America        ALL PRODUCTS   Stanford, Matt  20020.00<br />
America        Bike           ALL SALESMAN    30.00<br />
America        Bike           Bean, Adam      10.00<br />
America        Bike           Stanford, Matt  20.00<br />
America        Car            ALL SALESMAN    30000.00<br />
America        Car            Bean, Adam      10000.00<br />
America        Car            Stanford, Matt  20000.00<br />
Canada         ALL PRODUCTS   ALL SALESMAN    30030.00<br />
Canada         ALL PRODUCTS   Bean, Adam      10010.00<br />
Canada         ALL PRODUCTS   Stanford, Matt  20020.00<br />
Canada         Bike           ALL SALESMAN    30.00<br />
Canada         Bike           Bean, Adam      10.00<br />
Canada         Bike           Stanford, Matt  20.00<br />
Canada         Car            ALL SALESMAN    30000.00<br />
Canada         Car            Bean, Adam      10000.00<br />
Canada         Car            Stanford, Matt  20000.00</p>
<p>(27 row(s) affected)</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlslayer.com/wp/2009/10/08/group-by-cube-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>&#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created.&#8221;</title>
		<link>http://www.sqlslayer.com/wp/2009/10/06/saving-changes-is-not-permitted-the-changes-you-have-made-require-the-following-tables-to-be-dropped-and-re-created/</link>
		<comments>http://www.sqlslayer.com/wp/2009/10/06/saving-changes-is-not-permitted-the-changes-you-have-made-require-the-following-tables-to-be-dropped-and-re-created/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 19:30:52 +0000</pubDate>
		<dc:creator>mlakarj</dc:creator>
				<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>
		<category><![CDATA[“Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created.”]]></category>

		<guid isPermaLink="false">http://www.sqlslayer.com/wp/?p=75</guid>
		<description><![CDATA[<p>Management Studio 2008 has a default that prevents saving table changes from the designer when the table will need to be rebuilt.  I’m constantly being asked about this error by developers:</p>
<p> &#8221;Saving changes is not permitted.  The changes you have made require the following tables to be dropped and re-created.&#8221;</p>
<p> To be able to make table changes [...]]]></description>
			<content:encoded><![CDATA[<p>Management Studio 2008 has a default that prevents saving table changes from the designer when the table will need to be rebuilt.  I’m constantly being asked about this error by developers:</p>
<p> &#8221;Saving changes is not permitted.  The changes you have made require the following tables to be dropped and re-created.&#8221;</p>
<p> To be able to make table changes go to Tools-&gt;Options-&gt;Designers and uncheck “Prevent saving changes that require table re-creation”.</p>
<p> It’s a good idea to do this as soon as you install 2008.  Tell everyone around you.  I’ve been asked about it at least half a dozen times, as recent as a few days ago.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlslayer.com/wp/2009/10/06/saving-changes-is-not-permitted-the-changes-you-have-made-require-the-following-tables-to-be-dropped-and-re-created/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

