过程错误:在需要条件的上下文中在“ AND”附近指定非布尔类型的表达式

时间:2019-02-20 20:04:57

标签: sql-server tsql

我收到此错误:

  

在需要条件的上下文中在“ AND”附近指定非布尔类型的表达式

执行此过程时:p_settle_undelivered_qty_typeq

过程是:

CREATE PROCEDURE p_settle_undelivered_qty_typeq
   (@al_sn INTEGER,
    @as_sql NVARCHAR(MAX))
AS
BEGIN
    DECLARE @SQLString NVARCHAR(MAX)

    IF OBJECT_ID('tempdb..#tmp') IS NOT NULL 
        DROP TABLE #tmp

    CREATE TABLE #tmp
    (
        cn INT NOT NULL,
        rn INT NOT NULL,
        po INT NOT NULL,
        cpr_qty FLOAT NOT NULL,
        new_cpr_qty FLOAT NOT NULL,
        schedule_qty FLOAT NULL,
        cr_qty FLOAT NULL,
        row_nbr INTEGER NULL,
        nbr_of_rows INTEGER NULL,
        cpr_qty_total FLOAT NULL,
        cumul_cpr_qty FLOAT NULL,
        change_row_nbr INTEGER NULL
    )

    SET @SQLString = 'INSERT INTO #tmp (cn, rn, po, cpr_qty, new_cpr_qty,
                                        schedule_qty, cr_qty, row_nbr,
                                        nbr_of_rows, cpr_qty_total, cumul_cpr_qty, change_row_nbr)
                          SELECT 
                              s1_contract.contract_nbr AS cn,
                              s1_contract_pricing_release.release_nbr AS rn,
                              s1_contract_pricing_release.pricing_order po,
                              round(s1_contract_pricing_release.quantity, s1_uom.nbr_of_decimals) - round(coalesce(s1_cpr_wash.quantity, 0), s1_uom.nbr_of_decimals) AS cpr_qty,
                              round(s1_contract_pricing_release.quantity, s1_uom.nbr_of_decimals) - round(coalesce(s1_cpr_wash.quantity, 0), s1_uom.nbr_of_decimals) AS new_cpr_qty,
                              round(s1_contract_pricing_release.quantity, s1_uom.nbr_of_decimals) AS schedule_qty,
                              case when s1_contract.new_load_based_flag = ''Y''
               then s1_release.estimated_applied_loads * s1_contract.vehicle_capacity
               else s1_release.estimated_applied_quantity
           end - s1_release.wash_quantity      AS cr_qty,
           ROW_NUMBER() OVER (PARTITION BY s1_contract.contract_nbr,
                                           s1_release.release_nbr
                                  ORDER BY s1_contract.contract_nbr,
                                           s1_release.release_nbr,
                                  CASE WHEN s1_contract_pricing.price is not NULL THEN 1
                                       WHEN s1_contract_pricing.price_future IS NOT NULL THEN 2
                                       WHEN s1_contract_pricing.price_basis IS NOT NULL THEN 3
                                       ELSE 4
                                  END,
                                  s1_contract_pricing_release.pricing_order
                             )                 AS row_nbr,
           COUNT(*) OVER (PARTITION BY s1_contract.contract_nbr,
                                       s1_release.release_nbr
                         )                     AS nbr_of_rows,
           SUM(ROUND(s1_contract_pricing_release.quantity, s1_uom.nbr_of_decimals) - round(coalesce(s1_cpr_wash.quantity, 0), s1_uom.nbr_of_decimals))
               OVER (PARTITION BY s1_contract.contract_nbr,
                                  s1_release.release_nbr
                    ) cpr_qty_total,
           CAST(0 AS decimal(25,6))            AS cumul_cpr_qty,
           10000                               AS change_row_nbr
      FROM s1_contract
           JOIN s1_release
             ON s1_contract.contract_nbr = s1_release.contract_nbr
           JOIN s1_contract_pricing_release
             ON s1_contract.contract_nbr = s1_contract_pricing_release.contract_nbr
            AND s1_release.release_nbr = s1_contract_pricing_release.release_nbr
           JOIN s1_contract_pricing
             ON s1_contract_pricing_release.contract_nbr = s1_contract_pricing.contract_nbr
            AND s1_contract_pricing_release.pricing_order = s1_contract_pricing.pricing_order
            AND s1_contract_pricing.pricing_average_type = ''C''
           JOIN s1_uom
             ON s1_contract.quantity_uom = s1_uom.uom_code
           JOIN a1_user_location
             ON s1_contract.plc_id = a1_user_location.plc_id
           LEFT OUTER JOIN s1_cpr_wash
             ON s1_contract_pricing_release.contract_nbr = s1_cpr_wash.contract_nbr
            AND s1_contract_pricing_release.release_nbr = s1_cpr_wash.release_nbr
            AND s1_contract_pricing_release.pricing_order = s1_cpr_wash.pricing_order
     WHERE s1_contract.contract_id > 0 ' + @as_sql +
    'ORDER BY s1_contract.contract_nbr,
              s1_contract_pricing_release.release_nbr,
              CASE WHEN s1_contract_pricing.price is not NULL THEN 1
                   WHEN s1_contract_pricing.price_future is not NULL THEN 2
                   WHEN s1_contract_pricing.price_basis is not NULL THEN 3
                   ELSE 4
              END,
              s1_contract_pricing_release.pricing_order ';

   EXECUTE sp_executesql @SQLString;

/*===============================================*/
/* Process finished with SUCCESS                 */
/*===============================================*/

   SELECT 1;

END;
go

我使用以下SQL测试该过程:

DECLARE @return_value int

EXEC    @return_value = [dbo].[p_settle_undelivered_qty_typeq]
        @al_sn = 8,
        @as_sql = ' AND (s1_contract.contract_type = ''P/C'')  AND (a1_user_location.user_id = ''DAVEG'')  AND (s1_contract.plc_id = ''1981'')  AND (s1_contract.commodity_id = ''1'')  AND (s1_contract.contract_status <> ''X'' AND s1_contract.contract_status <> ''C'')'

SELECT  'Return Value' = @return_value

GO 

我正在使用动态SQL来满足变量“ as_sql”中的其他选择条件。

如果该变量中没有任何值,则该过程将按预期工作。

当我在“ as_sql”变量中有一个条目时,它失败并显示以上错误消息。

0 个答案:

没有答案