<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</title>
        <link>http://blogs.essensys.ro/dan/Default.aspx</link>
        <description>CREATE BLOG [Sql Server] AUTHORIZATION [Daniel Bitin]</description>
        <language>en-US</language>
        <copyright>Daniel Bitin</copyright>
        <managingEditor>dan@essensys.ro</managingEditor>
        <generator>Subtext Version 1.9.5.176</generator>
        <image>
            <title>Sql Server</title>
            <url>http://blogs.essensys.ro/dan/images/RSS2Image.gif</url>
            <link>http://blogs.essensys.ro/dan/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Table spool la apelul unui UDF</title>
            <link>http://blogs.essensys.ro/dan/archive/2007/08/22/table-spool-la-apelul-unui-udf.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"&gt;Table spool la apelul unui UDF&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Mai demult, in cadrul unui proiect, a trebuit sa implementam o functionalitate ce cauta in rezultatele aplicarii unui UDF pe toate liniile dintr-o tabela foarte mare. &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Ne asteptam la un timp de executie mare - se executa un table scan iar tabela nu putea fi cache-ata cu totul in memorie, mai ales in conditii de concurenta mare deoarece alte parti din baza de date erau folosite mai intens in utilizarea normala. Estimarile ne-au fost insa depasite cu mult. Era timpul studierii planului de executie.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;In acesta aparea un operator fizic la care nu ne-am fi asteptat : Table Spool (Eager Spool). Desi acesta prezenta un cost I/O foarte mic, in realitate acesta era mult mai mare deoarece aceste rezultate temporare nu incapeau in memorie si trebuiau scrise pe disc.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Recreand functia cu optiunea SCHEMABINDING (dupa ce am gasit postul &lt;a href="http://blogs.msdn.com/sqlprogrammability/archive/2006/05/12/596424.aspx"&gt;http://blogs.msdn.com/sqlprogrammability/archive/2006/05/12/596424.aspx&lt;/a&gt;) a disparut acest operator si timpul de executie a scazut cu 30%. Prezint mai jos scenariul de test si explicarea acestui comportament.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Scenariu:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Cream tabela de test:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;TABLE&lt;/span&gt; [dbo]&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;[test]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   [codloc] int &lt;span style="COLOR: gray"&gt;primary key,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   [codjud] [char]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   [tip] [char]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   [localit] [varchar]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;25&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;ON&lt;/span&gt; [PRIMARY]&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Umplem tabela test cu 2^20 linii in 20 pasi, fiecare dubland numarul de linii:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; test &lt;span style="COLOR: blue"&gt;values&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'AB'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'O'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Alba Iulia'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @maxkey &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: gray"&gt;&amp;lt;=&lt;/span&gt; 19&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; @maxkey &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codloc&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   &lt;span style="COLOR: blue"&gt;insert&lt;/span&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; codloc &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; @maxkey&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; codjud&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; tip&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; localit &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; @step &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @step &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;3.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Cream functia user-defined pe care o vom folosi ca exemplu:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;create&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;function&lt;/span&gt; MyUDF&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   @str &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;25&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;returns&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;50&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;as&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;reverse&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@str&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; @str&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;4.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Folosind functia definita pentru a filtra datele :&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @t &lt;span style="COLOR: blue"&gt;table&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codloc &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @t&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; codloc &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; test &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; dbo&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;MyUDF&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;like&lt;/span&gt; &lt;span style="COLOR: red"&gt;'%z%'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;obtinem planul de executie (ce are costul total estimat de 8.95):&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;&lt;img height="95" alt="Post 5 - Fig 1" width="628" src="/dan/images/blogs_essensys_ro/dan/Post 5 - fig 1.GIF" /&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;Deoarece am creat UDF-ul fara SCHEMABINDING database engine-ul seteaza intern proprietatea USERDATAACCESS pe 1 chiar daca functia noastra nu acceseaza direct tabele. Pentru ca aceasta proprietate este 1, pentru orice statement de insert/delete/update database engine-ul stocheaza temporar informatii de localizare si rezultatul functiei inainte de a face orice modificare pe date. Daca functia noastra ar fi accesat date din tabela modificata atunci rezultatul sau putea fi afectat de modificarile precedente.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;5.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Schimbam definitia functiei astfel incat sa includa SCHEMABINDING&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;alter&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;function&lt;/span&gt; MyUDF&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   @str &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;25&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;returns&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;50&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;schemabinding&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;as&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;   &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;reverse&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@str&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; @str&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.25in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;In momentul executiei engine-ul de optimizare stie ca functia noastra nu foloseste direct date din tabele. Astfel planul de executie pentru select-ul nostru este:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;&lt;img height="96" alt="Post 5 - Fig 2" width="628" src="/dan/images/blogs_essensys_ro/dan/Post 5 - fig 2.GIF" /&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Desi planul are un cost estimat de 10.2 el ruleaza intr-un timp cu 30% mai mic.&lt;/div&gt;&lt;img src="http://blogs.essensys.ro/dan/aggbug/5.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Bitin</dc:creator>
            <guid>http://blogs.essensys.ro/dan/archive/2007/08/22/table-spool-la-apelul-unui-udf.aspx</guid>
            <pubDate>Wed, 22 Aug 2007 13:31:31 GMT</pubDate>
            <wfw:comment>http://blogs.essensys.ro/dan/comments/5.aspx</wfw:comment>
            <comments>http://blogs.essensys.ro/dan/archive/2007/08/22/table-spool-la-apelul-unui-udf.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.essensys.ro/dan/comments/commentRss/5.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Isolation level read commited - linii duplicate</title>
            <link>http://blogs.essensys.ro/dan/archive/2007/08/22/isolation-level-read-commited---linii-duplicate.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"&gt;Isolation level read commited - linii duplicate&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Urmatorul post este inspirat de postul lui Lubor Kollar “Previously committed rows might be missed if NOLOCK hint is used” (&lt;a href="http://blogs.msdn.com/sqlcat/archive/2007/02/01/previously-committed-rows-might-be-missed-if-nolock-hint-is-used.aspx"&gt;http://blogs.msdn.com/sqlcat/archive/2007/02/01/previously-committed-rows-might-be-missed-if-nolock-hint-is-used.aspx&lt;/a&gt;). In acesta autorul discuta despre lipsa in rezultatele unui select cu nolock a unor linii commit-ate anterior.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Voi discuta astazi tot despre niste rezultate surprinzatoare, de data aceasta pentru un select ce ruleaza in isolation level read commited. Scenariul pe care il propun consta in folosirea a 2 conexiuni:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Una ce foloseste isolation level read commited si executa in cadrul unei tranzactii un select pe o tabela mare (&amp;gt;500k linii) organizata ca un index clusterat.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;A doua ce foloseste tot isolation level read commited si, in cadrul unei tranzactii executate in timpul selectului, muta liniile in btree din frunzele cele mai din stanga catre frunze aflate la capatul din drepta.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;Rezultatul selectului de pe conexiunea 1 este surprinzator prin faptul ca liniile mutate vor fi luate in considerare de 2 ori. Surprinderea este data de faptul ca, in credinta populara, in cadrul isolation level read commited lockurile shared luate pentru citirea tabelei sunt pastrate pana la sfarsitul statement-ului select. Aceasta credinta e generata probabil si de Books Online unde citim:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;“If READ_COMMITTED_SNAPSHOT is set to OFF (the default), the Database Engine uses shared locks to prevent other transactions from modifying rows while the current transaction is running a read operation. The shared locks also block the statement from reading rows modified by other transactions until the other transaction is completed. The shared locks are released when the statement completes.”&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;Ce se intampla de fapt, pe timpul rularii statement-ului select, database engine-ul proceseaza paginile de pe ultimul nivel din btree achizitionand lock-uri pe 1 pagina, maxim 2 si renuntand la ele odata ce procesarea paginii s-a incheiat. El previne astfel dirty read-uri insa sufera de nonreapeatable reads in scenariul descris. Un nivel de izolare repeatable read sau mai mare rezolva aceasta problema deoarece pastreaza lockurile (in exemplul nostru luat direct la nivel de tabela si nu de pagini) pana la sfarsitul tranzactiei.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt;Trebuie sa intelegem insa: comportamentul observat, desi neasteptat, este normal pentru nivelul de izolare ales. Sql Server achizitioneaza lockuri la un nivel cat mai mic si renunta la ele cat mai devreme pentru a mari concurenta, in acelasi timp respectand functionalitatea descrisa in Books Online unde nonreapeatable reads este un side-effect al nivelului de izolare read commited. In cazul in care suntem afectati de asemeneaza efecte in cadrul unei aplicatii trebuie sa ridicam nivelul de izolare pentru ca acest tip de probleme sa dispara.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.25in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Scenariu:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Se creaza tabela organizata ca si index clusterat (campul codloc asignat ca si primary key duce la crearea unui index clusterat unique pe acesta).&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;TABLE&lt;/span&gt; [dbo]&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;[test]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      [codloc] int &lt;span style="COLOR: gray"&gt;primary key,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      [codjud] [char]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      [tip] [char]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      [localit] [varchar]&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;25&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;ON&lt;/span&gt; [PRIMARY]&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Umplem tabela test cu 2^19 linii in 19 pasi, fiecare dubland numarul de linii:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; test &lt;span style="COLOR: blue"&gt;values&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'AB'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'O'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'Alba Iulia'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @maxkey &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @step &lt;span style="COLOR: gray"&gt;&amp;lt;=&lt;/span&gt; 19&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; @maxkey &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codloc&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;insert&lt;/span&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; codloc &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; @maxkey&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; codjud&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; tip&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; localit &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; @step &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @step &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;3.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Pe aceeasi conexiune construim selectul care va dura mult. Deoarece numarul de linii folosite in exemplu nu este foarte mare, aceasta va contine multe functii matematice care incearca sa incetineasca procesarea. De asemenea incercam sa nu duplicam functiile deoarece vor fi evaluate o singura data.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;isolation&lt;/span&gt; &lt;span style="COLOR: blue"&gt;level&lt;/span&gt; &lt;span style="COLOR: blue"&gt;read&lt;/span&gt; &lt;span style="COLOR: blue"&gt;committed&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;tran&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: fuchsia"&gt;count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;0&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; cnt&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sqrt&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sqrt&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sqrt&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sqrt&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;4&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;log10&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;log10&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;log10&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;log10&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;codjud&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;4&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;cos&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;cos&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;cos&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;cos&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;4&lt;span style="COLOR: gray"&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sin&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sin&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sin&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;))),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;sin&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;exp&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ascii&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;localit&lt;span style="COLOR: gray"&gt;)+&lt;/span&gt;4&lt;span style="COLOR: gray"&gt;)))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; test&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;commit&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: blue"&gt;tran&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;Din rezultatele selectului ne intereseaza cnt care va fi 524288.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;cnt   &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%"&gt;------&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;524288&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;4.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Deschidem o conexiune noua in care rulam de mai multe ori sp_lock in timp ce selectul de mai sus ruleaza. Observam faptul ca de fiecare data avem un lock S pe o singura pagina in afara de cel IS pe tabela:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;spid   dbid   ObjId       IndId Type Resource                         Mode     Status&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%"&gt;------ ------ ----------- ------ ---- -------------------------------- -------- ------&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;62     7      565577053   0      TAB                                   IS       GRANT&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%"&gt;62     7      565577053   1      PAG 1:1741                           S        GRANT&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;5.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Deschidem o noua conexiune pe care vom rula codul:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;isolation&lt;/span&gt; &lt;span style="COLOR: blue"&gt;level&lt;/span&gt; &lt;span style="COLOR: blue"&gt;read&lt;/span&gt; &lt;span style="COLOR: blue"&gt;committed&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;begin&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;tran&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; test &lt;span style="COLOR: blue"&gt;set&lt;/span&gt; codloc &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1000000 &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; codloc &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; codloc &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; 1000&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;commit&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: blue"&gt;tran&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;Rezultatul este:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%"&gt;(999 row(s) affected)&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;6.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Rulam selectul de pe conexiunea descrisa la punctul 3. In timp ce ruleaza pornim si update-ul de la punctul 5. In rezultatul selectului vedem:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;cnt   &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in"&gt;&lt;span style="FONT-SIZE: 8pt; LINE-HEIGHT: 115%"&gt;------&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 8pt"&gt;525287&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in"&gt;Numarul de linii din [test] nu s-a schimbat (nu au avut loc insert-uri sau delete-uri). Totusi obtinem o valoare pentru [cnt] mai mare cu 999, ceea ce inseamna ca liniile mutate prin update au fost luat in considerare de doua ori.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt; &lt;/div&gt;&lt;img src="http://blogs.essensys.ro/dan/aggbug/4.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Bitin</dc:creator>
            <guid>http://blogs.essensys.ro/dan/archive/2007/08/22/isolation-level-read-commited---linii-duplicate.aspx</guid>
            <pubDate>Wed, 22 Aug 2007 13:18:27 GMT</pubDate>
            <wfw:comment>http://blogs.essensys.ro/dan/comments/4.aspx</wfw:comment>
            <comments>http://blogs.essensys.ro/dan/archive/2007/08/22/isolation-level-read-commited---linii-duplicate.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://blogs.essensys.ro/dan/comments/commentRss/4.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Linked server Excel 2007</title>
            <link>http://blogs.essensys.ro/dan/archive/2007/08/22/linked-server-excel-2007.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"&gt;Linked server Excel 2007&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Cand a trebuit sa folosesc un fisier Excel 2007 (.xlsx) ca si sursa de date in Sql Server am avut o mica surpriza. Noul format 2007 nu mai este citibil folosind [Microsoft Jet 4.0 OLE DB Provider] si trebuie folosit providerul [Microsoft Office 12.0 Access Database Engine OLE DB Provider].&lt;/div&gt;
&lt;span style="FONT-SIZE: 11pt; LINE-HEIGHT: 115%"&gt;Configurarea este identica, trebuind completat [Product name] cu o valoare nevida si [Provider string] = ‘Excel 12.0’ ([Provider string] ajunge in [Extended Properties] pe provider).&lt;/span&gt;&lt;img src="http://blogs.essensys.ro/dan/aggbug/3.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Bitin</dc:creator>
            <guid>http://blogs.essensys.ro/dan/archive/2007/08/22/linked-server-excel-2007.aspx</guid>
            <pubDate>Wed, 22 Aug 2007 13:17:28 GMT</pubDate>
            <wfw:comment>http://blogs.essensys.ro/dan/comments/3.aspx</wfw:comment>
            <comments>http://blogs.essensys.ro/dan/archive/2007/08/22/linked-server-excel-2007.aspx#feedback</comments>
            <slash:comments>39</slash:comments>
            <wfw:commentRss>http://blogs.essensys.ro/dan/comments/commentRss/3.aspx</wfw:commentRss>
        </item>
        <item>
            <title>xp_cmdshell – spawn cu credentiale la alegere ?</title>
            <link>http://blogs.essensys.ro/dan/archive/2007/08/22/xp_cmdshell--spawn-cu-credentiale-la-alegere.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"&gt;xp_cmdshell – spawn cu credentiale la alegere ?&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Zilele trecute am fost sunat de un client ce avea probleme la accesarea din cod TSQL a datelor din niste fisiere Excel aflate intr-un share pe o masina din domeniu.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Persoana in cauza face parte din departamentul de raportare al unei banci si taskul pe care il avea de terminat consta in preluarea datelor dintr-un fisier Excel aflat pe un share, procesarea lor in corelatie cu datele dintr-o baza de date Sql Server si scrierea altui fisier Excel pe alt share. Acest fisier destinatie trebuia generat pe un schedule sau la comanda. &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Deoarece procesarea datelor dupa preluarea din fisierul Excel sursa dureaza un timp considerabil, solutia pe care as fi propus-o la inceperea unui asemenea task consta in crearea unui job Sql Server ce ar fi folosit Integration Services in primul pas (preluarea datelor din Excel) si in ultimul (scrierea rezultatelor). Pe ambii pasi puteam seta credentiale Windows de rulare, credentiale ce se foloseau la accesarea share-urilor cu fisierele sursa si detinatie.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Am fost sunat insa catre terminarea taskului, atunci cand implementarea era deja facuta folosind exclusiv TSQL, cod ce accesa sursa si destinatia Excel folosind OPENQUERY. Asa cum am discutat intr-un alt post, folosirea unor linked servers si a maparilor de login-uri remote nu ne ajutau deoarece aceste login-uri remote nu sunt folosite ca si credentiale Windows pentru accesarea share-urilor. Pentru a functiona abordarea cu OPENQUERY, clientul a  redus problema la accesarea fisierelor XLS aflate pe masina cu instanta de Sql Server, dupa ce au fost copiate folosind xp_cmdshell de pe share-ul aflat pe alta masina.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;In acest punct insa rezultatul operatiei de copiere era constant “Access denied”. O privire in Books Online ne spune ca, daca user-ul ce ruleaza xp_cmdshell este membru al role-ului sysadmin (iar cel ce testa acest lucru se autentifica in Sql Server cu user-ul de domeniu ce era in Administrators pe masina cu instanta Sql Server) procesul cmd.exe spawn-at de Sql Server rula cu credentialele serviciului Sql Server (in cazul nostru Local System Account, adica SYSTEM). Am verificat repede acest lucru ruland in contextul sau de securitate:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;exec&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: maroon"&gt;xp_cmdshell&lt;/span&gt; &lt;span style="COLOR: red"&gt;'copy con'&lt;/span&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Copy con va astepta input de pe stdin iar acesta nu va veni niciodata, lucru ce ne permite observarea aparitiei procesului cmd.exe ce ruleaza ca SYSTEM in Task Manager. Terminand fortat acest proces va conduce la terminarea statementului &lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;exec&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: maroon"&gt;xp_cmdshell. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Tot din Books Online aflam faptul ca atunci cand userul ce ruleaza xp_cmdshell nu e membru sysadmin atunci cmd.exe va fi spawn-at folosind credentialele &lt;strong&gt;##xp_cmdshell_proxy_account##&lt;/strong&gt;. Am creat aceste credentiale folosind &lt;strong&gt;sp_xp_cmdshell_proxy_account&lt;/strong&gt; si am testat acest lucru folosind un login Sql Server pe care l-am creat. Ne-am logat cu noul user si am dat de prima neconcordanta in Books Online unde citim ce pentru a rula &lt;span style="FONT-SIZE: 10pt; COLOR: maroon; LINE-HEIGHT: 115%"&gt;xp_cmdshell&lt;/span&gt; trebuie sa avem permisiune CONTROL SERVER. Lucru ce nu este adevarat, maparea login-ului pe master si acordarea permisiunii EXECUTE pe master.sys.xp_cmdshell fiind suficiente pentru a rula acest extended stored procedure. Am putut observa apoi procesul cmd.exe spawn-at cu credentialele din &lt;strong&gt;##xp_cmdshell_proxy_account##&lt;/strong&gt;.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Intrebarea pe care ne-am pus-o a fost, ce este cu aceasta permisiune CONTROL SERVER legata de xp_cmdshell. Raspunsul pe care l-am aflat contrazicea din nou Books Online : acordarea acestei permisiuni user-ului ce ruleaza &lt;span style="FONT-SIZE: 10pt; COLOR: maroon; LINE-HEIGHT: 115%"&gt;xp_cmdshell&lt;/span&gt; este suficienta (nu trebuie sa fie sysadmin) pentru ca spawn-area cmd.exe sa se faca folosind account-ului sub care ruleaza instanta de Sql Server. Cine nu are aceasta permisiune isi va rula comanda shell folosind credentialele &lt;strong&gt;##xp_cmdshell_proxy_account##&lt;/strong&gt;.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Workaround-ul gasit de noi pentru a putea rula operatia de copiere folosind credentialele configurabile ( &lt;strong&gt;##xp_cmdshell_proxy_account##&lt;/strong&gt;) si  pentru useri din sysadmin a fost schimbarea contextului de securitate (catre un login cu drept de execute pe xp_cmdshell dar fara CONTROL SERVER permission) inainte de executia comenzii shell si apoi revenirea la vechiul context dupa terminarea acesteia:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;execute&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; &lt;span style="COLOR: blue"&gt;login&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: red"&gt;'newlogin'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;exec&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: maroon"&gt;xp_cmdshell&lt;/span&gt; &lt;span style="COLOR: red"&gt;'copy con'&lt;/span&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;revert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray; LINE-HEIGHT: 115%"&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Aceasta mi se pare o limitare fantastica. Pentru userii fara aceasta permisiune exista o modalitate de configurare a credentialelor sub care va rula cmd.exe iar pentru ceilalti nu. Sunt sigur ca aceasta schema de securitate nu a fost creata avand in minte rularea programatica a comenzilor shell folosind credentiale configurabile, insa rezultatul este departe de a fi unul natural, la care te-ai astepta.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt; &lt;/div&gt;&lt;img src="http://blogs.essensys.ro/dan/aggbug/2.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Bitin</dc:creator>
            <guid>http://blogs.essensys.ro/dan/archive/2007/08/22/xp_cmdshell--spawn-cu-credentiale-la-alegere.aspx</guid>
            <pubDate>Wed, 22 Aug 2007 13:16:56 GMT</pubDate>
            <wfw:comment>http://blogs.essensys.ro/dan/comments/2.aspx</wfw:comment>
            <comments>http://blogs.essensys.ro/dan/archive/2007/08/22/xp_cmdshell--spawn-cu-credentiale-la-alegere.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blogs.essensys.ro/dan/comments/commentRss/2.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Securitate linked servers</title>
            <link>http://blogs.essensys.ro/dan/archive/2007/08/22/securitate-linked-servers.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"&gt;Securitate linked servers&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Multe neintelegeri am vazut legate de securitatea pe linked servere. Zilele trecute un client incerca sa acceseze datele dintr-un fisier Excel aflat pe un share in domeniu folosind credentialele altui user Windows decat cel cu care era logat.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;In tab-ul Security din proprietatile unui linked server se poate configura ca pentru un login ce il acceseaza (fie ca este in lista de mapare de sus, fie in “restul”) sa:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Impersoneze acel login inainte de a accesa sursa de date. Aici avem 2 cazuri:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 1.25in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;a.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Login-ul este Windows -&amp;gt; Inainte de a se conecta la sursa de date contextul de securitate pe firul de executie va fi setat la acest login. Va face apoi conectarea ca si cum noi ne-am conecta la o instanta Sql Server folosind SSMS si alegand Windows Authentication din drop-down list-ul Authentication. Aceasta va functiona deoarece procesul SSMS ruleaza sub contextul nostru de securitate cand il pornim.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 1.25in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;b.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;      &lt;/span&gt;&lt;/span&gt;Login-ul este Sql Server -&amp;gt; Daca impersonam va functiona doar conectarea catre o alta instanta de Sql Server ce are definit un login Sql Server cu acelasi username si parola.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Sa se conecteze la sursa de date folosind o pereche username/password. Acestea nu pot fi credentiale Windows (sau mai bine spus nu vor fi tratate ca si credentiale Windows). Sql Server nu va impersona user-ul dat de aceste credentiale ci le va folosi in stringul de conectare pasat provider-ului. Credentialele trebuie sa aibe relevanta pentru sursa de date si nu pentru Windows. Daca sursa de date este Sql Server, credentialele trebuie sa reprezinte un login Sql Server, daca sursa de date este Excel atunci ele trebuie sa poata fi folosite de providerul Jet (singura combinatie de username/password care functioneaza ptr. Jet este admin/&amp;lt;empty&amp;gt;).&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.75in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;3.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;       &lt;/span&gt;&lt;/span&gt;Sa nu trimita credentiale catre sursa de date. Daca sursa de date este Sql Server aceasta optiune nu va functiona. Daca sursa de date este de exemplu Excel iar login-ul ce incearca conectare este de tip Windows atunci se va accesa fisierul xls tot cu credentialele login-ului dar nu va trimite credentiale catre providerul Jet (in Extended properties : “User ID=admin;Password=;”). &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Ce este important de retinut aici este faptul ca oriunde in tab-ul Security putem introduce username/password nu putem introduce credentiale de Windows. Pentru a accesa linked server-ul cu alte credentiale decat cele “curente” va trebui sa modificam credentialele curente inainte de a accesa linked server-ul in cod TSQL. Putem face acest lucru la un nivel superior, de ex:&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;-&lt;span style="FONT: 7pt 'Times New Roman'"&gt;          &lt;/span&gt;&lt;/span&gt;Includem codul ce acceseaza linked serverul intr-un job step pe care putem seta credentiale;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;-&lt;span style="FONT: 7pt 'Times New Roman'"&gt;          &lt;/span&gt;&lt;/span&gt;Schimbam contextul de securitate in cod .NET de exemplu, ne conectam la baza de date folosind ADO.NET si Integrated Security=SSPI si accesam linked server-ul;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in"&gt;&lt;span&gt;-&lt;span style="FONT: 7pt 'Times New Roman'"&gt;          &lt;/span&gt;&lt;/span&gt;Schimbam credentialele pe data source in Reporting Services la niste credentiale Windows si bifam “&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt;Use as Windows credentials when connecting to the data source”&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Odata ajunsi in codul TSQL nu vom putea face acest lucru, nici chiar folosirea “execute as login=”nu va duce la o authentificare cu succes pe sursa de date.&lt;/div&gt;&lt;img src="http://blogs.essensys.ro/dan/aggbug/1.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Bitin</dc:creator>
            <guid>http://blogs.essensys.ro/dan/archive/2007/08/22/securitate-linked-servers.aspx</guid>
            <pubDate>Wed, 22 Aug 2007 13:15:17 GMT</pubDate>
            <wfw:comment>http://blogs.essensys.ro/dan/comments/1.aspx</wfw:comment>
            <comments>http://blogs.essensys.ro/dan/archive/2007/08/22/securitate-linked-servers.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blogs.essensys.ro/dan/comments/commentRss/1.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>