Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 计算SQL Server 2008中存储在字符串变量中的表达式_Sql Server_Sql Server 2008_Stored Procedures - Fatal编程技术网

Sql server 计算SQL Server 2008中存储在字符串变量中的表达式

Sql server 计算SQL Server 2008中存储在字符串变量中的表达式,sql-server,sql-server-2008,stored-procedures,Sql Server,Sql Server 2008,Stored Procedures,我在字符串变量中存储了一个条件表达式 例如: declare @x int=501,@y int=0,@str varchar(max) set @str='if @x > 500 set @y=1000 else set @y=500' 有人能告诉我如何计算这个字符串,并从存储过程中获取SQL Server 2008中的结果吗 提前谢谢 SD您需要使用 [尽管如此,我还是会检查为什么需要这样做…]您需要使用 [虽然,我会检查您为什么需要这样做…]您需要sp_executesql,

我在字符串变量中存储了一个条件表达式

例如:

declare @x int=501,@y int=0,@str varchar(max)

set @str='if @x > 500 set @y=1000 else set @y=500'
有人能告诉我如何计算这个字符串,并从存储过程中获取SQL Server 2008中的结果吗

提前谢谢


SD

您需要使用


[尽管如此,我还是会检查为什么需要这样做…]

您需要使用


[虽然,我会检查您为什么需要这样做…]

您需要sp_executesql,但它有一些细微差别,特别是在传入变量和导出值方面

declare @x int=501,@y int=0,@str Nvarchar(max)
set @str='if @x > 500 set @y=1000 else set @y=500'

exec sp_executesql @str, N'@x int, @y int output', @x, @y output
select @y

-- result
1000
请注意,@str已被重新定义为NVARCHAR。sp_executesql希望查询为NVARCHAR。

第二点需要注意的是,
output
子句必须对任何需要在
sp_executesql
调用中保持值更改的变量使用两次您需要sp_executesql,但它有一些细微差别,特别是在传入变量和取出值时

declare @x int=501,@y int=0,@str Nvarchar(max)
set @str='if @x > 500 set @y=1000 else set @y=500'

exec sp_executesql @str, N'@x int, @y int output', @x, @y output
select @y

-- result
1000
请注意,@str已被重新定义为NVARCHAR。sp_executesql希望查询为NVARCHAR。

第二点需要注意的是,
output
子句必须对任何需要在
sp_executesql
调用中保持值更改的变量使用两次。我也怀疑是故意破坏公物。我怀疑是故意破坏