需要说明的就是: output:表明此参数是可以回传的。 [with]{recompile|encryption}中的recompile:表明每次执行此存储过程的时候,都重新编译一次(默认情况下只有在创建的时候才进行编译)。 encryption:所创建的存储过程的内容会被加密。 小技巧:在这里需要说明的是,如果我们有时候要在数据库中查找所有包含A关键字的表的列的名称,那么该如何寻找呢?可以利用下面的语句: 代码如下: select table_name,column_name from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAME like '%A%'; --查看那些表含有包含A的列
但是如果想在存储过程找存在表“B”的存储过程的名称,该如何做呢,可以利用下面的语句来进行: 代码如下: select routine_name, routine_definition from information_schema.routines where routine_definition like '%B%' and routine_type='procedure'
当然了,我们其实还可以利用SQL中的syscomments,sysobjects,sysdepends来查看具体的数据信息,这个和oracle中的dba_objects等很像: 代码如下: select * from syscomments; --查看标注 select * from sysobjects; --查看数据库对象 select * from sysdepends; --查看依赖关系
二、存储过程进阶 当然了,说先来说明下存储过程的格式语法规则: 代码如下: Create Procedure Procedure-name ( Input parameters , Output Parameters (If required))AsBegin Sql statement used in the stored procedureEnd
在这里我们利用一个普通的例子来说明: 代码如下: /* Getstudentname is the name of the stored procedure*/ Create PROCEDURE Getstudentname( @studentid INT --Input parameter , Studentid of the student ) AS BEGIN SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END
当然了,这里的@studentid参数只是一个传入的参数,但是如果想回传一个值,那么就需要利用到out参数来实现,具体的实现代码如下: 代码如下: /* GetstudentnameInOutputVariable is the name of the stored procedure which uses output variable @Studentname to collect the student name returns by the stored procedure */ Create PROCEDURE GetstudentnameInOutputVariable ( @studentid INT, --Input parameter , Studentid of the student @studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END
从上面的代码,可以看出out参数的具体用法,但是如果想在SQL服务器端执行这段代码,那该如何进行呢? 其实,一说到这,稍微麻烦一点,如果是只有in参数,那么只需要利用execute/exec 后面加上存储过程的名称,里面给参数赋值即可;但是如果不仅有in参数,而且有out参数,这个该怎么来弄呢? 下面通过一个具体的实例来详细的描述用法: 代码如下: Alter PROCEDURE GetstudentnameInOutputVariable ( @studentid INT, --Input parameter , Studentid of the student @studentname VARCHAR (200) OUT, -- Output parameter to collect the student name @StudentEmail VARCHAR (200)OUT -- Output Parameter to collect the student email ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname, @StudentEmail=email FROM tbl_Students WHERE studentid=@studentid END