sql下,如下sql:
declare @sql varchar(2000)
set @sql='select * from Sysobjects'
exec(@sql)
在此如果不加括号,如(exec @sql),就会提示"找不到存储过程 'select * from Sysobjects'。",加上括号则正常.此是否为sql语法规则?知道的请解释下!
执行存储过程或者函数,不用加挎号,否则要加
见 http://msdn.microsoft.com/en-us/library/ms188332.aspx 下面是Exec 的语法
你从这个语法定义可以看出,如果不加挎号, SQL SERVER 就会认为Exec 后面跟的是存储过程或者函数,所以才会出现找不到存储过程的错误。
Execute a stored procedure or function
[ { EXEC | EXECUTE } ]
{
[ @return_status = ]
{ module_name [ ;number ] | @module_name_var }
[ [ @parameter = ] { value
| @variable [ OUTPUT ]
| [ DEFAULT ]
}
]
[ ,...n ]
[ WITH RECOMPILE ]
}
[;]
Execute a character string
{ EXEC | EXECUTE }
( { @string_variable | [ N ]'tsql_string' } [ + ...n ] )
[ AS { LOGIN | USER } = ' name ' ]
[;]
Execute a pass-through command against a linked server
{ EXEC | EXECUTE }
( { @string_variable | [ N ] 'command_string [ ? ] ' } [ + ...n ]
[ { , { value | @variable [ OUTPUT ] } } [ ...n ] ]
)
[ AS { LOGIN | USER } = ' name ' ]
[ AT linked_server_name ]
[;]
declare @sql varchar(2000) 声明变量
set @sql='select * from Sysobjects' 接受结果
exec(@sql) 调用存储过程
@sql 必须有@ , 名字随便,规定.