⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 61057.htm

📁 一本很基础的SQL讲解
💻 HTM
字号:
<link href="./dzs_cs.css" rel="stylesheet" type="text/css" /><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">      <tr>        <td>&nbsp;</td>      </tr>      <tr>        <td height="24" align="center" valign="bottom" class="d_font3">SQL Server存储过程命名标准</td>      </tr>      <tr>        <td height="3" bgcolor="#E3E3E3"></td>      </tr>      <tr>        <td>&nbsp;</td>      </tr>      <tr>        <td class="d_font4"><P>这个标准蓝图的存储过程命名方法只适用于SQL内部,当创建一个新的存储过程,或者发现一个没有按照这个标准构造的存储过程,请使用这个标准。</P>
<P>注意:如果存储过程以sp_为前缀开始命名,那么会运行的稍微缓慢,这是因为SQL Server将首先查找系统存储过程,所以我们决不推荐使用sp_作为前缀。</P>
<P>句法:</P>
<P>存储过程的命名有这个的语法:</P>
<P>[proc] [MainTableName] By [FieldName(optional)] [Action]</P>
<P><BR>[ 1&nbsp; ]&nbsp; [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ][&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp; ]</P>
<P>[1] 所有的存储过程必须有前缀'proc'。所有的系统存储过程都有前缀"sp_",推荐不使用这样的前缀因为会稍微的减慢。<BR>[2] 表名就是存储过程访问的对象。<BR>[3] 可选字段名就是条件子句。比如:procClientByCoNameSelect,procClientByClientIDSelect。<BR>[4] 最后的行为动词就是存储过程要执行的任务。</P>
<P>如果存储过程返回一条记录那么后缀是:Select</P>
<P>如果存储过程插入数据那么后缀是:Insert</P>
<P>如果存储过程更新数据那么后缀是:Update</P>
<P>如果存储过程有插入和更新那么后缀是:Save</P>
<P>如果存储过程删除数据那么后缀是:Delete</P>
<P>如果存储过程更新表中的数据 (ie. drop and create) 那么后缀是:Create</P>
<P>如果存储过程返回输出参数或0,那么后缀是:Output</P>
<P>例子:</P>
<P>一个仅仅返回一个输出参数的存储过程:</P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>ALTER PROCEDURE procClientRateOutput <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @pstrClientID VARCHAR(6) = 'CABLE',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @pstrCategoryID VARCHAR(6) = '&lt;All&gt;',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @pstrEmpID VARCHAR(6)='AC',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @pdteDate datetime = '1996/1/1',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @curRate MONEY OUTPUT</P><P>AS</P><P>-- Description: Get the $Rate for this client and this employee <BR>--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and this category from Table ClientRate</P><P>SET @curRate = (<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT TOP 1 Rate <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM ClientRate <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE <A href="mailto:ClientID=@pstrClientID">ClientID=@pstrClientID</A> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND <A href="mailto:EmpID=@pstrEmpID">EmpID=@pstrEmpID</A> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND <A href="mailto:CategoryID=@pstrCategoryID">CategoryID=@pstrCategoryID</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND DateEnd &gt; @pdteDate<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY DateEnd<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )</P><P>IF @curRate IS NULL</P><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET @curRate =<BR>(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT TOP 1 Rate <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM ClientRate <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE <A href="mailto:ClientID=@pstrClientID">ClientID=@pstrClientID</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND <A href="mailto:EmpID=@pstrEmpID">EmpID=@pstrEmpID</A> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND CategoryID='&lt;ALL&gt;' <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND DateEnd &gt; @pdteDate <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY DateEnd<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )</P><P>RETURN</P></PRE></TD></TR></TBODY></TABLE></P>
<P>过时的风格:</P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE>Select 'procGetRate' or 'sp_GetRate' <BR>Insert 'procEmailMergeAdd'</PRE></TD></TR></TBODY></TABLE></P>
<P>推荐的风格:</P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>'procClientRateSelect' <BR>'procEmailMergeInsert'</P></PRE></TD></TR></TBODY></TABLE></P>
<P><FONT size=4>【相关文章】</FONT></P>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200711/60809.htm" target=_blank><U><FONT color=blue>如何实现SQL Server快速导入数据</FONT></U></A></LI></UL>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200711/60641.htm" target=_blank><U><FONT color=blue>使用SQL Server数据库嵌套子查询</FONT></U></A></LI></UL>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200711/60587.htm" target=_blank><U><FONT color=blue>把SQL Server数据库部署到远程主机环境</FONT></U></A></LI></UL><DIV align=right>【责任编辑:<A class=ln href='mailto:sunsj@51cto.com'>火凤凰</A> TEL:(010)68476606】</DIV></td>      </tr>      <tr>        <td class="d_font4">&nbsp;</td>      </tr>    </table>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -