2017-08-04 23 views
0

SQL AlwaysONの正常性情報を取得するクエリを作成しました。残された唯一のことは、最後にWhere句を追加してAvailabilityGroupNameとDatabaseNameをフィルタリングすることです。これは、クエリに「select * from results」と入力したときに動作します。ここで、AvailabilityGroupName = 'LAB-VIP-USADB'とDatabaseName = 'CPS'です。しかし、最初にnvarchar変数@sqlにクエリを格納し、後で変数に格納されたクエリを実行すると、 "EXEC 'というキーワードの近くに構文が正しくないというエラーメッセージが表示されました。私が照会をパラメタリ化する理由は、1つの可用性グループに複数のデータベースを含めることができるためです。パラメタ化された入力はPRTGから来て、その特定のデータベースの可用性グループを照会します。あなたはそれを行うことはできません動的SQL、パラメータ化クエリ

DECLARE @sql NVARCHAR(MAX) 
 

 
SET @sql='SELECT * FROM Results WHERE AvailabilityGroupName = ''LAB-VIP-USADB'' AND DatabaseName = ''CPS''' 
 
--select @sql 
 
; 
 
WITH basicaginfo AS(
 
SELECT 
 
     
 
     ag.name AS AvailabilityGroupName, 
 
     cs.replica_server_name AS NodeName, 
 
     rs.role_desc, 
 
     rs.synchronization_health_desc, 
 
     DB_NAME(drs.database_id) AS DatabaseName 
 
     
 

 

 
FROM 
 
     sys.availability_groups ag 
 
        JOIN 
 
     sys.dm_hadr_availability_replica_cluster_states cs on ag.group_id = cs.group_id 
 
        JOIN 
 
     sys.dm_hadr_availability_replica_states rs ON (ag.group_id=rs.group_id AND cs.replica_id = rs.replica_id) 
 
        JOIN 
 
     sys.dm_hadr_database_replica_states drs ON (ag.group_id=drs.group_id AND cs.replica_id = drs.replica_id) 
 
       
 

 
\t \t \t \t \t), 
 

 
    
 
     AG_Stats AS 
 
        (
 
        SELECT AR.replica_server_name, 
 
           HARS.role_desc, 
 
           Db_name(DRS.database_id) [DBName], 
 
           DRS.last_commit_time 
 
        FROM sys.dm_hadr_database_replica_states DRS 
 
        INNER JOIN sys.availability_replicas AR ON DRS.replica_id = AR.replica_id 
 
        INNER JOIN sys.dm_hadr_availability_replica_states HARS ON AR.group_id = HARS.group_id 
 
          AND AR.replica_id = HARS.replica_id 
 
        ), 
 
     Pri_CommitTime AS 
 
        (
 
        SELECT replica_server_name 
 
           , DBName 
 
           , last_commit_time 
 
        FROM AG_Stats 
 
        WHERE role_desc = 'PRIMARY' 
 
        ), 
 
     Sec_CommitTime AS 
 
        (
 
        SELECT replica_server_name 
 
           , DBName 
 
           , last_commit_time 
 
        FROM AG_Stats 
 
        WHERE role_desc = 'SECONDARY' 
 
        ), 
 
\t \t Results AS 
 
\t \t \t \t \t (
 
\t \t \t \t \t SELECT 
 
\t \t \t \t \t \t AvailabilityGroupName, 
 
\t \t \t \t \t \t DatabaseName, 
 
\t \t \t \t \t \t [LAB-SCB-SQL01], 
 
\t \t \t \t \t \t [LAB-SCB-SQL02], 
 
\t \t \t \t \t \t [LAB-LAS-SQL01], 
 
\t \t \t \t \t \t [LAB-LAS-SQL02], 
 
\t \t \t \t \t \t [Max_Sync_Lag_Secs] 
 
\t \t \t \t \t FROM(
 
\t \t \t \t \t \t \t SELECT 
 
\t \t \t \t \t \t \t \t bb.AvailabilityGroupName, 
 
\t \t \t \t \t \t \t \t bb.DatabaseName, 
 
\t \t \t \t \t \t \t \t bb.NodeName, 
 
\t \t \t \t \t \t \t \t bb.synchronization_health_desc, 
 
\t \t \t \t \t \t \t \t MAX(DATEDIFF(ss,s.last_commit_time,p.last_commit_time)) OVER (PARTITION BY NULL) AS [Max_Sync_Lag_Secs] 
 
    
 
\t \t \t \t \t \t \t FROM 
 
\t \t \t \t \t \t \t \t basicaginfo bb 
 
\t \t \t \t \t \t \t \t \t LEFT JOIN 
 
\t \t \t \t \t \t \t \t Pri_CommitTime p ON p.DBName=bb.DatabaseName 
 
\t \t \t \t \t \t \t \t \t LEFT JOIN 
 
\t \t \t \t \t \t \t \t Sec_CommitTime s ON bb.NodeName = s.replica_server_name 
 
\t \t \t \t \t \t) AS Data 
 
\t \t \t \t \t PIVOT(
 

 
\t \t \t \t \t \t \t MAX(synchronization_health_desc) FOR [NodeName] IN([LAB-SCB-SQL01], [LAB-SCB-SQL02], [LAB-LAS-SQL01], [LAB-LAS-SQL02]) 
 
\t \t \t \t \t \t ) AS PivotedData 
 
\t \t \t \t \t) 
 
EXEC(@sql)

答えて

1

、CTEテーブルは、次のSQLコマンドにアクセスできます。

EXEC(@sql)コマンドを内部的に実行すると、SQLは複数のコマンドを実行します。

すべてのコードを変数に移動してみます。

DECLARE @sql NVARCHAR(MAX) 

SET @sql=N'WITH basicaginfo AS(
SELECT 

     ag.name AS AvailabilityGroupName, 
     cs.replica_server_name AS NodeName, 
     rs.role_desc, 
     rs.synchronization_health_desc, 
     DB_NAME(drs.database_id) AS DatabaseName 



FROM 
     sys.availability_groups ag 
        JOIN 
     sys.dm_hadr_availability_replica_cluster_states cs on ag.group_id = cs.group_id 
        JOIN 
     sys.dm_hadr_availability_replica_states rs ON (ag.group_id=rs.group_id AND cs.replica_id = rs.replica_id) 
        JOIN 
     sys.dm_hadr_database_replica_states drs ON (ag.group_id=drs.group_id AND cs.replica_id = drs.replica_id) 


        ), 


     AG_Stats AS 
        (
        SELECT AR.replica_server_name, 
           HARS.role_desc, 
           Db_name(DRS.database_id) [DBName], 
           DRS.last_commit_time 
        FROM sys.dm_hadr_database_replica_states DRS 
        INNER JOIN sys.availability_replicas AR ON DRS.replica_id = AR.replica_id 
        INNER JOIN sys.dm_hadr_availability_replica_states HARS ON AR.group_id = HARS.group_id 
          AND AR.replica_id = HARS.replica_id 
        ), 
     Pri_CommitTime AS 
        (
        SELECT replica_server_name 
           , DBName 
           , last_commit_time 
        FROM AG_Stats 
        WHERE role_desc = ''PRIMARY'' 
        ), 
     Sec_CommitTime AS 
        (
        SELECT replica_server_name 
           , DBName 
           , last_commit_time 
        FROM AG_Stats 
        WHERE role_desc = ''SECONDARY'' 
        ), 
     Results AS 
        (
        SELECT 
         AvailabilityGroupName, 
         DatabaseName, 
         [LAB-SCB-SQL01], 
         [LAB-SCB-SQL02], 
         [LAB-LAS-SQL01], 
         [LAB-LAS-SQL02], 
         [Max_Sync_Lag_Secs] 
        FROM(
          SELECT 
           bb.AvailabilityGroupName, 
           bb.DatabaseName, 
           bb.NodeName, 
           bb.synchronization_health_desc, 
           MAX(DATEDIFF(ss,s.last_commit_time,p.last_commit_time)) OVER (PARTITION BY NULL) AS [Max_Sync_Lag_Secs] 

          FROM 
           basicaginfo bb 
            LEFT JOIN 
           Pri_CommitTime p ON p.DBName=bb.DatabaseName 
            LEFT JOIN 
           Sec_CommitTime s ON bb.NodeName = s.replica_server_name 
         ) AS Data 
        PIVOT(

          MAX(synchronization_health_desc) FOR [NodeName] IN([LAB-SCB-SQL01], [LAB-SCB-SQL02], [LAB-LAS-SQL01], [LAB-LAS-SQL02]) 
         ) AS PivotedData 
        ) 
        SELECT * FROM Results WHERE AvailabilityGroupName = ''LAB-VIP-USADB'' AND DatabaseName = ''CPS''' 
EXEC(@sql) 

私の環境でこのコマンドを実行しようとしましたが、私はそれにテーブルがないため、エラーが発生しました。

Msg 208, Level 16, State 1, Line 1 
Invalid object name 'sys.availability_groups'.