[原创]对ASE系统存储过程的剖析-sp_who
<P align=left><FONT face=宋体 color=#000000 size=2>昨晚有空,把</FONT> <FONT face="Times New Roman" color=#000000 size=2>sybase</FONT> <FONT face=宋体 color=#000000 size=2>系统过程</FONT> <FONT face="Times New Roman" color=#000000 size=2>sp_who</FONT> <FONT face=宋体 color=#000000 size=2>,</FONT> <FONT face="Times New Roman" color=#000000 size=2>DDL</FONT> <FONT face=宋体 color=#000000 size=2>后分析了一下,全文如下:</FONT> <BR><P align=left><BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以下是</FONT> <FONT face="Times New Roman" color=#0000ff size=2>DDL</FONT> <FONT face=宋体 color=#0000ff size=2>出来的内容:</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>-----------------------------------------------------------------------------</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>-- DDL for Stored procedure 'sybsystemprocs.dbo.sp_who'</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>-----------------------------------------------------------------------------</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以上是注释</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>print 'Creating Stored procedure sp_who'</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>上面两句是显示一句话,</FONT> <FONT face="Times New Roman" color=#0000ff size=2>go</FONT> <FONT face=宋体 color=#0000ff size=2>是执行(下同)</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>use sybsystemprocs </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>选择将要使用的数据库(存储过程建在什么数据库里)</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>setuser 'dbo' </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>使当前用户充当</FONT> <FONT face="Times New Roman" color=#0000ff size=2>'dbo</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以下是存储过程的实体</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>/* Sccsid = "%Z% generic/sproc/%M% %I% %G%" */</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>/*4.81.106/14/90sproc/src/serveroption */</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>/*</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>** Messages for "sp_who" 17nnn</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>** </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>** 17231, "No login with the specified name exists." </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>*/</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以上是注释</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>create procedure sp_who @loginame varchar(30) = NULL as</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>创建存储过程</FONT> <FONT face="Times New Roman" color=#0000ff size=2>sp_who)</FONT> <FONT face=宋体 color=#0000ff size=2>的参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>loginame</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>declare @low int</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>declare @high int</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>declare @spidlow int</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>declare @spidhigh int</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>declare @len1 int, @len2 int, @len3 int</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>申明了若干变量</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>if @@trancount = 0</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@</FONT> <FONT face=宋体 color=#0000ff size=2>开头的都是系统全局变量</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@trancount</FONT> <FONT face=宋体 color=#0000ff size=2>是用来检查事务嵌套级别的全局变量,批处理中每个</FONT> <FONT face="Times New Roman" color=#0000ff size=2>begin transaction</FONT> <FONT face=宋体 color=#0000ff size=2>将事务计数加</FONT> <FONT face="Times New Roman" color=#0000ff size=2>1</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>set chained off</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>用在会话开始和事务结束时使用,指示是否在第一个数据检索或数据修改语句前开始一个事务(是否在</FONT> <FONT face="Times New Roman" color=#0000ff size=2>delte</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>fetch</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>insert</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>open</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>select</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>update</FONT> <FONT face=宋体 color=#0000ff size=2>前隐式执行一个</FONT> <FONT face="Times New Roman" color=#0000ff size=2>begin transaction</FONT> <FONT face=宋体 color=#0000ff size=2>命令)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>end</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>set transaction isolation level 1</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>设置会话所用的事务隔离级别。当设置此选项后,所有当前或将来的事务都将在些隔离级别上运行</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--level 1 </FONT> <FONT face=宋体 color=#0000ff size=2>的意义:允许对数据使用共享读取锁</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>select @low = @@minsuid, @high = @@maxsuid, </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @spidlow = @@minspid, @spidhigh = @@maxspid</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>给局部变量赋值,四个全局变量的含义:</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@minsuid</FONT> <FONT face=宋体 color=#0000ff size=2>:最小服务器用户</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@maxsuid</FONT> <FONT face=宋体 color=#0000ff size=2>:最大服务器用户</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@minspid</FONT> <FONT face=宋体 color=#0000ff size=2>:最小服务器进程</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--@@maxspid</FONT> <FONT face=宋体 color=#0000ff size=2>:最大服务器进程</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>if @loginame is not NULL</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>判断存储过程的参数是否为空</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>开始一个新的批</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>select @low = suser_id(@loginame), @high = suser_id(@loginame)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> </FONT> <FONT face="Times New Roman" color=#0000ff size=2>--suser_id:</FONT> <FONT face=宋体 color=#0000ff size=2>系统函数,返回服务器用户在</FONT> <FONT face="Times New Roman" color=#0000ff size=2>syslogins</FONT> <FONT face=宋体 color=#0000ff size=2>表中的</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <FONT face=宋体 color=#0000ff size=2>号</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>if @low is NULL</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> </FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>如果参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>不正确的话,</FONT> <FONT face="Times New Roman" color=#0000ff size=2>suser_id</FONT> <FONT face=宋体 color=#0000ff size=2>应该返回</FONT> <FONT face="Times New Roman" color=#0000ff size=2>NULL</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> if @loginame like "%"</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>判断一下</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>是不是数字开头的字符串</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>如果是的话把说明参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>应该是服务器系统进和</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> select @spidlow = convert(int, @loginame),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @spidhigh = convert(int, @loginame),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @low = 0, @high = @@maxsuid</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>把参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>转换成</FONT> <FONT face="Times New Roman" color=#0000ff size=2>int</FONT> <FONT face=宋体 color=#0000ff size=2>型并赋给</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidlow</FONT> <FONT face=宋体 color=#0000ff size=2>和</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidhigh</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> end</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> else</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>如果不是数据开头的字符串,返回错误代码,并返回</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> /*</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> ** 17231, "No login with the specified name exists." </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> */</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> raiserror 17231</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> </FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>返回的错误号是</FONT> <FONT face="Times New Roman" color=#0000ff size=2>17231</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> </FONT> <FONT face="Times New Roman" color=#0000ff size=2>--raiserror</FONT> <FONT face=宋体 color=#0000ff size=2>的作用是返回预定义的错误号</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> return (1)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> end</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>end</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>end</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以上进行的工作是将</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>进行处理,生成下面查询语言的所需的条件值</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@low</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@high</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidlow</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidhigh</FONT> <BR></P> <P align=left><FONT face="Times New Roman" color=#000000 size=2>select @len1 = max(datalength(suser_name(suid))),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @len2 = max(datalength(db_name(dbid))),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @len3 = max(datalength(suser_name(origsuid)))</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>from master..sysprocesses</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>where suid >= @low and suid <= @high and</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> spid >= @spidlow and spid <= @spidhigh</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--suser_name()</FONT> <FONT face=宋体 color=#0000ff size=2>:返回当前服务器用户的名称或已指定服务器用户</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <FONT face=宋体 color=#0000ff size=2>的用户</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--db_name()</FONT> <FONT face=宋体 color=#0000ff size=2>:返回已指定</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <FONT face=宋体 color=#0000ff size=2>号的数据库的名称</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--suser_name()</FONT> <FONT face=宋体 color=#0000ff size=2>:返回当前服务用户的名称或已指定服务器用户</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <FONT face=宋体 color=#0000ff size=2>的用户</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--datalength()</FONT> <FONT face=宋体 color=#0000ff size=2>:返回指定列或字符串的实际长度(以字节表示)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--max()</FONT> <FONT face=宋体 color=#0000ff size=2>:返回表达式的最大值</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以上语句找出</FONT> <FONT face="Times New Roman" color=#0000ff size=2>master</FONT> <FONT face=宋体 color=#0000ff size=2>库</FONT> <FONT face="Times New Roman" color=#0000ff size=2>sysprocesses</FONT> <FONT face=宋体 color=#0000ff size=2>表中</FONT> <FONT face="Times New Roman" color=#0000ff size=2>suid</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>dbid</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>origsuid</FONT> <FONT face=宋体 color=#0000ff size=2>三列的最大实际长度</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>以下语句是返回结果</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>两个</FONT> <FONT face="Times New Roman" color=#0000ff size=2>select</FONT> <FONT face=宋体 color=#0000ff size=2>语句的检索列、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>from</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>where</FONT> <FONT face=宋体 color=#0000ff size=2>、</FONT> <FONT face="Times New Roman" color=#0000ff size=2>order</FONT> <FONT face=宋体 color=#0000ff size=2>子句都是一样的,只是检索列的字符串长度不一致!(更好看些,呵呵)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>两个</FONT> <FONT face="Times New Roman" color=#0000ff size=2>select</FONT> <FONT face=宋体 color=#0000ff size=2>语句都是从</FONT> <FONT face="Times New Roman" color=#0000ff size=2>master</FONT> <FONT face=宋体 color=#0000ff size=2>库</FONT> <FONT face="Times New Roman" color=#0000ff size=2>sysprocesses</FONT> <FONT face=宋体 color=#0000ff size=2>表中检索出符合条件的指定列,</FONT> <FONT face=宋体 color=#ff0000 size=2>红色的地方是两个</FONT> <FONT face="Times New Roman" color=#ff0000 size=2>select</FONT> <FONT face=宋体 color=#ff0000 size=2>不一致的地方</FONT> <FONT face=宋体 color=#0000ff size=2>。</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>if (@len1 > 8 or @len2 > 6 or @len3 > 8)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>select fid,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> spid,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> status,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> loginame=suser_name(suid),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> origname=isnull(suser_name(origsuid), suser_name(suid)),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> hostname,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> blk_spid=convert(char(5),blocked),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> dbname=db_name(dbid),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> cmd,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> block_xloid</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>from master..sysprocesses</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>where suid >= @low and suid <= @high</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>and spid >= @spidlow and spid <= @spidhigh</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>order by fid,spid,dbname</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> end</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>else</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>select fid,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> spid,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> status,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> loginame=</FONT> <FONT face="Times New Roman" color=#ff0000 size=2>convert(char(12), </FONT> <FONT face="Times New Roman" color=#000000 size=2>suser_name(suid)</FONT> <FONT face="Times New Roman" color=#ff0000 size=2>)</FONT> <FONT face="Times New Roman" color=#000000 size=2>,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> origname=</FONT> <FONT face="Times New Roman" color=#ff0000 size=2>convert(char(12), </FONT> <FONT face="Times New Roman" color=#000000 size=2>isnull(suser_name(origsuid), suser_name(suid))</FONT> <FONT face="Times New Roman" color=#ff0000 size=2>)</FONT> <FONT face="Times New Roman" color=#000000 size=2>,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> hostname,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> blk_spid=convert(char(5),blocked), </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> dbname=convert(char(10),db_name(dbid)),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> cmd,</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> block_xloid</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>from master..sysprocesses</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>where suid >= @low and suid <= @high</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>and spid >= @spidlow and spid <= @spidhigh</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>order by fid,spid,dbname</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> end</FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>return (0)</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>返回</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go </FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>Grant Execute on dbo.sp_who to public </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>向</FONT> <FONT face="Times New Roman" color=#0000ff size=2>public</FONT> <FONT face=宋体 color=#0000ff size=2>组授予执行</FONT> <FONT face="Times New Roman" color=#0000ff size=2>sp_who</FONT> <FONT face=宋体 color=#0000ff size=2>的权限</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>setuser </FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>如果在执行</FONT> <FONT face="Times New Roman" color=#0000ff size=2>setuser</FONT> <FONT face=宋体 color=#0000ff size=2>命令时没有指定用户名,则将重新建立数据库所有者的初始标识</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2>go </FONT> <BR>
<P align=left><BR>
<P align=left><FONT face="Times New Roman" color=#800000 size=2>--</FONT> <FONT face=宋体 color=#800000 size=2>我们可以看到,该存储过程的基本思路是先对传入的参数进行处理,生成查询用的条件参数,进而得出查询结果。</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#800000 size=2>--</FONT> <FONT face=宋体 color=#800000 size=2>对于该存储过程为了最终显示效果而加入的一段代码的作法,是值得我们学习的。</FONT> <BR></P> <P>分析下来,如果你执行一个 sp_who '1sgfadf'</P>
<P>就会发生:</P>
<P>Syntax error during explicit conversion of VARCHAR value '1sgfadf' to a INT field. </P>
<P>的错误<BR></P>
<P>呵呵,都是下面这段语句惹的祸:</P>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> if @loginame like "%"</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>判断一下</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>是不是数字开头的字符串</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> begin</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>如果是的话把说明参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>应该是服务器系统进和</FONT> <FONT face="Times New Roman" color=#0000ff size=2>ID</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> select @spidlow = convert(int, @loginame),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @spidhigh = convert(int, @loginame),</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> @low = 0, @high = @@maxsuid</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2></FONT> <FONT face="Times New Roman" color=#0000ff size=2>--</FONT> <FONT face=宋体 color=#0000ff size=2>把参数</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@loginame</FONT> <FONT face=宋体 color=#0000ff size=2>转换成</FONT> <FONT face="Times New Roman" color=#0000ff size=2>int</FONT> <FONT face=宋体 color=#0000ff size=2>型并赋给</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidlow</FONT> <FONT face=宋体 color=#0000ff size=2>和</FONT> <FONT face="Times New Roman" color=#0000ff size=2>@spidhigh</FONT> <BR>
<P align=left><FONT face="Times New Roman" color=#000000 size=2> end</FONT> </P> <P>阿辉真厉害</P>
<P>我以前也埋头看了三个月的时间(sybase的系统procedure)</P> 厉害啊 77777777777777777777777 <P>楼主真厉害,</P>
<P>我是新人.</P>
<P>以后还请多多指教.</P> 楼主有没有分析过sp_helpdb?我每次都要通过sp_helpdb dbname然后把free kbytes加起来看看够不够大(200M左右),如果低于该值,要不删表里的数据,要不再加设备。 顶一下 顶过!~ 顶 <p>厉害啊,能分析下sp_configure吗?</p><p>谢谢</p>
页:
[1]
2