<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>altinoren.com - SQL</title>
    <link>http://altinoren.com/</link>
    <description>Gokhan Altinoren's Blog and Projects</description>
    <language>en-us</language>
    <copyright>Gokhan Altinoren</copyright>
    <lastBuildDate>Thu, 08 Mar 2007 10:46:34 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>gokhan@altinoren.com</managingEditor>
    <webMaster>gokhan@altinoren.com</webMaster>
    <item>
      <trackback:ping>http://altinoren.com/Trackback.aspx?guid=d40c6060-6ea3-40b0-bb41-ec8591d075c7</trackback:ping>
      <pingback:server>http://altinoren.com/pingback.aspx</pingback:server>
      <pingback:target>http://altinoren.com/PermaLink,guid,d40c6060-6ea3-40b0-bb41-ec8591d075c7.aspx</pingback:target>
      <dc:creator>Gokhan Altinoren</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
I was doing a code review for a client and something like the following was everywhere:<br /><br /><div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><p style="margin: 0px;">
    command.CommandText = <span style="color: maroon;">"EXEC ManageBlogs;2
'"</span> + blogName + <span style="color: maroon;">"'"</span>;
</p><p style="margin: 0px;">
    command.ExecuteReader();
</p></div><br />
Yes, they don't use parametrized queries. They don't even mark the command as a SP
but that's the usual bad practice I happen to see everywhere. But what the heck "<b>;2</b>"
stands for? I remember reading something like that, let's fire Books Online for Create
Procedure statement (T-SQL 2000, which my client use):<br /><br /><blockquote><i>CREATE PROC [ EDURE ] [ owner<b>.</b> ] </i><i>procedure_name [ <b>; </b></i><i>number
] </i><br /><br /><p><i><b>;</b></i><i>number</i></p><p class="indent"><i>Is an optional integer used to group procedures of the same name so they can be
dropped together with a single DROP PROCEDURE statement. For example, the procedures
used with an application called orders may be named <b>orderproc</b>;1, <b>orderproc</b>;2,
and so on. The statement DROP PROCEDURE <b>orderproc</b> drops the entire group. If
the name contains delimited identifiers, the number should not be included as part
of the identifier; use the appropriate delimiter around </i><i>procedure_name only.</i></p></blockquote><br /><br />
...and here's the 2005 version:<br /><br /><blockquote><i>CREATE { PROC | PROCEDURE } [schema_name<b>.</b>] </i><i>procedure_name
[ <b>;</b></i><i>number ]</i><br /><i>...</i><br /></blockquote><dl><dd><i>; number</i><br /></dd><dd><p><i>Is an optional integer that is used to group procedures of the same name. These
grouped procedures can be dropped together by using one DROP PROCEDURE statement.
For example, an application called <b>orders</b> might use procedures named <b>orderproc;1</b>, <b>orderproc;2</b>,
and so on. The DROP PROCEDURE <b>orderproc</b> statement drops the whole group. If
the name contains delimited identifiers, the number should not be included as part
of the identifier; use the appropriate delimiter around only </i><i>procedure_name.</i></p><p><i>Numbered stored procedures have the following restrictions:</i></p><ul><li><i>Cannot use <b>xml</b> or CLR user-defined types as the data types. 
<br /><br /></i></li><li><i>Cannot create a plan guide on a numbered stored procedure.<br /><br /></i></li></ul><div class="alert"><table cellpadding="0" cellspacing="0" width="100%"><tbody><tr><th align="left"><blockquote><i>Note: </i></blockquote></th></tr><tr><td><blockquote><p><font color="#ff0000"><i>This feature will be removed in a future version of Microsoft
SQL Server. Avoid using this feature in new development work, and plan to modify applications
that currently use this feature.</i></font></p></blockquote></td></tr></tbody></table></div></dd></dl>
I believe this is something from the archaic days when all the development happened
on the DB, that's why SP's for in the first place, isn't it? I even bet this is a
fossil syntax leftover from the <a href="http://en.wikipedia.org/wiki/Sybase_SQL_Server">Sybase
SQL</a> days. Good to see that this will be dropped, but it should have already been
with SQL 2005.<br /><br />
So, what was my client thinking? Since they use SP's extensively, they use this as
a method overload. SP name stands for the static class and the number is something
like different methods to call. They use 1 for INSERT, 2 for SELECT and 3 for DELETE
and so on.<br /><div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><blockquote><p style="margin: 0px;"><span style="color: blue;">CREATE PROCEDURE </span>ManageBlogs;1
</p><p style="margin: 0px;">
    @Name <span style="color: blue;">varchar</span>(50)
</p><p style="margin: 0px;"><span style="color: blue;">AS</span></p><p style="margin: 0px;">
    <span style="color: blue;">INSERT INTO </span>Blogs (blog_name) <span style="color: blue;">VALUES </span>(@Name)
</p><p style="margin: 0px;">
GO
</p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">CREATE PROCEDURE </span>ManageBlogs;2
</p><p style="margin: 0px;"><span style="color: blue;">AS</span></p><p style="margin: 0px;">
    <span style="color: blue;">SELECT </span>* <span style="color: blue;">FROM </span>Blogs
</p><p style="margin: 0px;">
GO
</p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">CREATE PROCEDURE </span>ManageBlogs;3
</p><p style="margin: 0px;">
    @Id <span style="color: blue;">int</span></p><p style="margin: 0px;"><span style="color: blue;">AS</span></p><p style="margin: 0px;">
    <span style="color: blue;">DELETE FROM </span>Blogs <span style="color: blue;">WHERE </span>blog_id
= @Id
</p><p style="margin: 0px;">
GO
</p></blockquote></div>
Usage?<br /><div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><blockquote><p style="margin: 0px;"><span style="color: blue;">Exec </span>ManageBlogs <span style="color: green;">--
Error</span></p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">Exec </span>ManageBlogs <span style="color: maroon;">'MyBlog' </span><span style="color: green;">--
Executes ;1</span></p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">Exec </span>ManageBlogs;1 <span style="color: maroon;">'MyBlog' </span><span style="color: green;">--
Executes ;1</span></p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">Exec </span>ManageBlogs;2 <span style="color: green;">--
Executes ;2</span></p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">Exec </span>[ManageBlogs];2 <span style="color: green;">--
Executes ;2</span></p><p style="margin: 0px;">
 
</p><p style="margin: 0px;"><span style="color: blue;">Exec </span>[ManageBlogs;2] <span style="color: green;">--
Error</span></p></blockquote></div>
Total chaos :) And I was quick to find ;1's that don't insert in some SP's, so they
broke their own rules already.<br /><br />
VS 2005 designer picks up only the first one:<br /><br /><img src="content/binary/NumberedSP_VS.png" border="0" /><br /><br />
Hey, even Management Studio doesn't support numbered SP's:<br /><br /><img src="content/binary/NumberedSP_ManStudio.png" border="0" /><br /><br />
I believe I have enough evidence to to convince my client that this is pure evil :)<br /><img width="0" height="0" src="http://altinoren.com/aggbug.ashx?id=d40c6060-6ea3-40b0-bb41-ec8591d075c7" /></body>
      <title>Stored Procedures are More Evil Than You Think</title>
      <guid isPermaLink="false">http://altinoren.com/PermaLink,guid,d40c6060-6ea3-40b0-bb41-ec8591d075c7.aspx</guid>
      <link>http://altinoren.com/PermaLink,guid,d40c6060-6ea3-40b0-bb41-ec8591d075c7.aspx</link>
      <pubDate>Thu, 08 Mar 2007 10:46:34 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
I was doing a code review for a client and something like the following was everywhere:&lt;br&gt;
&lt;br&gt;
&lt;div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; command.CommandText = &lt;span style="color: maroon;"&gt;"EXEC ManageBlogs;2
'"&lt;/span&gt; + blogName + &lt;span style="color: maroon;"&gt;"'"&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; command.ExecuteReader();
&lt;/p&gt;
&lt;/div&gt;
&lt;br&gt;
Yes, they don't use parametrized queries. They don't even mark the command as a SP
but that's the usual bad practice I happen to see everywhere. But what the heck "&lt;b&gt;;2&lt;/b&gt;"
stands for? I remember reading something like that, let's fire Books Online for Create
Procedure statement (T-SQL 2000, which my client use):&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;i&gt;CREATE PROC [ EDURE ] [ owner&lt;b&gt;.&lt;/b&gt; ] &lt;/i&gt;&lt;i&gt;procedure_name [ &lt;b&gt;; &lt;/b&gt;&lt;/i&gt;&lt;i&gt;number
] &lt;/i&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;i&gt;&lt;b&gt;;&lt;/b&gt;&lt;/i&gt;&lt;i&gt;number&lt;/i&gt;
&lt;/p&gt;
&lt;p class="indent"&gt;
&lt;i&gt;Is an optional integer used to group procedures of the same name so they can be
dropped together with a single DROP PROCEDURE statement. For example, the procedures
used with an application called orders may be named &lt;b&gt;orderproc&lt;/b&gt;;1, &lt;b&gt;orderproc&lt;/b&gt;;2,
and so on. The statement DROP PROCEDURE &lt;b&gt;orderproc&lt;/b&gt; drops the entire group. If
the name contains delimited identifiers, the number should not be included as part
of the identifier; use the appropriate delimiter around &lt;/i&gt;&lt;i&gt;procedure_name only.&lt;/i&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;br&gt;
&lt;br&gt;
...and here's the 2005 version:&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;i&gt;CREATE { PROC | PROCEDURE } [schema_name&lt;b&gt;.&lt;/b&gt;] &lt;/i&gt;&lt;i&gt;procedure_name
[ &lt;b&gt;;&lt;/b&gt; &lt;/i&gt;&lt;i&gt;number ]&lt;/i&gt;
&lt;br&gt;
&lt;i&gt;...&lt;/i&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;dl&gt;
&lt;dd&gt;&lt;i&gt;; number&lt;/i&gt;
&lt;br&gt;
&lt;/dd&gt;
&lt;dd&gt;
&lt;p&gt;
&lt;i&gt;Is an optional integer that is used to group procedures of the same name. These
grouped procedures can be dropped together by using one DROP PROCEDURE statement.
For example, an application called &lt;b&gt;orders&lt;/b&gt; might use procedures named &lt;b&gt;orderproc;1&lt;/b&gt;, &lt;b&gt;orderproc;2&lt;/b&gt;,
and so on. The DROP PROCEDURE &lt;b&gt;orderproc&lt;/b&gt; statement drops the whole group. If
the name contains delimited identifiers, the number should not be included as part
of the identifier; use the appropriate delimiter around only &lt;/i&gt;&lt;i&gt;procedure_name.&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;i&gt;Numbered stored procedures have the following restrictions:&lt;/i&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;i&gt;Cannot use &lt;b&gt;xml&lt;/b&gt; or CLR user-defined types as the data types. 
&lt;br&gt;
&lt;br&gt;
&lt;/i&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;i&gt;Cannot create a plan guide on a numbered stored procedure.&lt;br&gt;
&lt;br&gt;
&lt;/i&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="alert"&gt;
&lt;table cellpadding="0" cellspacing="0" width="100%"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th align="left"&gt;
&lt;blockquote&gt;&lt;i&gt;Note: &lt;/i&gt;&lt;/blockquote&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;blockquote&gt;
&lt;p&gt;
&lt;font color="#ff0000"&gt;&lt;i&gt;This feature will be removed in a future version of Microsoft
SQL Server. Avoid using this feature in new development work, and plan to modify applications
that currently use this feature.&lt;/i&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/dd&gt;
&lt;/dl&gt;
I believe this is something from the archaic days when all the development happened
on the DB, that's why SP's for in the first place, isn't it? I even bet this is a
fossil syntax leftover from the &lt;a href="http://en.wikipedia.org/wiki/Sybase_SQL_Server"&gt;Sybase
SQL&lt;/a&gt; days. Good to see that this will be dropped, but it should have already been
with SQL 2005.&lt;br&gt;
&lt;br&gt;
So, what was my client thinking? Since they use SP's extensively, they use this as
a method overload. SP name stands for the static class and the number is something
like different methods to call. They use 1 for INSERT, 2 for SELECT and 3 for DELETE
and so on.&lt;br&gt;
&lt;div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;blockquote&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;CREATE PROCEDURE &lt;/span&gt;ManageBlogs;1
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @Name &lt;span style="color: blue;"&gt;varchar&lt;/span&gt;(50)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;AS&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;INSERT INTO &lt;/span&gt;Blogs (blog_name) &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;(@Name)
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
GO
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;CREATE PROCEDURE &lt;/span&gt;ManageBlogs;2
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;AS&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;SELECT &lt;/span&gt;* &lt;span style="color: blue;"&gt;FROM &lt;/span&gt;Blogs
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
GO
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;CREATE PROCEDURE &lt;/span&gt;ManageBlogs;3
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; @Id &lt;span style="color: blue;"&gt;int&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;AS&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;DELETE FROM &lt;/span&gt;Blogs &lt;span style="color: blue;"&gt;WHERE &lt;/span&gt;blog_id
= @Id
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
GO
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;/div&gt;
Usage?&lt;br&gt;
&lt;div style="background: white none repeat scroll 0% 50%; font-family: Lucida Console; font-size: 12pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;blockquote&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;ManageBlogs &lt;span style="color: green;"&gt;--
Error&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;ManageBlogs &lt;span style="color: maroon;"&gt;'MyBlog' &lt;/span&gt;&lt;span style="color: green;"&gt;--
Executes ;1&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;ManageBlogs;1 &lt;span style="color: maroon;"&gt;'MyBlog' &lt;/span&gt;&lt;span style="color: green;"&gt;--
Executes ;1&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;ManageBlogs;2 &lt;span style="color: green;"&gt;--
Executes ;2&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;[ManageBlogs];2 &lt;span style="color: green;"&gt;--
Executes ;2&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;
&lt;span style="color: blue;"&gt;Exec &lt;/span&gt;[ManageBlogs;2] &lt;span style="color: green;"&gt;--
Error&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;/div&gt;
Total chaos :) And I was quick to find ;1's that don't insert in some SP's, so they
broke their own rules already.&lt;br&gt;
&lt;br&gt;
VS 2005 designer picks up only the first one:&lt;br&gt;
&lt;br&gt;
&lt;img src="content/binary/NumberedSP_VS.png" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
Hey, even Management Studio doesn't support numbered SP's:&lt;br&gt;
&lt;br&gt;
&lt;img src="content/binary/NumberedSP_ManStudio.png" border="0"&gt;
&lt;br&gt;
&lt;br&gt;
I believe I have enough evidence to to convince my client that this is pure evil :)&lt;br&gt;
&lt;img width="0" height="0" src="http://altinoren.com/aggbug.ashx?id=d40c6060-6ea3-40b0-bb41-ec8591d075c7" /&gt;</description>
      <category>SQL</category>
    </item>
  </channel>
</rss>