<rss version="2.0" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>SQL Server DBA </title>
        <link>http://community.dotnetwork.it/Excentric/category/72.aspx</link>
        <description>SQL Server DBA </description>
        <language>en-US</language>
        <copyright>Sabrina C.</copyright>
        <managingEditor>ziayeye@ziayeye.it</managingEditor>
        <generator>Subtext Version 1.9.5.176</generator>
        <item>
            <title>How to create a Guid.Empty in SQL</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2010/08/10/how-to-create-a-guid.empty-in-sql.aspx</link>
            <description>&lt;p&gt;Last friday my colleague Luca was “fighting” with a problem in managing null values and GUIDs. A Guid is a value type so it is not nullable. In my stored procedures when using parameters for filtering, I use the NULL value as the default value to ignore a filter parameter.&lt;/p&gt;  &lt;p&gt;Changing the code of several classes to be able to change a Guid.Empty into a DbNull before passing it to a SELECT command was decisely not the best thing to do, so we searched a way to recreate a Guid.Empty value in SQL.&lt;/p&gt;  &lt;p&gt;We were able to create the small snippet of SQL in the following function:&lt;/p&gt;  &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;FUNCTION&lt;/span&gt; Guid_Empty()
&lt;span class="kwrd"&gt;RETURNS&lt;/span&gt; UniqueIdentifier
&lt;span class="kwrd"&gt;AS&lt;/span&gt;
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
&lt;span class="kwrd"&gt;RETURN&lt;/span&gt; &lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="kwrd"&gt;cast&lt;/span&gt;(0 &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;binary&lt;/span&gt;) &lt;span class="kwrd"&gt;as&lt;/span&gt; uniqueidentifier)
&lt;span class="kwrd"&gt;END&lt;/span&gt;
GO&lt;/pre&gt;

&lt;p&gt;After the creation of the above function and a simple table on my Paperinik Database,  I was able to write the following query:&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; [ID]
      ,[Descrizione]
      ,[Prezzo]
      ,[Quantità]
      ,[Umi]
      ,IsNull(AltGuid, dbo.Guid_EMPTY()) &lt;span class="kwrd"&gt;AS&lt;/span&gt; Alt
  &lt;span class="kwrd"&gt;FROM&lt;/span&gt; [paperinik].[dbo].[TbRighe]&lt;/pre&gt;

&lt;p&gt;The query produces the following output:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://community.dotnetwork.it/images/community_dotnetwork_it/Excentric/WindowsLiveWriter/HowtocreateaGuid.EmptyinSQL_141CA/guidquery_2.jpg" rel="lightbox"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="guidquery" border="0" alt="guidquery" src="http://community.dotnetwork.it/images/community_dotnetwork_it/Excentric/WindowsLiveWriter/HowtocreateaGuid.EmptyinSQL_141CA/guidquery_thumb.jpg" width="644" height="104" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I can use it also to build a query like the one below:&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; [ID]
      ,[Descrizione]
      ,[Prezzo]
      ,[Quantità]
      ,[Umi]
      ,IsNull(AltGuid, dbo.Guid_EMPTY()) &lt;span class="kwrd"&gt;AS&lt;/span&gt; Alt
  &lt;span class="kwrd"&gt;FROM&lt;/span&gt; [paperinik].[dbo].[TbRighe]
&lt;span class="kwrd"&gt;WHERE&lt;/span&gt;
IsNull(AltGuid, dbo.Guid_EMPTY()) = dbo.Guid_EMPTY()&lt;/pre&gt;

&lt;p&gt;That allows me to filter only the Guid Empty rows, or I can use the &amp;lt;&amp;gt; to find only the rows with a non Empty Guid. This way of writing the query, is giving me the possibility to use Guid.Empty in my Code as a SQLParameter value without the need to use a Nullable Guid or to implement complicated control expressions in my C# classes.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:06fc812e-6fbb-411a-9fb5-c5c256bbe9e0" class="wlWriterEditableSmartContent"&gt;Technorati Tag: &lt;a href="http://technorati.com/tags/SQL" rel="tag"&gt;SQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Guid" rel="tag"&gt;Guid&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/484.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2010/08/10/how-to-create-a-guid.empty-in-sql.aspx</guid>
            <pubDate>Tue, 10 Aug 2010 20:53:20 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/484.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2010/08/10/how-to-create-a-guid.empty-in-sql.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/484.aspx</wfw:commentRss>
        </item>
        <item>
            <title>SQL Server Update a Table with values selected from another table</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2010/07/12/sql-server-update-a-table-with-values-selected-from-another.aspx</link>
            <description>&lt;p&gt;Today, on another step of the job started with the last post, I had the following problem, I created a table with some informations and I needed to add more informations taken from other tables.&lt;/p&gt;  &lt;p&gt;A an Example, I created an inventory table, with Article Code, Description, Quantity, to create it I used an INSERT query from the Articles table, then the users of my system inserted the Inventory values and now I need to add Sales Price and Cost of every article, reading them from the PriceList.&lt;/p&gt;  &lt;p&gt;For Simplicity my tables are as follows:&lt;/p&gt;  &lt;pre class="TsqlFormat"&gt;TbInventory
   IDInventory &lt;span class="kwrd"&gt;int&lt;/span&gt;
   IDArticle nvarchar(32)
   Quantity &lt;span class="kwrd"&gt;decimal&lt;/span&gt;(18,5)
   Price &lt;span class="kwrd"&gt;decimal&lt;/span&gt;(18,5)
   Cost &lt;span class="kwrd"&gt;decimal&lt;/span&gt;(18,5)

TbListini
   IDPricelist &lt;span class="kwrd"&gt;int&lt;/span&gt;
   IDarticle nvarchar(32)
   Price &lt;span class="kwrd"&gt;decimal&lt;/span&gt;(18,5)
   Cost &lt;span class="kwrd"&gt;decimal&lt;/span&gt; (18,5)&lt;/pre&gt;

&lt;p&gt;To update price and cost I’lluse, an Update query, with a Join.&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;UPDATE&lt;/span&gt; TbInventory
    &lt;span class="kwrd"&gt;SET&lt;/span&gt; Price = isnull(lis.Price,0)
      , Cost = isnull(lis.Cost,0)
&lt;span class="kwrd"&gt;FROM&lt;/span&gt; TbInventory inv
&lt;span class="kwrd"&gt;INNER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt;
    TbPriceList lis
&lt;span class="kwrd"&gt;ON&lt;/span&gt;
  inv.IDArticle = lis.IDArticle  &lt;/pre&gt;

&lt;p&gt;This way, I’m setting a relation between the two tables usin ga JOIN thenI update the Inventory Table with The Price List values.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:31d404cd-266b-40d3-9641-90ce40ff82aa" class="wlWriterEditableSmartContent"&gt;Tags: &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/SQL/default.aspx" rel="tag"&gt;SQL&lt;/a&gt;, &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/UPDATE/default.aspx" rel="tag"&gt;UPDATE&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/455.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2010/07/12/sql-server-update-a-table-with-values-selected-from-another.aspx</guid>
            <pubDate>Mon, 12 Jul 2010 16:40:51 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/455.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2010/07/12/sql-server-update-a-table-with-values-selected-from-another.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/455.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Use of Rank function to extract a list of rows with the highest date</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2010/07/07/use-of-rank-function-to-extract-a-list-of-rows.aspx</link>
            <description>&lt;p&gt;Today I had the problem to create a query to get the most recent price from a pricelists table. Every pricelist in the table can contain the same article any number of times with different startup dates. I made some search to find a way to obtain what i needed and I found the RANK function, it was used in a similar scenario. I publish this post as usual to remember how to use this function next time I need it and in the hope to help someone else solving similar problems.&lt;/p&gt;  &lt;p&gt;The table:&lt;/p&gt;  &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; [dbo].[TbPricelistsRg](
    [IDPriceListRg] [&lt;span class="kwrd"&gt;int&lt;/span&gt;] &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [IDPriceList] [nvarchar](10) &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [IDArticle] [nvarchar](32) &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [Price] [&lt;span class="kwrd"&gt;decimal&lt;/span&gt;](18, 5) &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [StartDate] [&lt;span class="kwrd"&gt;date&lt;/span&gt;] &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
 &lt;span class="kwrd"&gt;CONSTRAINT&lt;/span&gt; [PK_TbPriceListsRg] &lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt; &lt;span class="kwrd"&gt;KEY&lt;/span&gt; &lt;span class="kwrd"&gt;CLUSTERED&lt;/span&gt; 
(
    [IDPriceListRg] &lt;span class="kwrd"&gt;ASC&lt;/span&gt;
)&lt;span class="kwrd"&gt;WITH&lt;/span&gt; (PAD_INDEX  = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, STATISTICS_NORECOMPUTE  = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, IGNORE_DUP_KEY = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, ALLOW_ROW_LOCKS  &lt;br /&gt;     = &lt;span class="kwrd"&gt;ON&lt;/span&gt;, ALLOW_PAGE_LOCKS  = &lt;span class="kwrd"&gt;ON&lt;/span&gt;) &lt;span class="kwrd"&gt;ON&lt;/span&gt; [&lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt;]
) &lt;span class="kwrd"&gt;ON&lt;/span&gt; [&lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt;]&lt;/pre&gt;

&lt;p&gt;Every row is linked to a PriceList through the IDPriceLIst field, we don’t need to use it for the example so it is omitted.&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'ABC'&lt;/span&gt;
           ,10
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'OCQ'&lt;/span&gt;
           ,14
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'FYS'&lt;/span&gt;
           ,21
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'MUI'&lt;/span&gt;
           ,13
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'SIO'&lt;/span&gt;
           ,42
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'GOU'&lt;/span&gt;
           ,18
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [dbo].[TbPricelistsRg]
           ([IDPriceList]
           ,[IDArticle]
           ,[Price]
           ,[StartDate])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (&lt;span class="str"&gt;'CLD'&lt;/span&gt;
           ,&lt;span class="str"&gt;'HOU'&lt;/span&gt;
           ,20
           ,&lt;span class="kwrd"&gt;CONVERT&lt;/span&gt;(&lt;span class="kwrd"&gt;date&lt;/span&gt;, &lt;span class="str"&gt;'20080105'&lt;/span&gt;, 112))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The previous script inserts some articles, we can repeat it changing the PriceLIst, the Dates and prices to be able to show how the query meets our needs.&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; [IDPriceListRg]
      ,[IDPriceList]
      ,[IDArticle]
      ,[Price]
      ,[StartDate]
      ,RANK() &lt;span class="kwrd"&gt;OVER&lt;/span&gt; (PARTITION &lt;span class="kwrd"&gt;BY&lt;/span&gt; IDPriceList, IDArticle &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; StartDate &lt;span class="kwrd"&gt;DESC&lt;/span&gt;) DATERANK
  &lt;span class="kwrd"&gt;FROM&lt;/span&gt; [paperinik].[dbo].[TbPricelistsRg]&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;font color="#0080ff"&gt;RANK&lt;/font&gt;&lt;/strong&gt; function, allows us to indicate one or more grouping fields on which calculate ranking, this is done using the &lt;strong&gt;&lt;font color="#0080ff"&gt;PARTITION BY&lt;/font&gt;&lt;/strong&gt; keywords, in our query we indicate the two fields on which ranking has to break. The &lt;font color="#0080ff"&gt;&lt;strong&gt;ORDER BY&lt;/strong&gt;&lt;/font&gt;  keyword, instead indicates on which field we have to measure ranking. while the &lt;font color="#0080ff"&gt;&lt;strong&gt;DESC&lt;/strong&gt;&lt;/font&gt; keywords tells that we want to start from the most recent date..&lt;/p&gt;

&lt;p&gt;This is the result of our query.&lt;/p&gt;

&lt;table style="width: 296pt; border-collapse: collapse" border="0" cellspacing="0" cellpadding="0" width="395"&gt;&lt;colgroup&gt;&lt;col style="width: 48pt" width="64" /&gt;&lt;col style="width: 56pt; mso-width-source: userset; mso-width-alt: 2742" width="75" /&gt;&lt;col style="width: 48pt" width="64" /&gt;&lt;/colgroup&gt;&lt;tbody&gt;
    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="width: 48pt; height: 15pt" height="20" width="64"&gt;IDListinoRg&lt;/td&gt;

      &lt;td style="width: 48pt" width="64"&gt;IDListino&lt;/td&gt;

      &lt;td style="width: 48pt" width="64"&gt;IDArticolo&lt;/td&gt;

      &lt;td style="width: 48pt" width="64"&gt;Prezzo&lt;/td&gt;

      &lt;td style="width: 56pt" width="75"&gt;ValidoDal&lt;/td&gt;

      &lt;td style="width: 48pt" width="64"&gt;DATERANK&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;107&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;24/10/2010&lt;/td&gt;

      &lt;td align="right"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;86&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;15/05/2010&lt;/td&gt;

      &lt;td align="right"&gt;2&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;65&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/01/2010&lt;/td&gt;

      &lt;td align="right"&gt;3&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;44&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/09/2009&lt;/td&gt;

      &lt;td align="right"&gt;4&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;23&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;16/04/2009&lt;/td&gt;

      &lt;td align="right"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;1&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;05/01/2008&lt;/td&gt;

      &lt;td align="right"&gt;6&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;2&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;ABC&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;05/01/2008&lt;/td&gt;

      &lt;td align="right"&gt;6&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;109&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;24/10/2010&lt;/td&gt;

      &lt;td align="right"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;88&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;15/05/2010&lt;/td&gt;

      &lt;td align="right"&gt;2&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;67&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/01/2010&lt;/td&gt;

      &lt;td align="right"&gt;3&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;46&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/09/2009&lt;/td&gt;

      &lt;td align="right"&gt;4&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;25&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;16/04/2009&lt;/td&gt;

      &lt;td align="right"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;4&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;FYS&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.100.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;05/01/2008&lt;/td&gt;

      &lt;td align="right"&gt;6&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;112&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;24/10/2010&lt;/td&gt;

      &lt;td align="right"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;91&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;15/05/2010&lt;/td&gt;

      &lt;td align="right"&gt;2&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;70&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/01/2010&lt;/td&gt;

      &lt;td align="right"&gt;3&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;49&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/09/2009&lt;/td&gt;

      &lt;td align="right"&gt;4&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;28&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;16/04/2009&lt;/td&gt;

      &lt;td align="right"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;7&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;GOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;1.800.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;05/01/2008&lt;/td&gt;

      &lt;td align="right"&gt;6&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;113&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;24/10/2010&lt;/td&gt;

      &lt;td align="right"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;92&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;15/05/2010&lt;/td&gt;

      &lt;td align="right"&gt;2&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;71&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/01/2010&lt;/td&gt;

      &lt;td align="right"&gt;3&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;50&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;08/09/2009&lt;/td&gt;

      &lt;td align="right"&gt;4&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;29&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;16/04/2009&lt;/td&gt;

      &lt;td align="right"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr style="height: 15pt" height="20"&gt;
      &lt;td style="height: 15pt" height="20" align="right"&gt;8&lt;/td&gt;

      &lt;td&gt;CLD&lt;/td&gt;

      &lt;td&gt;HOU&lt;/td&gt;

      &lt;td class="xl63" align="right"&gt;2.000.000&lt;/td&gt;

      &lt;td class="xl64" align="right"&gt;05/01/2008&lt;/td&gt;

      &lt;td align="right"&gt;6&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;So to obtain the most recent prices, we need just to add a filter on DATERANK = 1, and to obtain the less recent price we just need to delete the DESC keyword and filter on rank 1.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:27b8a83b-f846-404d-ae0f-c3a2d70793b7" class="wlWriterEditableSmartContent"&gt;Technorati Tag: &lt;a href="http://technorati.com/tags/SQL" rel="tag"&gt;SQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/RANK" rel="tag"&gt;RANK&lt;/a&gt;,&lt;a href="http://technorati.com/tags/DATE" rel="tag"&gt;DATE&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/453.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2010/07/07/use-of-rank-function-to-extract-a-list-of-rows.aspx</guid>
            <pubDate>Wed, 07 Jul 2010 21:45:13 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/453.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2010/07/07/use-of-rank-function-to-extract-a-list-of-rows.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/453.aspx</wfw:commentRss>
        </item>
        <item>
            <title>FILESTREAM feature in SQL server 2008</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2010/03/04/filestream-feature-in-sql-server-2008.aspx</link>
            <description>&lt;p&gt;One of the most important new features in SQL Server 2008 is certainly the possibility to use File System to store BLOB data. So if you work with images, binary files and so on and you need to store them in a SQL server database, it is possible to use this feature. It is important for those who are using SQL Server Express edition, because it avoids the problem of the 4GB maximum size for the database giving virtually no limit to the BLOB data growth.&lt;/p&gt;  &lt;p&gt;Let’s see how to activate this feature on our server:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;First step is to activate the feature at server protocol level, so, from Programs menu &amp;gt; Microsoft SQL Server 2008 &amp;gt; Configuration Tools – launch the SQL Server Configuration Manager (icon with the red toolbox).&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Select SQL Services in the left treeview&lt;/li&gt;      &lt;li&gt;On the right window, select SQL Server (MSSQLSERVER) for the default instance or the name of the instance to configure.&lt;/li&gt;      &lt;li&gt;Right click on it, and select properties.&lt;/li&gt;      &lt;li&gt;On the property window there is a series of TABS, select FILESTREAM, activate the T-SQL access and if necessary the API access and the remote client access (I haven’t done a full test so I don’t know if this is necessary, so you have to make some tests on this.)&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;Second step is in SQL Management Studio, connect to the server with an account with Sysadmin rights, on the server, right click and select properties, select Advanced on the left options list and on the first row of the property grid on the right part of the window, activate the Filestream option in T-SQL mode or Full Access.&lt;/li&gt;    &lt;li&gt;Create a Database able to memorize data on FILESTREAM:&lt;/li&gt;    &lt;ul&gt;     &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; [master]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

/****** &lt;span class="kwrd"&gt;Object&lt;/span&gt;:  &lt;span class="kwrd"&gt;Database&lt;/span&gt; [Paperinik]    Script &lt;span class="kwrd"&gt;Date&lt;/span&gt;: 03/04/2010 18:09:07 ******/
&lt;span class="kwrd"&gt;IF&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;EXISTS&lt;/span&gt; (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; name &lt;span class="kwrd"&gt;FROM&lt;/span&gt; sys.databases &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; name = N&lt;span class="str"&gt;'Paperinik'&lt;/span&gt;)
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;DATABASE&lt;/span&gt; [Paperinik] &lt;span class="kwrd"&gt;ON&lt;/span&gt;  &lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt; 
( NAME = N&lt;span class="str"&gt;'Paperinik'&lt;/span&gt;, FILENAME = N&lt;span class="str"&gt;'C:\SQL.DIR\Data\Test\Paperinik.mdf'&lt;/span&gt; , &lt;br /&gt;    &lt;span class="kwrd"&gt;SIZE&lt;/span&gt; = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), 
 FILEGROUP [Binari] &lt;span class="kwrd"&gt;CONTAINS&lt;/span&gt; FILESTREAM  &lt;span class="kwrd"&gt;DEFAULT&lt;/span&gt; 
( NAME = N&lt;span class="str"&gt;'Paperinik_Bin'&lt;/span&gt;, FILENAME = N&lt;span class="str"&gt;'c:\sql.dir\data\test\PaperinikFS\paperinik_bin'&lt;/span&gt; )
 LOG &lt;span class="kwrd"&gt;ON&lt;/span&gt; 
( NAME = N&lt;span class="str"&gt;'Paperinik_log'&lt;/span&gt;, FILENAME = N&lt;span class="str"&gt;'C:\SQL.DIR\Data\Test\Paperinik_log.ldf'&lt;/span&gt; , &lt;br /&gt;   &lt;span class="kwrd"&gt;SIZE&lt;/span&gt; = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
END&lt;/pre&gt;
  &lt;/ul&gt;

  &lt;ul&gt;this example made with my favourite database :D creates the database with the Paperinik.mdf as master data file and the file Paperinik_log.ldf as a log file. Adding a FILEGROUP named Binari which defines the FILESTREAM zone, telling the server which is the filesystem folder where to put the data.  in my case, c:\sql.dir\data\test\PaperinikFS\ pay attention that the Service account needs full control on this folder.&lt;/ul&gt;

  &lt;li&gt;To proceed with test we need to create a table with a Varbinary(MAX) field with option FILESTREAM 
    &lt;ul&gt;
      &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; [Paperinik]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; [dbo].[TbFiles](
    [IDFile] [uniqueidentifier] &lt;span class="kwrd"&gt;ROWGUIDCOL&lt;/span&gt;  &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [IDFileSerial] [&lt;span class="kwrd"&gt;int&lt;/span&gt;] &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [DDFile] [nvarchar](255) &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
    [FileData] [varbinary](&lt;span class="kwrd"&gt;max&lt;/span&gt;) FILESTREAM  &lt;span class="kwrd"&gt;NULL&lt;/span&gt;,
 &lt;span class="kwrd"&gt;CONSTRAINT&lt;/span&gt; [PK_TbFiles] &lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt; &lt;span class="kwrd"&gt;KEY&lt;/span&gt; &lt;span class="kwrd"&gt;NONCLUSTERED&lt;/span&gt; 
(
    [IDFile] &lt;span class="kwrd"&gt;ASC&lt;/span&gt;
)&lt;span class="kwrd"&gt;WITH&lt;/span&gt; (PAD_INDEX  = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, STATISTICS_NORECOMPUTE  =     &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, 
    IGNORE_DUP_KEY = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, ALLOW_ROW_LOCKS  = &lt;span class="kwrd"&gt;ON&lt;/span&gt;,
    ALLOW_PAGE_LOCKS  = &lt;span class="kwrd"&gt;ON&lt;/span&gt;) &lt;span class="kwrd"&gt;ON&lt;/span&gt; [&lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt;]
) &lt;span class="kwrd"&gt;ON&lt;/span&gt; [&lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt;] FILESTREAM_ON [Binari]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;USE&lt;/span&gt; [Paperinik]
/****** &lt;span class="kwrd"&gt;Object&lt;/span&gt;:  &lt;span class="kwrd"&gt;Index&lt;/span&gt; [UQ_TbFileSerial]    Script &lt;span class="kwrd"&gt;Date&lt;/span&gt;: 03/04/2010 17:23:33 ******/
&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;UNIQUE&lt;/span&gt; &lt;span class="kwrd"&gt;CLUSTERED&lt;/span&gt; &lt;span class="kwrd"&gt;INDEX&lt;/span&gt; [UQ_TbFileSerial] &lt;span class="kwrd"&gt;ON&lt;/span&gt; [dbo].[TbFiles] 
(
    [IDFileSerial] &lt;span class="kwrd"&gt;ASC&lt;/span&gt;
)&lt;span class="kwrd"&gt;WITH&lt;/span&gt; (PAD_INDEX  = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, STATISTICS_NORECOMPUTE  = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, 
    SORT_IN_TEMPDB = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, IGNORE_DUP_KEY = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, 
    DROP_EXISTING = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, ONLINE = &lt;span class="kwrd"&gt;OFF&lt;/span&gt;, ALLOW_ROW_LOCKS  = &lt;span class="kwrd"&gt;ON&lt;/span&gt;, 
    ALLOW_PAGE_LOCKS  = &lt;span class="kwrd"&gt;ON&lt;/span&gt;) &lt;span class="kwrd"&gt;ON&lt;/span&gt; [&lt;span class="kwrd"&gt;PRIMARY&lt;/span&gt;] FILESTREAM_ON [Binari]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;&lt;/pre&gt;
In this example, I’ve created a table to memorize some files, even though in the example I put into the table something simpler, I will made some more test and let you know how it works later on this blog.

      &lt;br /&gt;To be able to contain a FILESTREAM field, it is mandatory that the table has a GUID (uniqueidentifier) ID field, to made it more easy I added an integer unique ID to be able to find things in a more fashionable way.

      &lt;br /&gt;&lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Now we need to insert the test data in the table, it is very simple using T-SQL:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [Paperinik].[dbo].[TbFiles]
           ([IDFile]
           ,[IDFileSerial]
           ,[DDFile]
           ,[FileData])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (NEWID()
           ,1
           ,&lt;span class="str"&gt;'Nullo'&lt;/span&gt;
           ,&lt;span class="kwrd"&gt;null&lt;/span&gt;)
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [Paperinik].[dbo].[TbFiles]
           ([IDFile]
           ,[IDFileSerial]
           ,[DDFile]
           ,[FileData])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (NEWID()
           ,2
           ,&lt;span class="str"&gt;'Vuoto'&lt;/span&gt;
           ,&lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="str"&gt;''&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; varbinary(&lt;span class="kwrd"&gt;max&lt;/span&gt;)))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [Paperinik].[dbo].[TbFiles]
           ([IDFile]
           ,[IDFileSerial]
           ,[DDFile]
           ,[FileData])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (NEWID()
           ,3
           ,&lt;span class="str"&gt;'Stringa'&lt;/span&gt;
           ,&lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="str"&gt;'Questa stringa è binaria'&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; varbinary(&lt;span class="kwrd"&gt;max&lt;/span&gt;)))
&lt;span class="kwrd"&gt;GO&lt;/span&gt;

&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; [Paperinik].[dbo].[TbFiles]
           ([IDFile]
           ,[IDFileSerial]
           ,[DDFile]
           ,[FileData])
     &lt;span class="kwrd"&gt;VALUES&lt;/span&gt;
           (NEWID()
           ,4
           ,&lt;span class="str"&gt;'La quarta'&lt;/span&gt;
           ,&lt;span class="kwrd"&gt;cast&lt;/span&gt;(&lt;span class="str"&gt;'Questa riga contiene questa stringa'&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; varbinary(&lt;span class="kwrd"&gt;max&lt;/span&gt;)))
GO&lt;/pre&gt;
I’ve created four rows one with a NULL binary field, one with an empty string and two with a string inside.&lt;/ul&gt;

&lt;p&gt;If we open our root folder after this operation on c:\sql.dir\data\test\PaperinikFS\ we will find some new folders, with a GUID as name, digging into the content we will find some files with names like nnnnnnnn-nnnnnnnn-nnnn that when opened with notepad will show the data in our varbinary field, the NULL field has no file. I haven’t yet tried to memorize a binary file inside this field, I’ll tell you what happens in a future post.&lt;/p&gt;

&lt;p&gt;What comes out in the end from this test? If we need to store unstructured data in a database, this can be a good solution, because SQL Server gives us transactions, logs, managed purge the possibility to activate indexing, I’ve to try Full text indexing on NON Sql Express servers, the possibility to backup these data as a normal file system directory, eliminates the 4GB database limit but pay attention, even though the BLOB data can be accessed from the file system it is not easy to find things inside this file system, it is very simple to find things and check out/check in binary data using the T-SQL or the direct acces APIs. So if you thought to use FILESYSTEM in SQL Server to be able to access data both from the database and directly from file system, this is not the right way.&lt;/p&gt;

&lt;p&gt;If you want to build a database to index informations on your non structured data and be able to keep files on the filesystem, and use them directly from where they are stored, well, you have to build something manual, and put just the “Path” info inside your database.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ec47414a-c36f-4f22-b45c-4a45b190793b" class="wlWriterEditableSmartContent"&gt;Tags: &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/SQL/default.aspx" rel="tag"&gt;SQL&lt;/a&gt;, &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/FILESTREAM/default.aspx" rel="tag"&gt;FILESTREAM&lt;/a&gt;, &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/SQLServer+2008/default.aspx" rel="tag"&gt;SQLServer 2008&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/373.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2010/03/04/filestream-feature-in-sql-server-2008.aspx</guid>
            <pubDate>Thu, 04 Mar 2010 18:19:20 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/373.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2010/03/04/filestream-feature-in-sql-server-2008.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/373.aspx</wfw:commentRss>
        </item>
        <item>
            <title>How to search text inside Views SQL definition on a SQL Server database</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2010/01/29/how-to-search-text-inside-views-sql-definition-on-a.aspx</link>
            <description>&lt;p&gt;Scenario: There is a problem in SQL 2005 and 2008, due to Legacy functionalities of SQL 2000. In SQL 2000 it was possible to use an ORDER BY clause inside a VIEW putting a SELECT TOP 100 PERCENT in the View definition. Obviously We all used it massively :D, after the SP2 or SP3 in SQL 2005 this feature available for all databases set in SQL 2000 compatibility mode does not work anymore. &lt;/p&gt;  &lt;p&gt;I've discovered this on a production site where a nice small Access Function Developed in 2001 stopped working on October 15 2009. &lt;/p&gt;  &lt;p&gt;There is an Hot Fix (Made after SP2 but not contained in SP3) that fixes the problem &lt;a href="http://support.microsoft.com/kb/936305/" target="_blank"&gt;available here&lt;/a&gt;, but of course it is opportune for us to find out and change all views using the Order By feature and modify then the code to order things after the View. &lt;/p&gt;  &lt;p&gt;After the problem has been discovered and the fix made on the Function, the question was: &lt;/p&gt;  &lt;p&gt;How many views with an Order By do I have in this database? &lt;/p&gt;  &lt;p&gt;Searching on the internet I've found a nice small script on Stack overflow Forums so I put it here just in case: &lt;/p&gt;  &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 
    v.name, 
    m.definition 
&lt;span class="kwrd"&gt;FROM&lt;/span&gt;  
    sys.views v 
&lt;span class="kwrd"&gt;INNER&lt;/span&gt; &lt;span class="kwrd"&gt;JOIN&lt;/span&gt;  
    sys.sql_modules m &lt;span class="kwrd"&gt;ON&lt;/span&gt; v.object_ID = m.object_id 
&lt;span class="kwrd"&gt;WHERE&lt;/span&gt; m.definition &lt;span class="kwrd"&gt;LIKE&lt;/span&gt; &lt;span class="str"&gt;'%TOP%'&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This script allowed me to search the TOP clause in all my views definitions, and can be customized to search probably any piece of sql contained in all object definitions in a database.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href="http://adsi.mvps.org/" target="_blank"&gt;Mark S.&lt;/a&gt; for sharing it.&lt;/p&gt;

&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ec014b8f-06b8-49db-8aab-cf337277893a" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tag: &lt;a href="http://technorati.com/tags/SQL" rel="tag"&gt;SQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Order+by" rel="tag"&gt;Order by&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SortedViews" rel="tag"&gt;SortedViews&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/331.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2010/01/29/how-to-search-text-inside-views-sql-definition-on-a.aspx</guid>
            <pubDate>Fri, 29 Jan 2010 11:28:58 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/331.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2010/01/29/how-to-search-text-inside-views-sql-definition-on-a.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/331.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Some Database Modification tests</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2009/12/31/some-database-modification-tests.aspx</link>
            <description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;If you, like me, have the misfortune to work with very creative software architects :D:D:D, you probably need to write Database Modification scripts on a daily base. It is a good practice be sure that a script executed two times in a row doesn’t throw exceptions, mainly if the script can be executed by your customers DBA (or the customers themselves). I write down here some of the most common tests, you can use when you have to Add tables, Drop Tables, Add Columns, Drop Columns or Foreign Keys. &lt;/p&gt;  &lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;IF&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;EXISTS&lt;/span&gt; (
    &lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 1 &lt;span class="kwrd"&gt;FROM&lt;/span&gt; sysobjects &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; xtype=&lt;span class="str"&gt;'u'&lt;/span&gt; 
       &lt;span class="kwrd"&gt;AND&lt;/span&gt; name=&lt;span class="str"&gt;'MyTable'&lt;/span&gt;)
    &lt;span class="kwrd"&gt;BEGIN&lt;/span&gt;
       &lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; Mytable …;
    END&lt;/pre&gt;

&lt;p&gt;Without the NOT this can be used also to Drop Tables.&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;IF&lt;/span&gt; &lt;span class="kwrd"&gt;EXISTS&lt;/span&gt; (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; sys.foreign_keys &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; object_id = &lt;br /&gt;    OBJECT_ID(&lt;span class="str"&gt;'[FK_MyForeignKeyConstraintName]'&lt;/span&gt;) &lt;br /&gt;    &lt;span class="kwrd"&gt;AND&lt;/span&gt; parent_object_id = OBJECT_ID(&lt;span class="str"&gt;'[MyTable]'&lt;/span&gt;))
    &lt;span class="kwrd"&gt;ALTER&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; [MyTable] &lt;span class="kwrd"&gt;DROP&lt;/span&gt; &lt;span class="kwrd"&gt;CONSTRAINT&lt;/span&gt; [FK_MyForeignKeyConstraintName]&lt;/pre&gt;

&lt;p&gt;If you need to delete a table with Relations, it is better drop the Constraints before dropping the table. If you Add a Constraint you can set a NOT before Exists and avoid trying to create a constraint already present.&lt;/p&gt;

&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;IF&lt;/span&gt; &lt;span class="kwrd"&gt;NOT&lt;/span&gt; &lt;span class="kwrd"&gt;EXISTS&lt;/span&gt; (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 1 &lt;span class="kwrd"&gt;FROM&lt;/span&gt; sys.columns &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; object_id=object_id(&lt;span class="str"&gt;'MyTable'&lt;/span&gt;) &lt;span class="kwrd"&gt;AND&lt;/span&gt; name=&lt;span class="str"&gt;'MyColumnName'&lt;/span&gt;)
    &lt;span class="kwrd"&gt;ALTER&lt;/span&gt; &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;ADD&lt;/span&gt; MyColumnName &lt;span class="kwrd"&gt;int&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt;;&lt;/pre&gt;

&lt;p&gt;This one is useful when Adding, or without NOT, Dropping columns.&lt;/p&gt;

&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:134f10a2-992c-4c1f-9dfb-00922833361b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Tags: &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/SQL/default.aspx" rel="tag"&gt;SQL&lt;/a&gt;, &lt;a href="http://community.dotnetwork.it/Sabrina/Tags/DBMaintenance/default.aspx" rel="tag"&gt;DBMaintenance&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/284.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2009/12/31/some-database-modification-tests.aspx</guid>
            <pubDate>Thu, 31 Dec 2009 10:43:26 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/284.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2009/12/31/some-database-modification-tests.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/284.aspx</wfw:commentRss>
        </item>
        <item>
            <title>SQL Server,  plain security</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2009/11/29/sql-server--plain-security.aspx</link>
            <description>&lt;p&gt;A colleague, moving it’s first steps into .NET and SQL Server to move it’s applications in 21st century, taking a look at a database I made asked me the following questions:&lt;/p&gt;
&lt;p&gt;Why do I found the SQL User (MyUser) both in Server Security and in your Database? And Why did you tell me you granted permission to a Database Role (db_MyRole) instead of giving them to the user?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;font size="4"&gt;How the security works in SQL Server:&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To answer his questions I’ve decided to explain the basics here in the hope to be helpful to more beginners. &lt;br /&gt;
In order to be able to connect an Application to SQL Server, the Application needs to know a Username and a Password (in Sql Server mode) or the Windows Domain User that uses the application or a Domain Group of which it is member needs to be mapped as a trusted User or Group in SQL Server.&lt;/p&gt;
&lt;p&gt;This can be made manually by the SQL Server Administrator using SQL Management Studio and going to the Security Folder Using the Add Login feature, or it can be done using the following code in a query (connecting to the SQL Server with Administrator rights).&lt;/p&gt;
&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; LOGIN [MyUser] &lt;span class="kwrd"&gt;WITH&lt;/span&gt; PASSWORD=N&lt;span class="str"&gt;'MyPassword'&lt;/span&gt;
    , DEFAULT_DATABASE=[master]
    , DEFAULT_LANGUAGE=[us_english]
    , CHECK_EXPIRATION=&lt;span class="kwrd"&gt;OFF&lt;/span&gt;, CHECK_POLICY=&lt;span class="kwrd"&gt;OFF&lt;/span&gt;

&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; LOGIN [MyWindowsUserOrGroup] 
    &lt;span class="kwrd"&gt;FROM&lt;/span&gt; WINDOWS &lt;span class="kwrd"&gt;WITH&lt;/span&gt; DEFAULT_DATABASE=[master]
    , DEFAULT_LANGUAGE=[us_english]&lt;/pre&gt;
&lt;p&gt;But this is not enough, with this credentials an Application can open a connection to the SQL Server, but the server gives no automatic access to it’s data to a new login. &lt;/p&gt;
&lt;p&gt;To access a Database, the logins (Sql or trusted) first need to be mapped on the database (or databases), this can be achieved from SQL Management Studio, going to the Database Security Folder and adding the user, or through the Usermapping feature in the Login Mask on server security, otherwise it can be done using the following T-SQL queries.&lt;/p&gt;
&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;USER&lt;/span&gt; [MyUser] 
    &lt;span class="kwrd"&gt;FOR&lt;/span&gt; LOGIN [MyUser] &lt;span class="kwrd"&gt;WITH&lt;/span&gt; DEFAULT_SCHEMA=[dbo]

&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;USER&lt;/span&gt; [MyWindowsUserOrGroup] 
    &lt;span class="kwrd"&gt;FOR&lt;/span&gt; LOGIN [MyWindowsUserOrGroup] &lt;span class="kwrd"&gt;WITH&lt;/span&gt; DEFAULT_SCHEMA=[dbo]&lt;/pre&gt;
&lt;p&gt;After this operation is done, the application can connect to the server and access the database, but still it can’t see any data, because we didn’t grant it any permission on the database objects, such as Tables, Stored Procedures, Views, Functions and more.&lt;/p&gt;
&lt;p&gt;We can grant permission on the objects to our logins in two ways, mapping them directly to the User or Windows User, generate a database Role, give permissions to the role and then assign the role to the users. The second way is the one I choose because a user can be changed for any reason, so the role can be passed to another user and we are sure the user has all permissions he needs. The role can also be assigned to more than one user and again we are sure the users have all the correct permissions and if we need to add some permissions or remove some permission we do it on the role and all users benefit of the modifications.&lt;/p&gt;
&lt;p&gt;Again, this can be done both from the SQL management studio and by code.&lt;/p&gt;
&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; [MyDatabase]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;CREATE&lt;/span&gt; &lt;span class="kwrd"&gt;ROLE&lt;/span&gt; [MyRole] &lt;span class="kwrd"&gt;AUTHORIZATION&lt;/span&gt; [sa]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;use&lt;/span&gt; [MyDatabase]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;GRANT&lt;/span&gt; &lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;ON&lt;/span&gt; [dbo].[MyTable1] &lt;span class="kwrd"&gt;TO&lt;/span&gt; [MyRole]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;use&lt;/span&gt; [MyDatabase]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;GRANT&lt;/span&gt; &lt;span class="kwrd"&gt;EXECUTE&lt;/span&gt; &lt;span class="kwrd"&gt;ON&lt;/span&gt; [dbo].[MySpSelect1] &lt;span class="kwrd"&gt;TO&lt;/span&gt; [MyRole]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;use&lt;/span&gt; [MyDatabase]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;GRANT&lt;/span&gt; &lt;span class="kwrd"&gt;EXECUTE&lt;/span&gt; &lt;span class="kwrd"&gt;ON&lt;/span&gt; [dbo].[MySpSelect2] &lt;span class="kwrd"&gt;TO&lt;/span&gt; [MyRole]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;use&lt;/span&gt; [MyDatabase]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;GRANT&lt;/span&gt; &lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;ON&lt;/span&gt; [dbo].[MyTable2] &lt;span class="kwrd"&gt;TO&lt;/span&gt; [MyRole]
GO&lt;/pre&gt;
&lt;p&gt;After the role is made, we made the users member of the role and we have reached our goal. Also this operation can be done using SSMS or the following query.&lt;/p&gt;
&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; [FyRepository]
&lt;span class="kwrd"&gt;GO&lt;/span&gt;
&lt;span class="kwrd"&gt;EXEC&lt;/span&gt; sp_addrolemember N&lt;span class="str"&gt;'MyRole'&lt;/span&gt;, N&lt;span class="str"&gt;'Myuser'&lt;/span&gt;
GO&lt;/pre&gt;
&lt;p&gt;&lt;font size="4"&gt;&lt;strong&gt;In Short&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;To access the data of a SQL Server database, an Application needs to be able to Connect to the Server through a Login, the Login needs to be mapped as a Database User on the database (s) needed, and the Database User mapped needs to be member of one or more Database Roles that grant it permissions on the database objects.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:25314697-b4ff-418d-84b9-08a303b2fd3a" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;Technorati Tag: &lt;a rel="tag" href="http://technorati.com/tags/SQL"&gt;SQL&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/T-SQL"&gt;T-SQL&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Security"&gt;Security&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/273.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2009/11/29/sql-server--plain-security.aspx</guid>
            <pubDate>Sun, 29 Nov 2009 10:55:52 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/273.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2009/11/29/sql-server--plain-security.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/273.aspx</wfw:commentRss>
        </item>
        <item>
            <title>How to generate rapidly a copy of a table in SQL Server</title>
            <link>http://community.dotnetwork.it/Excentric/archive/2009/11/26/how-to-generate-rapidly-a-copy-of-a-table-in.aspx</link>
            <description>&lt;p&gt;Today I've found a helpful little piece of SQL I wasn't aware of so I share this with the whole Internet in the hope to help someone else. The scenario was the following, I have a remote server from which I need to import a few tables on my local SQL Server for further processing. The Two servers are connected through a VPN. The connection is good but I can’t kill the band with a long run series of data transferts through the network. So I first set a linked server from the local machine to the remote server that allows me to query the remote server on the Database I'm interested in. Then, to transfer the table I was interested in I wrote this code:&lt;/p&gt;
&lt;pre class="TsqlFormat"&gt;&lt;span class="kwrd"&gt;USE&lt;/span&gt; MyNewLocalDb

&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; FIELD1, FIELD2, FIELD3 .... FIELDn &lt;span class="kwrd"&gt;INTO&lt;/span&gt; dbo.MynewTable &lt;span class="kwrd"&gt;FROM&lt;/span&gt;
[MyRemoteServer].[MyRemoteDb].[Dbo].[MyremoteTable]&lt;/pre&gt;
&lt;p&gt;In this case I had just a simple select from table, but If you need to build the table from a a Join, in the from clause you can write a complex select with joins or whatever you need. The Table is very raw, but then you can set PK, indexes and whatever else you need on it.&lt;/p&gt;
&lt;p&gt;Obviously you need to have the proper rights on the two databases to make this work, you have to be at least Db_Owner of the destination db and Db_Datareader on the source db.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e4d92b9a-4537-4867-962b-d25919d60856" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;Technorati Tag: &lt;a rel="tag" href="http://technorati.com/tags/SQL"&gt;SQL&lt;/a&gt;&lt;/div&gt;&lt;img src="http://community.dotnetwork.it/Excentric/aggbug/270.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sabrina C.</dc:creator>
            <guid>http://community.dotnetwork.it/Excentric/archive/2009/11/26/how-to-generate-rapidly-a-copy-of-a-table-in.aspx</guid>
            <pubDate>Thu, 26 Nov 2009 10:35:46 GMT</pubDate>
            <wfw:comment>http://community.dotnetwork.it/Excentric/comments/270.aspx</wfw:comment>
            <comments>http://community.dotnetwork.it/Excentric/archive/2009/11/26/how-to-generate-rapidly-a-copy-of-a-table-in.aspx#feedback</comments>
            <wfw:commentRss>http://community.dotnetwork.it/Excentric/comments/commentRss/270.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>