我有以下语句,当直接在查询窗口中执行时,会以电子邮件的形式向我发送正确的结果
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL Server Agent Notification',
@recipients = '<EMAIL ADDRESS>',
@body = 'Please see INT overflow candidates attached',
@query = '
declare @sql nvarchar(2000)=N''
IF ''''?'''' LIKE ''''PROD_%''''
BEGIN
USE [?];
SELECT
DB_NAME() AS DB
,OBJECT_SCHEMA_NAME(object_id) AS SchemaName
,OBJECT_NAME(object_id) AS TableName
,name AS ColumnName
,TYPE_NAME(system_type_id) AS ColumnType
,CAST(Seed_Value AS BIGINT) AS Seed_Value
,CAST(Increment_Value AS BIGINT) AS Increment_Value
,POWER(2.0, (max_length * 8 - 1)) AS MaxSize
,CAST(Last_value AS BIGINT) AS Last_value
,CAST(Last_value AS BIGINT) / POWER(2.0, (max_length * 8 - 1)) AS ratio
FROM sys.identity_columns
WHERE TYPE_NAME(system_type_id) = ''''int'''';
END''
DECLARE @ratios TABLE (
DB NVARCHAR(100),
SchemaName NVARCHAR(29),
TableName NVARCHAR(100),
ColumnName NVARCHAR(100),
ColumnType NVARCHAR(20),
Seed_Value BIGINT,
Increment_Value BIGINT,
MaxSize BIGINT,
Last_value BIGINT,
ratio FLOAT);
INSERT INTO @ratios EXEC sp_MSforeachdb @sql
SELECT
*
FROM @ratios
WHERE ColumnType = ''int''
AND DB LIKE ''PROD_%''
AND ratio >= 0.95',
@execute_query_database = 'master',
@subject = 'INT Overflow candidates',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'OverflowCandidates.csv',
@query_result_header = 1,
@query_result_width = 512,
@query_result_separator = ';',
@exclude_query_output = 1,
@append_query_error = 1,
@query_no_truncate = 0,
@query_result_no_padding = 1;
结果:
(6546 rows affected)
DB SchemaName TableName ColumnName
ColumnType Seed_Value Increment_Value MaxSize Last_value ratio
---------------------------------------------------------------------------------------------------- ----------------------------- ---------------------------------------------------------------------------------------------------- ------------------------
---------------------------------------------------------------------------- -------------------- -------------------- -------------------- -------------------- -------------------- ------------------------
PROD_XXX dbo CPM#Balance_BAK_DBA_20220527 BalanceID
int 1 1 2147483648 2147254332 0.99989321641623974
PROD_XXX dbo CPM#Balance_BAK_20220527_SE BalanceID
int 1 1 2147483648 2147254332 0.99989321641623974
(2 rows affected)
现在我希望定期执行(每周一次)。当我设置相应的服务器代理作业时,我确实收到一封电子邮件,但似乎查询未完全执行。
(0 rows affected)
DB SchemaName TableName ColumnName
ColumnType Seed_Value Increment_Value MaxSize Last_value ratio
---------------------------------------------------------------------------------------------------- ----------------------------- ---------------------------------------------------------------------------------------------------- ------------------------
---------------------------------------------------------------------------- -------------------- -------------------- -------------------- -------------------- -------------------- ------------------------
(0 rows affected)
有人知道为什么或如何解决这个问题吗?
我现在找到了解决方案,但不是使用 SQL Server 代理。 我创建了一个计划任务和一个 Powershell 脚本来执行查询。现在我收到了包含正确结果的邮件。