execute语句不接受参数

时间:2015-05-14 17:29:41

标签: sql-server sql-server-2008

我正在尝试执行下面给出的代码,执行语句没有采用这些参数。

将这些参数用于执行语句的另一种方法是什么?

DECLARE @ver nvarchar(128)

SET @ver = CAST(serverproperty('ProductVersion') AS nvarchar)

SET @ver = SUBSTRING(@ver, 1, CHARINDEX('.', @ver) - 1)

SELECT @ver



DECLARE @chvCpuCount AS VARCHAR(256)

DECLARE @bintMemory AS BIGINT


IF @ver < 11

Execute('SELECT @chvCpuCount = cpu_count

, @bintMemory = (physical_memory_in_bytes / 1048576)

FROM master.sys.dm_os_sys_info')

else

Execute('SELECT @chvCpuCount = cpu_count

, @bintMemory = (physical_memory_kb / 1048.576)

FROM master.sys.dm_os_sys_info')

SELECT @chvCpuCount, @bintMemory

谢谢!

1 个答案:

答案 0 :(得分:2)

您不需要为此使用动态SQL。只是做

IF @ver < 11

   SELECT @chvCpuCount = cpu_count, @bintMemory = (physical_memory_in_bytes / 1048576)
   FROM master.sys.dm_os_sys_info

else

   SELECT @chvCpuCount = cpu_count, @bintMemory = (physical_memory_kb / 1048.576)
   FROM master.sys.dm_os_sys_info