动态SQL,参数化查询

时间:2017-08-04 17:58:06

标签: sql-server parameter-passing dynamic-sql alwayson

我写了一个查询,它提取了SQL AlwaysON健康信息。剩下要做的唯一事情是在最后添加一个where子句来过滤AvailabilityGroupName和DatabaseName。它在我输入查询时有效:'select * from results where AvailabilityGroupName ='LAB-VIP-USADB'AND DatabaseName ='CPS''。但是当我在开始时将查询存储在nvarchar变量@sql中以及稍后执行存储在变量中的查询时,我收到错误消息“关键字'EXEC'附近的语法不正确”。我想要对查询进行参数化的原因是一个可用性组可以包含多个数据库。参数化的输入将来自PRTG,以便它查询该特定数据库的可用性组。

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)
             

					),

   
       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
					)
EXEC(@sql)

1 个答案:

答案 0 :(得分: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'.