我有以下SQL,我需要作为exec语句的一部分运行。我正在努力的是让撇号正确。如果我只用1个撇号单独运行它,那么该语句就可以正常运行了,而且我认为有2个语句可以将它排除在外以允许它工作。
有什么想法吗?
exec
('
SELECT COALESCE(NULLIF(CAST((DATEDIFF(mi,sqlserver_start_time,GetDate())/1440 ) as varchar),''0'') + '' day(s), '','''')
+ COALESCE(NULLIF(CAST(((DATEDIFF(mi,sqlserver_start_time,GetDate())%1440)/60) as varchar),''0'') + '' hour(s), '','''')
+ CAST((DATEDIFF(mi,sqlserver_start_time,GetDate())%60) as varchar) + ''min'' as Uptime
FROM sys.dm_os_sys_info) as Uptime,
CAST(LEFT((RIGHT(@@version, LEN(@@version)- 3 -charindex ('' ON '',@@VERSION))),CHARINDEX(''<'',(RIGHT(@@version, LEN(@@version)- 3 -charindex ('' ON '',@@VERSION))),0)-2) as nvarchar(300)) as OS,
cast(serverproperty(''MachineName'') as nvarchar(255)) MachineName,
cast(serverproperty(''ServerName'') as nvarchar(255)) ServerInstanceName,
cast(LEFT(@@VERSION,CHARINDEX(''('',@@VERSION,0)-2) as nvarchar(300)) as Version,
cast(replace(cast(serverproperty(''Edition'')as varchar),''Edition'','''') as varchar(max)) Edition,
cast(serverproperty(''productVersion'') as nvarchar(255)) ProductBuildLevel,
cast(serverproperty(''productLevel'') as nvarchar(255)) SPLevel,
cast(serverproperty(''Collation'') as nvarchar(255)) CollationType,
cast(serverproperty(''IsClustered'') as nvarchar(255)) [IsClustered],
cast((select physical_memory_kb/1024 FROM sys.dm_os_sys_info) as int) AS PhysicalMemory,
cast((select value from sys.configurations where name = ''min server memory (MB)'') as int) as MinMemory,
cast((select value from sys.configurations where name = ''max server memory (MB)'') as int) as MaxMemory,
cast((select cpu_count FROM sys.dm_os_sys_info) as int) AS LogicalCPUCount,
cast((select hyperthread_ratio FROM sys.dm_os_sys_info) as int) AS HyperthreadRatio,
cast((select cpu_count/hyperthread_ratio FROM sys.dm_os_sys_info) as int) AS PhysicalCPUCount
')
我得到的错误是:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'as'.
答案 0 :(得分:1)
您发布的查询中有几处错误。尝试
EXEC ('SELECT COALESCE(NULLIF(CAST((DATEDIFF(mi,sqlserver_start_time,GetDate())/1440 ) as varchar),''0'') + '' day(s), '','''')
+ COALESCE(NULLIF(CAST(((DATEDIFF(mi,sqlserver_start_time,GetDate())%1440)/60) as varchar),''0'') + '' hour(s), '','''')
+ CAST((DATEDIFF(mi,sqlserver_start_time,GetDate())%60) as varchar) + ''min'' as Uptime,
CAST(LEFT((RIGHT(@@version, LEN(@@version)- 3 -charindex ('' ON '',@@VERSION))),CHARINDEX(''<'',(RIGHT(@@version, LEN(@@version)- 3 -charindex ('' ON '',@@VERSION))),0)-2) as nvarchar(300)) as OS,
cast(serverproperty(''MachineName'') as nvarchar(255)) MachineName,
cast(serverproperty(''ServerName'') as nvarchar(255)) ServerInstanceName,
cast(LEFT(@@VERSION,CHARINDEX(''('',@@VERSION,0)-2) as nvarchar(300)) as Version,
cast(replace(cast(serverproperty(''Edition'')as varchar),''Edition'','''') as varchar(max)) Edition,
cast(serverproperty(''productVersion'') as nvarchar(255)) ProductBuildLevel,
cast(serverproperty(''productLevel'') as nvarchar(255)) SPLevel,
cast(serverproperty(''Collation'') as nvarchar(255)) CollationType,
cast(serverproperty(''IsClustered'') as nvarchar(255)) [IsClustered],
cast((select physical_memory_kb/1024 FROM sys.dm_os_sys_info) as int) AS PhysicalMemory,
cast((select value from sys.configurations where name = ''min server memory (MB)'') as int) as MinMemory,
cast((select value from sys.configurations where name = ''max server memory (MB)'') as int) as MaxMemory,
cast((select cpu_count FROM sys.dm_os_sys_info) as int) AS LogicalCPUCount,
cast((select hyperthread_ratio FROM sys.dm_os_sys_info) as int) AS HyperthreadRatio,
cast((select cpu_count/hyperthread_ratio FROM sys.dm_os_sys_info) as int) AS PhysicalCPUCount
FROM sys.dm_os_sys_info as Uptime')