关于sql语句的疑问
sql2005下,如下sql:
declare @sql varchar(2000)
set @sql='select * from Sysobjects'
exec(@sql)
在此如果不加括号,如(exec @sql),就会提示"找不到存储过程 'select * from Sysobjects'。";
加上括号则可正常执行.
exec执行存储过程或者函数,不用加挎号,否则要加
见 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 ]
[;]