是一个常量,tsql_string 可以是 nvarchar 或 varchar 数据类型。如果包含 n,则该字符串将解释为 nvarchar 数据类型,最大值为服务器的可用内存。如果字符串长度超过 4,000 个字符,则把多个局部变量串联起来用于 execute 字符串。
注释
如果过程名称的前三个字符为 sp_,sql server 会在 master 数据库中寻找该过程。如果没能找到合法的过程名称,sql server 会寻找所有者名称为 dbo 的过程。若要将存储过程名称解析为与系统存储过程同名的用户定义存储过程,请提供一个完全合法的过程名称。
参数可以通过利用 value 或 @parameter_name = value 来提供。参数不是事务的一个部分;因而如果事务中的参数值更改,且该事务在以后回滚,该参数值不会退回到以前的值。返回给调用方的值总是过程返回时的值。
当一个存储过程调用另一个存储过程时,会产生嵌套。当调用的过程开始执行时,嵌套级会增加,当调用过程执行结束时,嵌套级则会减少。嵌套级最高为32级,超过32级时,会导致整个调用过程链失败。当前的嵌套级存储在 @@nestlevel 函数中。
sql server 目前使用返回值 0 到 -14 来表示存储过程的执行状态。值 –15 到 -99 留作后用。有关保留的返回状态值的列表的更多信息,请参见 return。
因为远程存储过程和扩展存储过程不在事务的作用域中(除非在 begin distributed transaction 语句中发出或者是和不同的配置选项一起使用),所以通过调用执行的命令不能回滚。有关更多信息,请参见系统存储过程和 begin distributed transaction。
当使用游标变量时,如果执行的过程传递一个分配有游标的游标变量,就会出错。
在执行存储过程时,如果语句是批处理中的第一个语句,则不一定要指定 execute 关键字。
使用带字符串的 execute 命令
使用字符串串联运算符 (+) 为动态执行创建长字符串。每个字符串表达式可以是 unicode 与 non-unicode 数据类型的混合。
尽管每个 [n] 'tsql_string' 或 @string_variable 不得超过 8,000 个字节,sql server 语法分析器中对这种串联只进行逻辑处理而不占用物理内存。例如,该语句决不会生成长 16,000 个串联起来的字符串:
| exec('name_of_8000_char_string' + 'another_name_of_8000_char_string') |
在 execute 语句执行前,不会编译 execute 语句内的语句。
数据库环境的更改只在 execute 语句结束前有效。例如,在这个例子的 exec 后,数据库环境是 master:
| use master exec ("use pubs") select * from authors |
权限
存储过程的 execute 权限默认给该存储过程的所有者,该所有者可以将此权限转让给其他用户。当遇到 execute 语句时,即使 execute 语句是在存储过程中,也会检查在 execute 字符串内使用该语句的权限。当运行一个执行字符串的存储过程时,系统会在执行该过程的用户环境中,而不是在创建该过程的用户环境中检查权限。但是,如果某用户拥有两个存储过程,并且第一个过程调用第二个过程,则在第二个过程中不进行 execute 权限检查。
示例
a. 使用 execute 传递单个参数
showind 存储过程需要参数 (@tabname),它是一个表的名称。下面这个例子执行 showind 存储过程,以 titles 为参数值。
说明 showind 存储过程只是用来作为一个例子,pubs 数据库并没有此过程。
exec showind titles在执行过程中变量可以显式命名:exec showind @tabname = titles
如果这是 isql 脚本或批处理中第一个语句,则 exec 语句可以省略:
showind titles-或-showind @tabname = titles
b. 使用多个参数与一个输出参数
这个例子执行 roy_check 存储过程,传递三个参数。第三个参数 @pc 是输出参数。过程执行完后,返回变量可以从变量@percent
得到。
说明 roy_check 存储过程只是用作举例,pubs 数据库中并没有此过程。
declare @percent int
execute roy_check 'bu1032', 1050, @pc = @percent output
set percent = @percent
c.使用带一个变量的 execute 'tsql_string' 语句
这个例子显示 execute 语句如何处理动态生成的、含有变量的字符串。这个例子创建 tables_cursor 游标来保存所有用户定义表
(type = u) 的列表。
说明 此例子只用作举例。
declare tables_cursor cursor
for
select name from sysobjects where type = 'u'
open tables_cursor
declare @tablename sysname
fetch next from tables_cursor into @tablename
while (@@fetch_status <> -1)
begin
/* a @@fetch_status of -2 means that the row has been deleted.
there is no need to test for this because this loop drops all
user-defined tables. */.
exec ('drop table ' + @tablename)
fetch next from tables_cursor into @tablename
end
print 'all user-defined tables have been dropped from the database.'
deallocate tables_cursor
d.使用带远程存储过程的 execute 语句
这个例子在远程服务器 sqlserver1 上执行 checkcontract 存储过程,在 @retstat 中保存返回状态,说明运行成功或失败。
declare @retstat int
execute @retstat = sqlserver1.pubs.dbo.checkcontract '409-56-4008'
e. 使用带扩展存储过程的 execute 语句
下例使用 xp_cmdshell 扩展存储过程列出文件扩展名为 .exe 的所有文件的目录。
use master
execute xp_cmdshell 'dir *.exe'
f. 使用带一个存储过程变量的 execute 语句
这个例子创建一个代表存储过程名称的变量。
declare @proc_name varchar(30)
set @proc_name = 'sp_who'
exec @proc_name
g. 使用带 default 的 execute 语句
这个例子创建了一个存储过程,过程中第一个和第三个参数为默认值。当运行该过程时,如果调用时没有传递值或者指定了默认值,
这些默认值就会赋给第一个和第三个参数。注意 default 关键字有多种使用方法。
use pubs
if exists (select name from sysobjects
where name = 'proc_calculate_taxes' and type = 'p')
drop procedure proc_calculate_taxes
go
-- create the stored procedure.
create procedure proc_calculate_taxes (@p1 smallint = 42, @p2 char(1),
@p3 varchar(8) = 'car')
as
select *
from mytable
proc_calculate_taxes 存储过程可以以多种组合方式执行:
execute proc_calculate_taxes @p2 = 'a'
execute proc_calculate_taxes 69, 'b'
execute proc_calculate_taxes 69, 'c', 'house'
execute proc_calculate_taxes @p1 = default, @p2 = 'd'
execute proc_calculate_taxes default, @p3 = 'local', @p2 = 'e'
execute proc_calculate_taxes 69, 'f', @p3 = default
execute proc_calculate_taxes 95, 'g', default
execute proc_calculate_taxes default, 'h', default
execute proc_calculate_taxes default, 'i', @p3 = default