无法将char值转换为money。 char值的语法不正确

时间:2015-04-20 20:36:19

标签: sql sql-server sql-server-2012

我收到错误"无法将char值转换为money。 char值的语法不正确。",当我认为它是错误的。我们从主表执行select语句,加入另一个表,然后尝试将这些值插入另一个表。查询如下:

 *--CREATE TABLE [ETL_Test_Source].[ETL].[Master_BAR_INS_XFER_TXN_FACT_NPR_0_ETL_Test_Source_No_valid_subdivisions]
 --   (
 --     Appl_Database_Id VARCHAR(256) ,
 --     Automatic_Transfer_Amount MONEY ,
 --     Batch_Date_Id DATETIME ,
 --     Bill_Number VARCHAR(256) ,
 --     Collector_Id VARCHAR(256) ,
 --     Crp_Bot_Comp_Val_Id VARCHAR(256) ,
 --     Facility_Id VARCHAR(256) ,
 --     Insurance_Id VARCHAR(256) ,
 --     Manual_Transfer_Amount MONEY ,
 --     Net_Transfer_Amount MONEY ,
 --     Patient_Account_Id VARCHAR(256) ,
 --     Transaction_Urn VARCHAR(256)
 --   )  
DECLARE @Database VARCHAR(256) = 'ETL_Test_Source' ,
    @RunDateTime DATETIME = 'Apr 19 2015  4:56PM' ,
    @ScheduleDateTime DATETIME = 'Apr 19 2015  4:56PM' ,
    @Subdivision VARCHAR(256) = 'No_valid_subdivisions' ,
    @DateAwareFromDateTime DATETIME = 'Jan  1 2000 12:00AM' ,
    @DateAwareThruDateTime DATETIME = 'Jan  1 2101 12:00AM';
INSERT  INTO [ETL_Test_Source].[ETL].[Master_BAR_INS_XFER_TXN_FACT_NPR_0_ETL_Test_Source_No_valid_subdivisions]
        ( [Appl_Database_Id] ,
          [Automatic_Transfer_Amount] ,
          [Batch_Date_Id] ,
          [Bill_Number] ,
          [Collector_Id] ,
          [Crp_Bot_Comp_Val_Id] ,
          [Facility_Id] ,
          [Insurance_Id] ,
          [Manual_Transfer_Amount] ,
          [Net_Transfer_Amount] ,
          [Patient_Account_Id] ,
          [Transaction_Urn]
        )
        SELECT  XFRTXN.SourceID ,
                XFRTXN.AutoXferAmount ,
                COLL.BatchDateTime ,
                COLL.BillNumber ,
                LEDG.Collector ,  
                FINS.Corporation ,
                VISIT.FacilityID ,
                XFRTXN.InsuranceID ,
                XFRTXN.ManualEdit ,
                XFRTXN.AutoXferAmount ,
                XFRTXN.BillingID ,
                XFRTXN.TransactionID
        FROM    [dbo].[BarTxnInsuranceDetail] AS XFRTXN
                LEFT OUTER JOIN [dbo].[BarVisits] AS VISIT ON [VISIT].[BillingID] = [XFRTXN].[BillingID]
                                                             AND [VISIT].[SourceID] = [XFRTXN].[SourceID]
                LEFT OUTER JOIN [dbo].[BarVisitFinancialData] AS FINS ON [FINS].[BillingID] = [XFRTXN].[BillingID]
                                                             AND [FINS].[SourceID] = [XFRTXN].[SourceID]
                INNER JOIN [dbo].[BarInsuranceLedger] AS LEDG ON [LEDG].[BillingID] = [XFRTXN].[BillingID]
                                                              AND [LEDG].[InsuranceID] = [XFRTXN].[InsuranceID]
                                                              AND [LEDG].[SourceID] = [XFRTXN].[SourceID]
                INNER JOIN [dbo].[BarCollectionTransactions] AS COLL ON [COLL].[BillingID] = [XFRTXN].[BillingID]
                                                              AND [COLL].[SourceID] = [XFRTXN].[SourceID]
                                                              AND [COLL].[TransactionID] = [XFRTXN].[TransactionID]
        WHERE   COLL.Type IN ( 'p', 'A', 'R', 'P', 'q', 'm', 'v', 'b' )
                AND COLL.BatchDateTime BETWEEN @DateAwareFromDateTime
                                       AND     @DateAwareThruDateTime*
看来,虽然COLL.SourceID虽然在COLL中定义为VARCHAR,但我们将其插入到表中,但SQL似乎认为它是一种MONEY类型。事实证明,我们碰巧选择的所有值都是整数。此查询现在正在生成此错误,但之前没有。现在,当查询编辑为以下内容时:

 --CREATE TABLE [ETL_Test_Source].[ETL].[Master_BAR_INS_XFER_TXN_FACT_NPR_0_ETL_Test_Source_No_valid_subdivisions]
 --   (
 --     Appl_Database_Id VARCHAR(256) ,
 --     Automatic_Transfer_Amount MONEY ,
 --     Batch_Date_Id DATETIME ,
 --     Bill_Number VARCHAR(256) ,
 --     Collector_Id VARCHAR(256) ,
 --     Crp_Bot_Comp_Val_Id VARCHAR(256) ,
 --     Facility_Id VARCHAR(256) ,
 --     Insurance_Id VARCHAR(256) ,
 --     Manual_Transfer_Amount MONEY ,
 --     Net_Transfer_Amount MONEY ,
 --     Patient_Account_Id VARCHAR(256) ,
 --     Transaction_Urn VARCHAR(256)
 --   )  
DECLARE @Database VARCHAR(256) = 'ETL_Test_Source' ,
    @RunDateTime DATETIME = 'Apr 19 2015  4:56PM' ,
    @ScheduleDateTime DATETIME = 'Apr 19 2015  4:56PM' ,
    @Subdivision VARCHAR(256) = 'No_valid_subdivisions' ,
    @DateAwareFromDateTime DATETIME = 'Jan  1 2000 12:00AM' ,
    @DateAwareThruDateTime DATETIME = 'Jan  1 2101 12:00AM';
INSERT  INTO [ETL_Test_Source].[ETL].[Master_BAR_INS_XFER_TXN_FACT_NPR_0_ETL_Test_Source_No_valid_subdivisions]
        ( [Appl_Database_Id] ,
          [Automatic_Transfer_Amount] ,
          [Batch_Date_Id] ,
          [Bill_Number] ,
          [Collector_Id] ,
          [Crp_Bot_Comp_Val_Id] ,
          [Facility_Id] ,
          [Insurance_Id] ,
          [Manual_Transfer_Amount] ,
          [Net_Transfer_Amount] ,
          [Patient_Account_Id] ,
          [Transaction_Urn]
        )
        SELECT  XFRTXN.SourceID ,
                XFRTXN.AutoXferAmount ,
                COLL.BatchDateTime ,
                COLL.BillNumber ,
                LEDG.Collector ,  
                FINS.Corporation ,
                VISIT.FacilityID ,
                XFRTXN.InsuranceID ,
                XFRTXN.ManualEdit ,
                XFRTXN.AutoXferAmount ,
                XFRTXN.BillingID ,
                XFRTXN.TransactionID
        FROM    [dbo].[BarTxnInsuranceDetail] AS XFRTXN
                LEFT OUTER JOIN [dbo].[BarVisits] AS VISIT ON [VISIT].[BillingID] = [XFRTXN].[BillingID]
                                                             AND [VISIT].[SourceID] = [XFRTXN].[SourceID]
                LEFT OUTER JOIN [dbo].[BarVisitFinancialData] AS FINS ON [FINS].[BillingID] = [XFRTXN].[BillingID]
                                                             AND [FINS].[SourceID] = [XFRTXN].[SourceID]
                INNER JOIN [dbo].[BarInsuranceLedger] AS LEDG ON [LEDG].[BillingID] = [XFRTXN].[BillingID]
                                                              AND [LEDG].[InsuranceID] = [XFRTXN].[InsuranceID]
                                                              AND [LEDG].[SourceID] = [XFRTXN].[SourceID]
                INNER JOIN [dbo].[BarCollectionTransactions] AS COLL ON [COLL].[BillingID] = [XFRTXN].[BillingID]
                                                              AND [COLL].[SourceID] = [XFRTXN].[SourceID]
                                                              AND [COLL].[TransactionID] = [XFRTXN].[TransactionID]
        WHERE   COLL.Type IN ( 'p', 'A', 'R', 'P', 'q', 'm', 'v', 'b' )
        **AND (ISNUMERIC(COLL.BillingID) = 1
        OR ISNUMERIC(COLL.BillingID) = 0)** 
                AND COLL.BatchDateTime BETWEEN @DateAwareFromDateTime
                AND     @DateAwareThruDateTime

查询成功运行。那个WHERE子句什么都没有过滤掉。当它们在COLL中显式定义为VARCHAR值时,SQL会将这些解释为数值也很奇怪。值得注意的是,此修复程序仅适用于COLL表。如果我们使用XFERTXN.BillingID,则会导致相同的错误。

我相信这个错误可能是一个SQL错误,并且想知道是否有人注意到任何类似的错误。我们插入的表中的所有列都是MONEY类型,其中包含有效的货币值,这不是问题。看来BillingID是这里的问题,因为它包含整数值,但在进程中的每个表上定义为VARCHAR。 SQL不知何故似乎认为它们是货币价值,并且失败了,即使它恰好发生了该列中的每个值都是货币价值。有没有人想知道为什么在第一个代码示例中可能会出现此错误?

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在尝试将XFRTXN.ManualEdit(类型varchar)插入到Manual_Transfer_Amount(类型货币)中,因此您将获得“无法将char值转换为货币”。

至于你的两个查询之间的区别,请仔细检查它们以确保这是唯一的区别:

AND (ISNUMERIC(COLL.BillingID) = 1 OR ISNUMERIC(COLL.BillingID) = 0)