<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-AU">
    <title>.NET Playground</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/dotNETPlayground/Atom.aspx" />
    <subtitle type="html">unearthing the fun in .net and sql server</subtitle>
    <id>http://geekswithblogs.net/dotNETPlayground/Default.aspx</id>
    <author>
        <name>Andrew den Hertog</name>
        <uri>http://geekswithblogs.net/dotNETPlayground/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2009-10-13T23:57:51Z</updated>
    <entry>
        <title>blog moved</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2009/10/13/135453.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2009/10/13/135453.aspx</id>
        <published>2009-10-13T23:57:51-05:00:00</published>
        <updated>2009-10-13T23:57:51Z</updated>
        <content type="html">&lt;font face="Arial"&gt;http://adenhertog.blogspot.com/&lt;/font&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/135453.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Reporting Services (SSRS) - Get data value into page header/footer</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/11/06/126655.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/11/06/126655.aspx</id>
        <published>2008-11-06T11:46:15-06:00:00</published>
        <updated>2008-11-06T11:46:15Z</updated>
        <content type="html">&lt;p&gt;In reporting services (ssrs), there are times where you need to get a value from a data source and put it into your page header/footer. The catch here is that ssrs does not allow you to access data sources within your header/footer. This is one way around it.&lt;/p&gt;
&lt;p&gt;First the easy but inflexible way.&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Create a textbox in the body of the report &amp;amp; set the value to what you want appearing in your header/footer&lt;/li&gt;
    &lt;li&gt;Set the name of your textbox to txtVarStore&lt;/li&gt;
    &lt;li&gt;In the properties of this textbox, set Visibility -&amp;gt; Hidden to True (ie: hide the textbox)&lt;/li&gt;
    &lt;li&gt;Create a textbox in your header/footer &amp;amp; set the value to the expression: "&lt;font face="Courier New"&gt;=ReportItems!txtVarStore.Value&lt;/font&gt;"&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This will set the value of the textbox in your header/footer; but will only do it for the page that the hidden textbox is on.&lt;/p&gt;
&lt;p&gt;The second way is a bit more involved, but persists across pages:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Go to Properties of your report&lt;/li&gt;
    &lt;li&gt;Go to the Code tab&lt;/li&gt;
    &lt;li&gt;Create a new public shared variable for your value, ie:
    &lt;ol&gt;
        &lt;li&gt;&lt;font face="Courier New"&gt;Public Shared Dim MyVar as String&lt;/font&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;Create a new function to set the value of this variable, ie:
    &lt;ol&gt;
        &lt;li&gt;&lt;font face="Courier New"&gt;Public Function SetMyVar ( ByVar var as String)&lt;/font&gt;&lt;/li&gt;
        &lt;li&gt;&lt;font face="Courier New"&gt;   MyVar = var&lt;/font&gt;&lt;/li&gt;
        &lt;li&gt;&lt;font face="Courier New"&gt;End Function&lt;/font&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;Create a new textbox in the body of your report &amp;amp; set the value to the expression: &lt;font size="1"&gt;&lt;span lang="ZH"&gt; &lt;font size="2" face="Courier New"&gt;"=Code.SetMyVar(First(Fields!colVar.Value, "DataSourceName"))"&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;Create a new textbox in your header/footer, set the value to the expression: &lt;font face="Courier New"&gt;"=Code.MyVar"&lt;/font&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What this does is set up a new static variable (shared variable for you VB ppl), which persists once across the entire report. In SSRS, the body content gets generated first, which is why we can set the static variable using the SetMyVar function.&lt;/p&gt;
&lt;p&gt;When it comes time for the header/footer to render, it simply reads out the value of the static variable. &lt;/p&gt;
&lt;p&gt;Bit of a sleazy hack, but such are the quirks of SSRS&lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/126655.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Google Chrome - Pros &amp; Cons</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/09/03/124876.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/09/03/124876.aspx</id>
        <published>2008-09-03T12:26:26-05:00:00</published>
        <updated>2008-09-03T12:26:26Z</updated>
        <content type="html">&lt;p&gt;&lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt;, Safari, iExplorer, Firefox, and now Chrome. Like we didn't have enough mainstream browsers as it was. Google's attempt to grab a slice of yet another market has now been realised, but is their new Chrome offereing enough to get people to switch?&lt;/p&gt;
&lt;p&gt;I've finally had time to put Chrome through its paces on some of the sites I've created &amp;amp; maintained, which are all ajax-based, css standardised, and javascript heavy. As a devout &lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt; 10 user, it's next to impossible to get me to switch browsers, but will Chrome have enough shine to move me? &lt;/p&gt;
&lt;p&gt;To summarise:&lt;/p&gt;
&lt;p&gt;Pros&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Compiled Javascript - all the sites performed much faster&lt;/li&gt;
    &lt;li&gt;Site sandboxing - runaway scripts can be killed without having to crash the browser&lt;/li&gt;
    &lt;li&gt;Plugins - I'm against plug-ins in general (bar small amounts of flash and firebug), but allowing them to run in their own process space sounds like a good thing&lt;/li&gt;
    &lt;li&gt;UI look &amp;amp; feel - plus big points for minimalistic approach&lt;/li&gt;
    &lt;li&gt;Open source&lt;/li&gt;
    &lt;li&gt;Built-in DOM inspector&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;"Most Visited" sites home page - Browse histories don't do it for me. I need a programmable &amp;amp; static set of icons (Opera!)&lt;/li&gt;
    &lt;li&gt;No mouse gestures - again, maybe it's because &lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt;'s spoiled me, but without mouse gestures, old-school 'point and click' is plain slow&lt;/li&gt;
    &lt;li&gt;"Beta" - fair enough it's just been released, but is there anything that ever makes it past beta from google (gmail anyone)? Perhaps they should just register the "beta" thing as a trademark and be done with it&lt;/li&gt;
    &lt;li&gt;No Synrchonisation - This was something that got released with the new build of &lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt; a few months ago. It lets you log into your browser, and have it sync your bookmarks et al regardless of where you are or which computer you're on. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Overall&lt;/p&gt;
&lt;p&gt;Very nice attempt, if I was coming from IE or Safari, or possibly even firefox chances are I'd convert (though if I was a firefox fanboy, I'd stick with FF out of principal). Chrome beats all contenders when it comes to observable performance, plus the internal wiring's more advanced. However, when it comes to useability, it just doesn't size up to &lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Since Chrome's open-source, I'm sure it's just a matter of time until the community creates enough plug-ins to mimic the UI smarts built in to &lt;a title="" href="http://geekswithblogs.net/dotNETPlayground/archive/2007/11/27/117134.aspx" rel=""&gt;Opera&lt;/a&gt;; but until then, I'm willing to sacrifice a couple of seconds of site performance &amp;amp; load time, to the incredible amount of time saved with navigation. &lt;/p&gt;
&lt;p&gt;Nice going Google, but I'll wait for the RC!&lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/124876.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Free Silverlight and WPF goodness for all!</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/08/15/124441.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/08/15/124441.aspx</id>
        <published>2008-08-15T07:58:56-05:00:00</published>
        <updated>2008-08-15T08:00:27Z</updated>
        <content type="html">&lt;p&gt;WpfStyles.com (&lt;a href="http://www.wpfstyles.com/"&gt;http://www.wpfstyles.com&lt;/a&gt;) launched the other day (hooray!). The site lets WPF and Silverlight developers and designers upload their components and share them with the community.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.wpfstyles.com"&gt;&lt;img alt="WpfStyles.com" width="385" height="125" src="/images/geekswithblogs_net/dotNETPlayground/Logo.gif" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;It's all free, so not only are you able to share your components, but you can also download those that other likeminded developers have put up there. &lt;/p&gt;
&lt;p&gt;If you have any such components that the community will find useful, throw them up there. I urge you to get behind this resource and show your support. &lt;img alt="" src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;&lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/124441.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Importing CSV flat files in SSIS - dealing with Double Quotes</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/08/14/124430.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/08/14/124430.aspx</id>
        <published>2008-08-14T12:57:37-05:00:00</published>
        <updated>2008-08-14T12:57:37Z</updated>
        <content type="html">&lt;p&gt;I've gone through a number of phases in SSIS. The first was fascination, the next was anger, then came love, and now I'm somewhere in the middle. One major flaw I've noticed is how SSIS handles double quotes (") in imported flat file data, or should I say how it doesn't handle it at all?&lt;/p&gt;
&lt;p&gt;Simple scenario, I have a *csv file that I'm importing to a db table. The flat file is all text delimited using double quotes. A number of the fields contain literal double quotes, which in any CSV text delmited implementation you would normally escape by using another double quote delimeter. To illustrate:&lt;/p&gt;
&lt;p&gt;This: &lt;/p&gt;
&lt;p&gt;he cried out "SSIS!" in frustration&lt;/p&gt;
&lt;p&gt;Becomes This:&lt;/p&gt;
&lt;p&gt;"he cried out ""SSIS!"" in frustration"&lt;/p&gt;
&lt;p&gt;Simple, standard csv behaviour, parses fine in excel.&lt;/p&gt;
&lt;p&gt;Try importing to SSIS and your import will crash out, and if you try to preview that row you'll get: "T&lt;font face="Arial"&gt;he flat file parser does not support embedding text qualifiers in data"&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Solution? The only way I've found to get around this is to write your own escape functionality. I piggy-backed off html encoding and replaced literal quotes with &amp;amp;quot;  , and then used derived columns with &lt;font face="Arial"&gt;REPLACE(ImportedField,"&amp;amp;quot;","\"") to change them back prior to putting them in their destination table.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I hate having to write work arounds like this, especially for functionality which should be part of the underlying core system. If anyone has a better way, please drop your comments in below. &lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/124430.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Sql Server - How to split time into n minute groups</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/04/29/121735.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/04/29/121735.aspx</id>
        <published>2008-04-29T13:30:11-05:00:00</published>
        <updated>2008-04-29T13:30:37Z</updated>
        <content type="html">&lt;p&gt;Another little data manipulation I've had to do recently is to split a heap of sql server datetime values into 15 minute segments (eg: 10:00, 10:15, 10:30 etc). Although there's no inherit function that I know about in sql server that achieves this, there's a nifty little trick using the modulus operator (%) that can do this.&lt;/p&gt;
&lt;div style="FONT-SIZE: 9pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: aqua"&gt;select&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;case &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;when datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;hour&lt;/span&gt;, DateTimeval) &amp;lt; &lt;span style="COLOR: yellow"&gt;12 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;then &lt;/span&gt;&lt;span style="COLOR: red"&gt;'AM' &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;else &lt;/span&gt;&lt;span style="COLOR: red"&gt;'PM'&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;end as &lt;/span&gt;Meridiem,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;Datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;hour&lt;/span&gt;, DateTimeval) &lt;span style="COLOR: aqua"&gt;as Hour&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;case &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;when &lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;minute&lt;/span&gt;, DateTimeval) % &lt;span style="COLOR: yellow"&gt;15 &lt;/span&gt;) &amp;lt; (&lt;span style="COLOR: yellow"&gt;15 &lt;/span&gt;/ &lt;span style="COLOR: yellow"&gt;2&lt;/span&gt;) &lt;span style="COLOR: aqua"&gt;then datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;minute&lt;/span&gt;, DateTimeval) - (&lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;minute&lt;/span&gt;, DateTimeval) % &lt;span style="COLOR: yellow"&gt;15&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;else &lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;minute&lt;/span&gt;, DateTimeval) + (&lt;span style="COLOR: yellow"&gt;15 &lt;/span&gt;- (&lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;minute&lt;/span&gt;, DateTimeval) % &lt;span style="COLOR: yellow"&gt;15&lt;/span&gt;))) % &lt;span style="COLOR: yellow"&gt;60&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;end as &lt;/span&gt;QuarterHour&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: aqua"&gt;from&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   MyTable&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Where DateTimeval is the column that holds the... date time value.&lt;/p&gt;
&lt;p&gt;Firstly the &amp;lt; (15/2) part determines the range of the midpoints (ie: the midpoint 15 ranges from 7.5 to 22.5).  Next it gets the minutes part from the value (eg: 10, 49, 15, etc), then does % 15. This will give the remainders (10, 04, 00). If the remainder is less that the range value (ie: &amp;lt; (15/2)), then by simply subtracting the remainder from the original value will give us the midpoint (eg: N/A, 49 - 4 = 45, 15 - 0 = 15). &lt;/p&gt;
&lt;p&gt;If, however, the remainder is &amp;gt; (15 / 2), then we need to add the difference of (15 - remainder) to get the upper midpoint value (eg: 10 + (15 - 10) = 5, 45, 15).&lt;/p&gt;
&lt;p&gt;And voila, the date time values are segmented into 15 minute chunks.&lt;/p&gt;
&lt;p&gt;Of course you can substitute 15 with however many minutes  you want to segment by (eg: 5, 10, 30 etc). &lt;/p&gt;
&lt;p&gt;Performance has been Ok. I ran this over 200,000 records and it took about 3 seconds on a shared dev server. Not bad.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/121735.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Generating List of Dates</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/04/28/121696.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/04/28/121696.aspx</id>
        <published>2008-04-28T10:15:05-05:00:00</published>
        <updated>2008-04-28T10:16:09Z</updated>
        <content type="html">&lt;p&gt;At times, especially when working with cubes with date/time dimensions, it's necessary for me to dynamically generate a list of date values for use in those dimensions. That single date dimension often services a number of cubes, and the range of date values can be determined from the min/max date values from serveral tables. &lt;/p&gt;
&lt;p&gt;I've written the following SQL that will retrieve the min/max date values, and create a single record for every day in that min/max range. It's useful to turn this sql into a view which will refresh itself automatically every time the cube calling it is processed.  &lt;/p&gt;
&lt;div style="FONT-SIZE: 9pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: aqua"&gt;select&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(dw, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   + &lt;span style="COLOR: red"&gt;', ' &lt;/span&gt;+ &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(mm, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   + &lt;span style="COLOR: red"&gt;' ' &lt;/span&gt;+ &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(dd, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   + &lt;span style="COLOR: red"&gt;' ' &lt;/span&gt;+ &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(yy, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) [datename],&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: red"&gt;'Fiscal ' &lt;/span&gt;+ &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(yy, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) fiscalyearname,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(mm, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   + &lt;span style="COLOR: red"&gt;', ' &lt;/span&gt;+ &lt;span style="COLOR: aqua"&gt;datename&lt;/span&gt;(yy, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) fiscalmonthname,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;day&lt;/span&gt;, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) &lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;dayNumber,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;month&lt;/span&gt;, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) &lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;monthNumber,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   &lt;span style="COLOR: aqua"&gt;datepart&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;year&lt;/span&gt;, &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) &lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;yearNumber&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: aqua"&gt;from&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;   (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;select&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;dateadd&lt;/span&gt;(&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;day&lt;/span&gt;, &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            n1.val * &lt;span style="COLOR: yellow"&gt;10000 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            n2.val * &lt;span style="COLOR: yellow"&gt;1000 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            n3.val * &lt;span style="COLOR: yellow"&gt;100 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            n4.val * &lt;span style="COLOR: yellow"&gt;10 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            n5.val,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;cast&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;floor&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;cast&lt;/span&gt;(mindate &lt;span style="COLOR: aqua"&gt;as decimal&lt;/span&gt;(&lt;span style="COLOR: yellow"&gt;12&lt;/span&gt;,&lt;span style="COLOR: yellow"&gt;5&lt;/span&gt;))) &lt;span style="COLOR: aqua"&gt;as datetime&lt;/span&gt;)) &lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;from&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;0 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;val&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;1 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;2 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;3 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;4 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;5 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;6 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;7 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;8 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;9&lt;/span&gt;) n1,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;0 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;val&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;1 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;2 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;3 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;4 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;5 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;6 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;7 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;8 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;9&lt;/span&gt;) n2,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;0 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;val&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;1 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;2 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;3 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;4 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;5 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;6 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;7 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;8 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;9&lt;/span&gt;) n3,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;0 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;val&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;1 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;2 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;3 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;4 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;5 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;6 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;7 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;8 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;9&lt;/span&gt;) n4,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;0 &lt;/span&gt;&lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;val&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;1 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;2 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;3 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;4 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;5 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;6 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;7 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;8 &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all select &lt;/span&gt;&lt;span style="COLOR: yellow"&gt;9&lt;/span&gt;) n5,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;min&lt;/span&gt;(minDate) &lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;minDate, &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;max&lt;/span&gt;(maxDate) &lt;span style="COLOR: aqua"&gt;as &lt;/span&gt;maxDate &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;from &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            (&lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &lt;span style="COLOR: aqua"&gt;min&lt;/span&gt;(date) minDate, &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &lt;span style="COLOR: aqua"&gt;max&lt;/span&gt;(date) maxdate&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;from &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &amp;lt;Data table 1&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;union all&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;select &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &lt;span style="COLOR: aqua"&gt;min&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;), &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &lt;span style="COLOR: aqua"&gt;max&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;date&lt;/span&gt;) &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;from &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;               &amp;lt;Data table 2&amp;gt;) dateVals&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;where &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;year&lt;/span&gt;(mindate) &amp;gt;= &lt;span style="COLOR: yellow"&gt;1980&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: aqua"&gt;and year&lt;/span&gt;(maxdate) &amp;lt;= &lt;span style="COLOR: aqua"&gt;dateadd&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;year&lt;/span&gt;, &lt;span style="COLOR: yellow"&gt;1&lt;/span&gt;, &lt;span style="COLOR: aqua"&gt;getdate&lt;/span&gt;())&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            ) c&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      &lt;span style="COLOR: aqua"&gt;where&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;dateadd&lt;/span&gt;(&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;day&lt;/span&gt;, &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        n1.val * &lt;span style="COLOR: yellow"&gt;10000 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        n2.val * &lt;span style="COLOR: yellow"&gt;1000 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        n3.val * &lt;span style="COLOR: yellow"&gt;100 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        n4.val * &lt;span style="COLOR: yellow"&gt;10 &lt;/span&gt;+&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        n5.val,&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: aqua"&gt;cast&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;floor&lt;/span&gt;(&lt;span style="COLOR: aqua"&gt;cast&lt;/span&gt;(mindate &lt;span style="COLOR: aqua"&gt;as decimal&lt;/span&gt;(&lt;span style="COLOR: yellow"&gt;12&lt;/span&gt;,&lt;span style="COLOR: yellow"&gt;5&lt;/span&gt;))) &lt;span style="COLOR: aqua"&gt;as datetime&lt;/span&gt;)) &amp;lt;= maxDate&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;      ) dateVals&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/121696.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Automatically Create Install Scripts with Sql Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/04/18/121331.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/04/18/121331.aspx</id>
        <published>2008-04-18T12:11:53-05:00:00</published>
        <updated>2008-04-20T13:16:17Z</updated>
        <content type="html">&lt;p&gt;Problem: How can I &lt;strong&gt;QUICKLY&lt;/strong&gt; create an install script automatically in sql server every time I change my database? Creating an install script in sql server is pretty easy thanks to the "Script Database As..." command. However, this is generally a long winded process that doesn't give you too much control. &lt;/p&gt;
&lt;p&gt;In this post, I propose a solution to create build scripts with the following requirements&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Don't need to write any code &lt;/li&gt;
    &lt;li&gt;Easily maintainable &lt;/li&gt;
    &lt;li&gt;Can script schema AND data &lt;/li&gt;
    &lt;li&gt;Can specify which objects to script &lt;/li&gt;
    &lt;li&gt;Can run automatically before an MSI setup/deployment project is built &lt;/li&gt;
    &lt;li&gt;It has to be FREE&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first thing you need to do is download Microsoft's Database Publishing Wizard (&lt;font face="Arial"&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&amp;amp;displaylang=en&lt;/a&gt;&lt;/font&gt;)&lt;/p&gt;
&lt;p&gt;Next, in your development sql server, add two new accounts named: ScriptSchema and ScriptData. Make sure they are created as a sql login. At this point, don't give them any other access than public access to the database you want to script.&lt;/p&gt;
&lt;p&gt;Open the database you want to script. For each table/view/stored proc etc that you want to include in the install, grant view definition access to the ScriptSchema user as follows:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;grant view definition on &amp;lt;schema&amp;gt;.&amp;lt;object name&amp;gt; to scriptschema&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Next, for each table that you want to script the data for, grant select permission on that table to the ScriptData user, ie:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;grant select on &amp;lt;schema&amp;gt;.&amp;lt;table name&amp;gt; to scriptdata&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;You can double up, so for instance if you want to script the creation and population of data of a certain table called &lt;font face="Courier New"&gt;[dbo].[customers]&lt;/font&gt; you'd run the two commands:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;grant view definition on [dbo].[customers] to scriptschema&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;grant select on [dbo].[customers] to scriptdata&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Once you've set up the permissions, you can check out what will be scripted by connecting to the database as the scriptschema and scriptdata users. As you've now defined what will be scripted, it's time to do the scripting itself.&lt;/p&gt;
&lt;p&gt;Jump out into a cmd.exe, and run the two commands; &lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;sqlpubwiz script -C "data source=[server]; initial catalog=[database]; uid=scriptschema; pwd=password;" schema.sql -schemaonly -f&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;sqlpubwiz script -C "data source=[server]; initial catalog=[database]; uid=scriptdata; pwd=password;" data.sql -dataonly -f&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The first command connects to the database using the scriptschema account, and generates the definition of all the objects it can see, ie: those which you gave grant view definition access to.&lt;/p&gt;
&lt;p&gt;Likewise the second command connects to teh database using the scriptdata account and generates the scripts to insert the data, based on all the tables it can select from.&lt;/p&gt;
&lt;p&gt;You now have two scripts: schema.sql, data.sql. You can add these to your msi/setup projects in visual studio (outside the scope of this post). Furthermore you can then add the two commands mentioned to the pre-build events of your setup's resource project to build the install scripts prior to the MSI etc being built. &lt;/p&gt;
&lt;p&gt;I really like this solution to automatically generate sql install scripts. I feel it's elegant, there's no code to be maintained, and no manual updating of sql scripts every time you change the database. &lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/121331.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Get date without time in Sql Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/03/31/120874.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/03/31/120874.aspx</id>
        <published>2008-03-31T11:45:33-05:00:00</published>
        <updated>2008-03-31T11:45:33Z</updated>
        <content type="html">&lt;p&gt;There are times when you need to write a query joining on dates, that is the day of a date, but not worry about the time portion. Without using the between x and y, as sometimes this isn't possible from places like SSAS Data source views, what's the most efficient way to remove the time part of a datetime?&lt;/p&gt;
&lt;p&gt;There are a number of ways to drop off the time part, as shown here:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;select CONVERT(datetime, CONVERT(varchar(10), c.date, 111), 111) from dbo.phonecalls c&lt;/font&gt; &lt;br /&gt;
go &lt;/font&gt;&lt;/p&gt;
&lt;div&gt;&lt;font size="+0"&gt;&lt;font size="2"&gt;&lt;span class="301115200-31032008"&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff0000"&gt;select cast(CAST(YEAR(c.date) AS VARCHAR(4)) + '/' +&lt;br /&gt;
           CAST(MONTH(c.date) AS VARCHAR(2)) + '/' +&lt;br /&gt;
           CAST(DAY(c.date) AS VARCHAR(2)) as datetime) from dbo.phonecalls c&lt;/font&gt;&lt;br /&gt;
           &lt;br /&gt;
go&lt;br /&gt;
&lt;font color="#008000"&gt;select CAST(FLOOR(CAST(c.date AS DECIMAL(12, 5))) AS DATETIME) from dbo.phonecalls c&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font size="+0"&gt;&lt;font face="Courier New" size="2"&gt;&lt;span class="301115200-31032008"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt; &lt;/div&gt;
&lt;div&gt;After clearing the query cache for the server (&lt;font size="+0"&gt;&lt;font face="Courier New" size="2"&gt;&lt;span class="301115200-31032008"&gt;dbcc freeproccache&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;) I executed the above in sequence, and got the following execution times:&lt;/div&gt;
&lt;div&gt; 
&lt;div&gt;&lt;font face="Arial" color="#0000ff" size="2"&gt;150871&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Arial" size="2"&gt;&lt;/font&gt; &lt;/div&gt;
&lt;div&gt;&lt;font face="Arial" color="#ff0000" size="2"&gt;186319&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Arial" size="2"&gt;&lt;/font&gt; &lt;/div&gt;
&lt;div&gt;&lt;font face="Arial" color="#008000" size="2"&gt;94119&lt;/font&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So it appears that casting the date to a 5 point decimal, dropping the decimal portion, and then casting back to a date time is by far the most efficient. I'm putting this down the fact that it's a completely numerical operation, and doesn't resort to casting to different value types (ie: varchar). &lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/120874.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>Sanitise your database input</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/dotNETPlayground/archive/2008/01/31/119104.aspx" />
        <id>http://geekswithblogs.net/dotNETPlayground/archive/2008/01/31/119104.aspx</id>
        <published>2008-01-31T08:13:42-06:00:00</published>
        <updated>2008-01-31T08:13:42Z</updated>
        <content type="html">&lt;p&gt;Saw this little strip today from &lt;a href="http://xkcd.com/"&gt;http://xkcd.com/&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;img height="205" alt="Courtesy of http://xkcd.com/" width="666" src="/images/geekswithblogs_net/dotNETPlayground/exploits_of_a_mom.png" /&gt;&lt;/p&gt;
&lt;p&gt;SQL injection's a nasty by-product of poor development. Anywhere a user can enter data, there's the potential for an exploit. In my opinion, performing input validation on the web/ui layer is repetitive and insecure. I've always taken the approach to move all that to the Data layer. &lt;/p&gt;
&lt;p&gt;Here resides a few exclusive set of methods that are the only ones that can interface with the database. Performing input checking here ensures that everything that goes to the database isn't going to cause any grief. Furthermore you can do it once and not have to worry about validating input from every new field you put on your display layer. &lt;/p&gt;
&lt;p&gt;Furthermore it's unecessary and bad practice to be generating sql in any of your layers except for the few times when it's necessary to gen dynamic sql in stored procs. Effectively if you design towards these sorts of goals, you can pretty much guarantee little Bobby tables won't be causing you any issues.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/dotNETPlayground/aggbug/119104.aspx" width="1" height="1" /&gt;</content>
    </entry>
</feed>