<?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>Codepolice.net &#187; SQL</title>
	<atom:link href="http://codepolice.net/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://codepolice.net</link>
	<description>C#, ASP.NET, MVC, LINQ, Wordpress and stuff like that</description>
	<lastBuildDate>Mon, 26 Jul 2010 08:58:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>IN queries with LINQ to SQL</title>
		<link>http://codepolice.net/2010/06/01/in-queries-with-linq-to-sql/</link>
		<comments>http://codepolice.net/2010/06/01/in-queries-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 13:33:12 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[in]]></category>
		<category><![CDATA[Linq to sql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[string array]]></category>

		<guid isPermaLink="false">http://codepolice.net/?p=277</guid>
		<description><![CDATA[This is another awesome feature in LINQ to SQL that i always forget about. Sometimes you have an array of strings or any other type and want to query the database for all values that have one of the values in the array. string platform = "windows&#124;linux"; string[] platformList = platform.Split('&#124;'); itemQuery = from m [...]]]></description>
			<content:encoded><![CDATA[<p>This is another awesome feature in LINQ to SQL that i always forget about. Sometimes you have an array of strings or any other type and want to query the database for all values that have one of the values in the array.</p>
<pre class="brush:c#">string platform = "windows|linux";
string[] platformList = platform.Split('|');
itemQuery = from m in itemQuery
from p in m.PlattformItemRelations
where platformList.Contains(p.Plattform.Text.ToLower())
select m;</pre>
<p>Ain&#8217;t that a beauty? I got this tip from <a href="http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql/">Ron Connery</a> so all the kudos to him.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/2010/06/01/in-queries-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Return random rows from a MS SQL database</title>
		<link>http://codepolice.net/2009/11/17/return-random-rows-from-a-ms-sql-database/</link>
		<comments>http://codepolice.net/2009/11/17/return-random-rows-from-a-ms-sql-database/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 12:13:36 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Microsoft SQL]]></category>
		<category><![CDATA[ms sql]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>

		<guid isPermaLink="false">http://codepolice.net/?p=194</guid>
		<description><![CDATA[I always forget this command: SELECT * FROM YourTable TABLESAMPLE SYSTEM (10 PERCENT) This gives you some sample data from your database. In this case 10 percent of the content.  You could also use TABLESAMPLE SYSTEM (1000 ROWS) to get 1000 sample rows. Great to use if you have a big database and do not [...]]]></description>
			<content:encoded><![CDATA[<p>I always forget this command:</p>
<p>SELECT * FROM YourTable<strong> TABLESAMPLE SYSTEM (10 PERCENT)</strong></p>
<p>This gives you some sample data from your database. In this case 10 percent of the content.  You could also use <strong>TABLESAMPLE SYSTEM (1000 ROWS) </strong>to get 1000 sample rows. Great to use if you have a big database and do not want the same data again and again when you debug or something.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/2009/11/17/return-random-rows-from-a-ms-sql-database/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SQL Server: Saving changes is not permitted because table will be re-created</title>
		<link>http://codepolice.net/2009/09/10/sql-server-saving-changes-is-not-permitted-because-table-will-be-re-created/</link>
		<comments>http://codepolice.net/2009/09/10/sql-server-saving-changes-is-not-permitted-because-table-will-be-re-created/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 09:52:35 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[management-studio]]></category>
		<category><![CDATA[re-create]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://codepolice.net/?p=168</guid>
		<description><![CDATA[Microsoft has added a setting to Management Studio to prevent us from re-create tables when we make changes to them. I don&#8217;t like this settings and it would have be much better if they said &#8216;This table will be re-created, are you sure you want to do that?&#8221; instead of just stop us from doing [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft has added a setting to Management Studio to prevent us from re-create tables when we make changes to them. I don&#8217;t like this settings and it would have be much better if they said &#8216;This table will be re-created, are you sure you want to do that?&#8221; instead of just stop us from doing it.</p>
<p>There is an option to turn this off if you go to &#8220;Tools -&gt; Options -&gt; Designers -&gt; Table and Database Designers&#8221; and the uncheck the checkbox &#8220;Prevent saving changes that require table re-creation&#8221;.</p>
<p><a href="http://codepolice.net/wp-content/uploads/2009/09/sql_option.png"><img class="aligncenter size-full wp-image-169" title="sql_option" src="http://codepolice.net/wp-content/uploads/2009/09/sql_option.png" alt="sql_option" width="483" height="290" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/2009/09/10/sql-server-saving-changes-is-not-permitted-because-table-will-be-re-created/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET membership: System.Web.Security. SqlRoleProvider problems</title>
		<link>http://codepolice.net/2008/12/16/aspnet-membership-systemwebsecuritysqlroleprovider-problems/</link>
		<comments>http://codepolice.net/2008/12/16/aspnet-membership-systemwebsecuritysqlroleprovider-problems/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 21:18:46 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[ASP.NET Membership]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[System.Web.Security.SqlRoleProvider]]></category>

		<guid isPermaLink="false">http://www.codepolice.net/?p=100</guid>
		<description><![CDATA[I had this really weird problem just now. I have done some work with the ASP.NET Membership provider and everything has been working great. But when i tried to deploy my application to the server i got this: The &#8216;System.Web.Security.SqlRoleProvider&#8217; requires a database schema compatible with schema version &#8217;1&#8242;. However, the current database schema is [...]]]></description>
			<content:encoded><![CDATA[<p>I had this really weird problem just now. I have done some work with the ASP.NET Membership provider and everything has been working great. But when i tried to deploy my application to the server i got this:</p>
<p><em>The &#8216;System.Web.Security.SqlRoleProvider&#8217; requires a database schema compatible with schema version &#8217;1&#8242;.  However, the current database schema is not compatible with this version.  You may need to either install a compatible schema with aspnet_reqsql.exe (available in the framework installation directory), or upgrade the provider to a newer version.</em></p>
<p>Alright, my database was exactly the same and everything looked fine. I tried to recreate my database with the aspnet_regsql.exe tool but nothing worked. Then i finally found this <a href="http://66.129.67.4/t/1314909.aspx">thread on the MSDN messageboard</a> and one guy said that he gave his application a name in the aspnet_Applications table and put that name in the web.config like this.</p>
<div class="codecolorer-container xml twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">connectionStringName</span>=<span style="color: #ff0000;">&quot;ApplicationServices&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">applicationName</span>=<span style="color: #ff0000;">&quot;APPLICATION NAME&quot;</span> </span><br />
<span style="color: #009900;"><span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AspNetSqlRoleProvider&quot;</span> </span><br />
<span style="color: #009900;"><span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>And that solved it. Really really strange and i have no idea if this is a bug or whatever. On my local machine (Vista with IIS7) it worked great to have a application named &#8220;/&#8221; but on my production machin (a Server 2003 with IIS6) it did not.</p>
<p>Now i should stop working for today.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/2008/12/16/aspnet-membership-systemwebsecuritysqlroleprovider-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Join and Group By with LINQ to Entities</title>
		<link>http://codepolice.net/2008/11/25/join-and-group-by-linq-to-entities/</link>
		<comments>http://codepolice.net/2008/11/25/join-and-group-by-linq-to-entities/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 15:21:55 +0000</pubDate>
		<dc:creator>Ola</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[Inner Joins]]></category>
		<category><![CDATA[Join]]></category>
		<category><![CDATA[Joins]]></category>
		<category><![CDATA[Left Outer Joins]]></category>
		<category><![CDATA[Linq to entities]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.codepolice.net/?p=67</guid>
		<description><![CDATA[I have banged my head against some LINQ to Entities stuff today. It started out when i was doing a query to get some stats out of my forum. I should point out that i&#8217;m a newbe with LINQ to Entities and the Entity Framework so if i have done anything stupid here you are [...]]]></description>
			<content:encoded><![CDATA[<p>I have banged my head against some LINQ to Entities stuff today. It started out when i was doing a query to get some stats out of my forum. I should point out that i&#8217;m a newbe with LINQ to Entities and the Entity Framework so if i have done anything stupid here you are free to mock me in the comments.</p>
<div class="codecolorer-container csharp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var query <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> s <span style="color: #0600FF; font-weight: bold;">in</span> db<span style="color: #008000;">.</span><span style="color: #0000FF;">ForumStatsSet</span><br />
<span style="color: #0600FF; font-weight: bold;">where</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">LogDate</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> date1 <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">LogDate</span> <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> date2<br />
group s by <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #008000;">&#123;</span><br />
&nbsp; s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">topicID</span>, s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">subject</span>,<br />
&nbsp; s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">datum</span>, s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Forum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumID</span>,<br />
&nbsp; s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Forum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumName</span>,<br />
&nbsp; s<span style="color: #008000;">.</span><span style="color: #0000FF;">Topic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Forum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ForumGroup</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span> <span style="color: #008000;">&#125;</span> into g<br />
orderby g<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> descending<br />
<span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TopicStatsData<br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; TopicId <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">topicID</span>,<br />
&nbsp; Count <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; Subject <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">subject</span>,<br />
&nbsp; ForumId <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumID</span>,<br />
&nbsp; ForumName <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumName</span>,<br />
&nbsp; ForumGroupName <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This code looked fine to me. It&#8217;s kind of massive and a lot of joins but it should be used in a admin interface so i did not care that much. But it turned out that it timeout every time in ran it. I had a look at the SQL that was generated and that was not a pretty sight. A massive amount of sub queries and left outer joins.</p>
<p>I <a href="http://stackoverflow.com/questions/317281/optimze-group-by-in-linq-to-entities">posted a question</a> on the new and really really good code community <a href="http://stackoverflow.com/">Stackoverflow</a> and got the tip that i should join the tables before group by clue. And that is what i tried to do.</p>
<p>The problem was that i got this error all the time.</p>
<p><em>from s in db.ForumStatsSet<br />
from t in s.Topic</em></p>
<p><em>Error:<br />
An expression of type &#8216;WhoaAdmin.Models.Topic&#8217; is not allowed in a subsequent from clause in a query expression with source type &#8216;System.Data.Objects.ObjectQuery&lt;WhoaAdmin.Models.ForumStats&gt;&#8217;. Type inference failed in the call to &#8216;SelectMany&#8217;.</em></p>
<p>I tried to google that error and i got nothing. I realised that maybe you have to do the joins in the correct order and that was it and i ended up with this query.</p>
<div class="codecolorer-container csharp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var query <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> fg <span style="color: #0600FF; font-weight: bold;">in</span> db<span style="color: #008000;">.</span><span style="color: #0000FF;">ForumGroupSet</span><br />
<span style="color: #0600FF; font-weight: bold;">from</span> f <span style="color: #0600FF; font-weight: bold;">in</span> fg<span style="color: #008000;">.</span><span style="color: #0000FF;">Forums</span><br />
<span style="color: #0600FF; font-weight: bold;">from</span> t <span style="color: #0600FF; font-weight: bold;">in</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Topics</span><br />
<span style="color: #0600FF; font-weight: bold;">from</span> s <span style="color: #0600FF; font-weight: bold;">in</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">ForumStats</span><br />
<span style="color: #0600FF; font-weight: bold;">where</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">LogDate</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> date1 <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">LogDate</span> <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> date2<br />
group s by <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #008000;">&#123;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">topicID</span>,<br />
&nbsp; t<span style="color: #008000;">.</span><span style="color: #0000FF;">subject</span>,<br />
&nbsp; t<span style="color: #008000;">.</span><span style="color: #0000FF;">datum</span>,<br />
&nbsp; f<span style="color: #008000;">.</span><span style="color: #0000FF;">forumID</span>,<br />
&nbsp; f<span style="color: #008000;">.</span><span style="color: #0000FF;">forumName</span>,<br />
&nbsp; fg<span style="color: #008000;">.</span><span style="color: #0000FF;">name</span> <span style="color: #008000;">&#125;</span> into g<br />
orderby g<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> descending<br />
<span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TopicStatsData<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp;TopicId <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">topicID</span>,<br />
&nbsp;Count <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp;Subject <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">subject</span>,<br />
&nbsp;ForumId <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumID</span>,<br />
&nbsp;ForumName <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">forumName</span>,<br />
&nbsp;ForumGroupName <span style="color: #008000;">=</span> g<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The SQL i ended up with looks kind of awful but probably the SQL engine do something like this &#8220;under the hood&#8221; on regular SQL group by to. At least it is kind of fast. The big diffrence in the generated SQL is that it has replaced all &#8220;LEFT OUTER JOINS&#8221; with &#8220;INNER JOINS&#8221; and they are of course faster.</p>
<p>Please leave comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://codepolice.net/2008/11/25/join-and-group-by-linq-to-entities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->