2017-09-27 8 views
0

私はexec文の一部として実行する必要がある次のSQLを持っています。私が苦労しているのは、アポストロフィを正しく得ることです。 1つのアポストロフィでそれを単独で実行すると、文は正常に実行され、2つを持つことでそれをエスケープして動作させると思った。アポストロフィ、EXECでの処理

アイデア?

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

実行しようとしているステートメントを印刷し、手動でそれを視覚的にスキャンすると便利です。たとえば、私がそれをすると、5行目のエラー( 'FROM sys.dm_os_sys_info)を稼働時間として簡単に見ることができます。 'は構文的には正しくありません。私はあなたのクエリの始まりは 'SELECT(SELECT')でなければならず、ここでは2番目の「select」を省略していると思います。調整してもクエリは正常に実行されます – ZLK

答えて

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') 
+0

ありがとうございます:) – Philip

関連する問題