我创建了一个存储过程,这是我在 Erin Stellato 的 Pluralsight 课程中学习的,用于报告服务器上是否有任何配置更改作为夜间作业,如下所示:
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
但它给了我以下错误信息:
消息 257,级别 16,状态 3,过程 usp_SysConfigReport1,第 19 行不允许从数据类型 sql_variant 到 int 的隐式转换。使用 CONVERT 函数运行此查询。
编辑:包括 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]
从您为 [dbo].[ConfigData] 提供的定义中可以看出,
Value
和ValueInUse
都被定义为SQL_VARIANT
数据类型。您收到的错误消息是:So
Value
和ValueInUse
are (以及、、和SQL_VARIANT
的派生值),但您希望将它们用作.OlderValue
OlderValueInUse
RecentValue
RecentValueInUse
INT
错误消息说您需要
SQL_VARIANT
使用该CONVERT
函数将 更改为 INT。选择 aSQL_VARIANT
并将其转换为INT
.将
CONVERT
数据类型(如果可能)从更改SQL_VARIANT
为INT
.只要确保
CONVERT
使用正确的数据类型,一切都会好起来的。