且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

T-SQL:无法将串联字符串作为参数传递给存储过程

更新时间:2023-02-12 17:00:51

EXECUTE 语句的语法与其他语句(如 SELECT 和 SET)不同.例如,观察以下两页顶部的语法部分.

The EXECUTE statement simply has a different grammar then other statements like SELECT and SET. For instance, observe the syntax section at the top of the following two pages.

EXECUTE 语句:http://msdn.microsoft.com/en-us/library/ms188332.aspx

EXECUTE statement: http://msdn.microsoft.com/en-us/library/ms188332.aspx

SET 声明:http://msdn.microsoft.com/en-us/library/ms189484.aspx

EXECUTE 的语法只接受一个

The syntax for EXECUTE only accepts a value

[[@parameter =] { |@多变的[输出] |[默认]]

[[@parameter =] {value | @variable [OUTPUT] | [DEFAULT]]

而 SET 的语法接受一个表达式

Whereas the syntax for SET accepts an expression

{@local_variable = 表达式}

{@local_variable = expression}

一个值基本上只是一个硬编码的常量,但一个表达式将被评估.这就像拥有 varchar 'SELECT 1 + 1'.现在它只是一个 varchar 值.但是,您可以像这样评估字符串:

A value is basically just a hard coded constant, but an expression is going to be evaluated. It's like having the varchar 'SELECT 1 + 1'. It's just a varchar value right now. However, you can evaluate the string like this:

EXEC('SELECT 1 + 1')

我想我所指出的只是 EXEC 命令根据定义不允许表达式,而您显然已经发现了这一点.我不知道 T-SQL 的开发人员在这样做时的意图是什么.我想如果你允许在存储过程的参数列表中的子查询中抛出子查询,语法就会失控.

I suppose all I'm pointing out is that the EXEC command doesn't allow expressions by definition, which you apparently found out already. I don't know what the intention of the developers of T-SQL where when they made it that way. I suppose the grammar would just get out of hand if you where allowed to throw subqueries within subqueries in the parameter list of a stored procedure.

T-SQL 表达式:http://msdn.microsoft.com/en-us/library/ms190286.aspx