Criei uma stored procedure que peguei no curso da Erin Stellato no Pluralsight para relatar se há alguma alteração de configuração no servidor como um job noturno da seguinte forma:
CREATEPROCEDURE dbo.usp_SysConfigReport1
(
@RecentDate DATETIME,
@OlderDate DATETIME
)
AS
BEGIN;
IF
@RecentDate IS NULL
OR @OlderDate IS NULL
BEGIN;
RAISERROR('Input parameters cannot be NULL', 16, 1);
RETURN;
END;
SELECT
@@SERVERNAME ServerName,
CaptureDate,
[O].[Name],
[O].[Value] AS "OlderValue",
[O].[ValueInUse] AS"OlderValueInUse",
[R].[Value] AS "RecentValue",
[R].[ValueInUse] AS "RecentValueInUse"
FROM [dbo].[ConfigData] O
JOIN
(SELECT [ConfigurationID], [Value], [ValueInUse]
FROM [dbo].[ConfigData]
WHERE [CaptureDate] = @RecentDate) R on [O].[ConfigurationID] = [R].[ConfigurationID]
WHERE [O].[CaptureDate] = @OlderDate
AND (([R].[Value] <> [O].[Value]) OR ([R].[ValueInUse] <> [O].[ValueInUse]))
END
--------
--Capture the capture dates into #temp table1
SELECT DISTINCT
[CaptureDate]
INTO #CapturedDates
FROM [dbo].[ConfigData]
ORDER BY [CaptureDate]
--Create temp table2
CREATE TABLE #CapturedDates2 (
ServerName NVARCHAR(20)
,[CaptureDate] DATETIMEOFFSET
,Name NVARCHAR(1000)
,[OlderValue] INT
,[OlderValueInUse] INT
,[RecentValue] INT
,RecentValueInUse INT
)
------Inserting into Temp2 gin=ving error message as below
DECLARE @MINCapturedDate DATETIME
DECLARE @MAXCapturedDate DATETIME
SELECT @MINCapturedDate =(SELECT CAST(MIN([CaptureDate]) AS DATETIME) FROM #CapturedDates)
SELECT @MAXCapturedDate =(SELECT CAST(MAX([CaptureDate]) AS DATETIME) FROM #CapturedDates)
INSERT INTO #CapturedDates2
EXEC SYSDBA.dbo.usp_SysConfigReport1 @MINCapturedDate ,@MAXCapturedDate
Mas me dá a seguinte mensagem de erro:
Msg 257, Nível 16, Estado 3, Procedimento usp_SysConfigReport1, Linha 19 A conversão implícita do tipo de dados sql_variant para int não é permitida. Use a função CONVERT para executar esta consulta.
EDIT: Incluir definição de ConfigData
CREATE TABLE [dbo].[ConfigData]
( [ConfigurationID] [INT] NOT NULL,
[Name] [NVARCHAR](35) NOT NULL,
[Value] [SQL_VARIANT] NULL,
[ValueInUse] [SQL_VARIANT] NULL,
[CaptureDate] [DATETIME] NULL ) ON [PRIMARY]