不允许隐式转换,while 循环

Implicit conversion not allowed, while loop

我正在尝试执行下面的查询,但我一直收到此错误:

消息 257,级别 16,状态 3,过程 spRptTenRentRolldbs1,第 802 行 [批处理开始第 3 行] 不允许从数据类型 datetime 到 int 的隐式转换。使用 CONVERT 函数 运行 此查询。

看来我每次循环都会遇到大约 1 个错误。

USE [DBs1]
GO

SELECT ROW_NUMBER() OVER (ORDER BY siteid) AS rownum, siteid INTO rowsiteRentRoll FROM dbo.sites

CREATE TABLE temptableRentRoll
(
    SiteID1 INT,
    LedgerID INT,
    sUnit VARCHAR (200),
    sSize VARCHAR (200),
    Area1 DECIMAL (19,2),
    UnitID INT,
    SiteID INT,
    EmployeeID INT ,
    UnitTypeID INT,
    UnitZoneID VARCHAR (200),
    MapRAID_Row VARCHAR (200),
    MapRAID_Aisle VARCHAR (200),
    sTrackingCode VARCHAR (200),
    sRFID VARCHAR (200),
    sGlobalUnitName VARCHAR (200),
    TransferredTo_sGlobalSiteNum VARCHAR (200),
    TransferredFrom_sGlobalSiteNum VARCHAR (200),
    dTransferred DATETIME ,
    TransferredBy_EmployeeID INT,
    sUnitName VARCHAR (200),
    iWalkThruOrder INT,
    iFloor INT,
    dcWidth INT,
    dcLength INT,
    dcMapTop VARCHAR (200),
    dcMapLeft VARCHAR (200),
    dcMapTheta VARCHAR (200),
    bMapReversWL VARCHAR (200),
    dcPushRate DECIMAL (19,2),
    dcStdRate DECIMAL (19,2),
    dcStdWeeklyRate DECIMAL (19,2),
    dcStdSecDep DECIMAL (19,2),
    dcStdLateFee DECIMAL (19,2),
    bPower INT,
    bClimate INT,
    bInside INT,
    bAlarm INT,
    bRentable INT,
    bRented INT,
    dCreated DATETIME,
    dDeleted DATETIME,
    sUnitNote VARCHAR (600),
    dUnitNote DATETIME,
    sOSSType VARCHAR (200),
    bCorporate VARCHAR (200),
    bMobile VARCHAR (200),
    dBuilt DATETIME,
    dFirstInService DATETIME,
    iMobileStatus VARCHAR (200),
    bDamaged VARCHAR (200),
    dDamaged DATETIME,
    bCollapsible VARCHAR (200),
    EmployeeID_BuiltBy VARCHAR (200),
    bPermanent VARCHAR (200),
    dUpdated DATETIME,
    uTS VARCHAR(200),
    OldPK INT,
    MarketID_Whse INT,
    sUnitDesc VARCHAR (200),
    iEntryLoc VARCHAR (200),
    dArchived DATETIME,
    bExcludeFromSqftReports VARCHAR (200),
    bExcludeFromWebsite VARCHAR (200),
    bNotReadyToRent VARCHAR (200),
    dcSchedRateMonthly DECIMAL (19,2),
    dSchedRateMonthStrt DATETIME,
    dcSchedRateWeekly  DECIMAL (19,2),
    dSchedRateWeeklyStrt DATETIME,
    iADA INT,
    iDoorType VARCHAR (200),
    iVehicleStorageAllowed VARCHAR (200),
    iDaysVacant VARCHAR (200),
    dcWebRate DECIMAL (19,2),
    dcWebPushRate DECIMAL (19,2),
    LockID VARCHAR (200),
    Area DECIMAL (19,2),
    sTaxExempt VARCHAR (200),
    sTenant VARCHAR (400),
    sCompany VARCHAR (400),
    iAnnivDays INT,
    dLeaseDate DATETIME,
    dcSecDep DECIMAL (19,2),
    dcStandardRate DECIMAL (19,2),
    dcRent DECIMAL (19,2),
    dcVar DECIMAL (19,2),
    dSchedRentStrt DATETIME,
    dcSchedRent DECIMAL (19,2),
    dcChargeBalance DECIMAL (19,2),
    dPaidThru DATETIME,
    dcPrePaidRentLiability DECIMAL (19,2),
    CreditCardID DECIMAL (19,2),
    dRentLastChanged DATETIME,
    DaysSame DATETIME,
    dSchedOut DATETIME,
    TenantID INT,
    sEmail VARCHAR (200),
    dcInsurPremium DECIMAL (19,2),
    dcPushRateAtMoveIn DECIMAL (19,2),
    iAutoBillType INT

)


DECLARE @enddate datetime
SET @enddate = DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))

DECLARE @maxrow INT
SET @maxrow = (SELECT COUNT(siteid) FROM rowsiteRentRoll)

DECLARE @row INT
SET @row = 1


WHILE(@row <= @maxrow)
BEGIN

DECLARE @siteidrow INT
SET @siteidrow = (SELECT siteid FROM rowsiteRentRoll WHERE rownum = @row)

INSERT INTO temptableRentRoll 
(
    SiteID1,
    LedgerID,
    sUnit,
    sSize,
    Area1,
    UnitID,
    SiteID,
    EmployeeID,
    UnitTypeID,
    UnitZoneID,
    MapRAID_Row,
    MapRAID_Aisle,
    sTrackingCode,
    sRFID,
    sGlobalUnitName,
    TransferredTo_sGlobalSiteNum,
    TransferredFrom_sGlobalSiteNum,
    dTransferred,
    TransferredBy_EmployeeID,
    sUnitName,
    iWalkThruOrder,
    iFloor,
    dcWidth,
    dcLength,
    dcMapTop,
    dcMapLeft,
    dcMapTheta,
    bMapReversWL,
    dcPushRate,
    dcStdRate,
    dcStdWeeklyRate,
    dcStdSecDep,
    dcStdLateFee,
    bPower,
    bClimate,
    bInside,
    bAlarm,
    bRentable,
    bRented,
    dCreated,
    dDeleted,
    sUnitNote,
    dUnitNote,
    sOSSType,
    bCorporate,
    bMobile,
    dBuilt,
    dFirstInService,
    iMobileStatus,
    bDamaged,
    dDamaged,
    bCollapsible,
    EmployeeID_BuiltBy,
    bPermanent,
    dUpdated,
    uTS,
    OldPK,
    MarketID_Whse,
    sUnitDesc,
    iEntryLoc,
    dArchived,
    bExcludeFromSqftReports,
    bExcludeFromWebsite,
    bNotReadyToRent,
    dcSchedRateMonthly,
    dSchedRateMonthStrt,
    dcSchedRateWeekly,
    dSchedRateWeeklyStrt,
    iADA,
    iDoorType,
    iVehicleStorageAllowed,
    iDaysVacant,
    dcWebRate,
    dcWebPushRate,
    LockID,
    Area,
    sTaxExempt,
    sTenant,
    sCompany,
    iAnnivDays,
    dLeaseDate,
    dcSecDep,
    dcStandardRate,
    dcRent,
    dcVar,
    dSchedRentStrt,
    dcSchedRent,
    dcChargeBalance,
    dPaidThru,
    dcPrePaidRentLiability,
    CreditCardID,
    dRentLastChanged,
    DaysSame,
    dSchedOut,
    TenantID,
    sEmail,
    dcInsurPremium,
    dcPushRateAtMoveIn,
    iAutoBillType
    )


EXEC dbo.spRptTenRentRolldbs1
        @SiteID = @siteidrow,
        @dEnd = @enddate,
        @Filter = '100'

SET @row = @row + 1

END

我了解到,如果 table 定义与插入的内容不匹配,就会发生这种情况,我检查了一下,一切都匹配。

此时我必须假设我对其中一列的数据类型进行了错误分类。我只是从我在输出中看到的内容出发,一些列只包含 NULL 值,所以我不确定它们是什么,不得不做出有根据的猜测。

不幸的是,这是利用了一个非常复杂的 SP,它引用了各种其他函数和 SP。我试图避免将每一列追溯到原点以确定数据类型。

如何读取错误以确定问题出在哪一列?我尝试转到查询(不存在)和存储过程(没有任何意义)中的第 802 行。我在想循环可能会使这变得复杂?

更新

我能够使用这个提取 SP 返回的所有数据类型:

EXEC sp_describe_first_result_set N'dbo.spRptTenRentRolldbs1'

我更新了我的代码以使用上面返回的确切数据类型:

SELECT ROW_NUMBER() OVER (ORDER BY siteid) AS rownum, siteid INTO rowsiteRentRoll FROM dbo.sites

CREATE TABLE temptableRentRoll
(
    SiteID1 int,
    LedgerID int,
    sUnit nvarchar(6),
    sSize varchar(4),
    Area1 money,
    UnitID int,
    SiteID int,
    EmployeeID int,
    UnitTypeID int,
    UnitZoneID int,
    MarketID_Whse int,
    MapRAID_Row int,
    MapRAID_Aisle int,
    sTrackingCode nvarchar(30),
    sRFID nvarchar(30),
    sGlobalUnitName nvarchar(30),
    TransferredTo_sGlobalSiteNum nvarchar(20),
    TransferredFrom_sGlobalSiteNum nvarchar(20),
    dTransferred datetime,
    TransferredBy_EmployeeID int,
    sUnitName nvarchar(6),
    iWalkThruOrder int,
    iFloor int,
    dcWidth money,
    dcLength money,
    dcMapTop money,
    dcMapLeft money,
    dcMapTheta money,
    bMapReversWL bit,
    dcPushRate money,
    dcStdRate money,
    dcStdWeeklyRate money,
    dcStdSecDep money,
    dcStdLateFee money,
    bPower bit,
    bClimate bit,
    bInside bit,
    bAlarm bit,
    bRentable bit,
    bRented bit,
    dCreated datetime,
    dDeleted datetime,
    sUnitNote nvarchar(150),
    dUnitNote datetime,
    sOSSType nvarchar(10),
    bCorporate bit,
    bMobile bit,
    dBuilt datetime,
    dFirstInService datetime,
    iMobileStatus int,
    bDamaged bit,
    dDamaged datetime,
    bCollapsible bit,
    EmployeeID_BuiltBy int,
    sUnitDesc nvarchar(50),
    iEntryLoc int,
    bExcludeFromSqftReports bit,
    bExcludeFromWebsite bit,
    bNotReadyToRent bit,
    iADA int,
    iDoorType int,
    iVehicleStorageAllowed int,
    dArchived datetime,
    bPermanent bit,
    dUpdated datetime,
    uTS binary (8),
    OldPK int,
    dcSchedRateMonthly money,
    dSchedRateMonthStrt datetime,
    dcSchedRateWeekly money,
    dSchedRateWeeklyStrt datetime,
    iDaysVacant int,
    dcWebRate money,
    dcWebPushRate money,
    LockID int,
    Area money,
    sTaxExempt nvarchar(1),
    sTenant nvarchar(100),
    sCompany nvarchar(80),
    iAnnivDays int,
    dLeaseDate datetime,
    dcSecDep money,
    dcStandardRate money,
    dcRent money,
    dcVar money,
    dSchedRentStrt datetime,
    dcSchedRent money,
    dcChargeBalance money,
    dPaidThru datetime,
    dcPrePaidRentLiability numeric(38,4),
    CreditCardID int,
    dRentLastChanged datetime,
    DaysSame int,
    dSchedOut datetime,
    TenantID int,
    sEmail nvarchar(100),
    dcInsurPremium money,
    dcPushRateAtMoveIn money,
    iAutoBillType int



)


DECLARE @enddate datetime
SET @enddate = DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))

DECLARE @maxrow INT
SET @maxrow = (SELECT COUNT(siteid) FROM rowsiteRentRoll)

DECLARE @row INT
SET @row = 1


WHILE(@row <= @maxrow)
BEGIN

DECLARE @siteidrow INT
SET @siteidrow = (SELECT siteid FROM rowsiteRentRoll WHERE rownum = @row)

INSERT INTO temptableRentRoll 
(
    SiteID1,
    LedgerID,
    sUnit,
    sSize,
    Area1,
    UnitID,
    SiteID,
    EmployeeID,
    UnitTypeID,
    UnitZoneID,
    MapRAID_Row,
    MapRAID_Aisle,
    sTrackingCode,
    sRFID,
    sGlobalUnitName,
    TransferredTo_sGlobalSiteNum,
    TransferredFrom_sGlobalSiteNum,
    dTransferred,
    TransferredBy_EmployeeID,
    sUnitName,
    iWalkThruOrder,
    iFloor,
    dcWidth,
    dcLength,
    dcMapTop,
    dcMapLeft,
    dcMapTheta,
    bMapReversWL,
    dcPushRate,
    dcStdRate,
    dcStdWeeklyRate,
    dcStdSecDep,
    dcStdLateFee,
    bPower,
    bClimate,
    bInside,
    bAlarm,
    bRentable,
    bRented,
    dCreated,
    dDeleted,
    sUnitNote,
    dUnitNote,
    sOSSType,
    bCorporate,
    bMobile,
    dBuilt,
    dFirstInService,
    iMobileStatus,
    bDamaged,
    dDamaged,
    bCollapsible,
    EmployeeID_BuiltBy,
    bPermanent,
    dUpdated,
    uTS,
    OldPK,
    MarketID_Whse,
    sUnitDesc,
    iEntryLoc,
    dArchived,
    bExcludeFromSqftReports,
    bExcludeFromWebsite,
    bNotReadyToRent,
    dcSchedRateMonthly,
    dSchedRateMonthStrt,
    dcSchedRateWeekly,
    dSchedRateWeeklyStrt,
    iADA,
    iDoorType,
    iVehicleStorageAllowed,
    iDaysVacant,
    dcWebRate,
    dcWebPushRate,
    LockID,
    Area,
    sTaxExempt,
    sTenant,
    sCompany,
    iAnnivDays,
    dLeaseDate,
    dcSecDep,
    dcStandardRate,
    dcRent,
    dcVar,
    dSchedRentStrt,
    dcSchedRent,
    dcChargeBalance,
    dPaidThru,
    dcPrePaidRentLiability,
    CreditCardID,
    dRentLastChanged,
    DaysSame,
    dSchedOut,
    TenantID,
    sEmail,
    dcInsurPremium,
    dcPushRateAtMoveIn,
    iAutoBillType
    )


EXEC dbo.spRptTenRentRolldbs1
        @SiteID = @siteidrow,
        @dEnd = @enddate,
        @Filter = '100'

SET @row = @row + 1

END

不知何故,我仍然在不同的批处理起始行收到相同的错误消息:

消息 257,级别 16,状态 3,过程 spRptTenRentRolldbs1,第 802 行 [批处理开始第 6 行] 不允许从数据类型 datetime 到 int 的隐式转换。使用 CONVERT 函数 运行 此查询。

据我所知这应该是不可能的,我提取了 SP 返回的确切数据类型并将它们作为我的 table 定义。这怎么可能???

对遇到类似问题的其他人的简洁回答...

我的数据类型出了问题。我不知道具体是什么问题,因为我找到了一种方法,可以直接从 SP 中用标签覆盖我所有的数据标签。

我运行这是为了得到SP返回的所有数据类型的列表:

EXEC sp_describe_first_result_set N'dbo.spRptTenRentRolldbs1'

然后我将结果集转储到 excel 中以快速重新创建我的 table 创建代码。然后我修改了查询以包含新的数据类型,"ultimately" 解决了我的问题。

我说 "ultimately" 是因为我 运行 遇到了一个额外的问题,我不小心将其中一列打乱了顺序,这导致了我最初收到的相同错误消息 - 所以它出现了这个特定错误并不一定意味着您有数据类型转换问题。

好像也可能是tablecreation/definition结构有问题