获取固定的T-SQL警告:聚合或其他SET操作消除了空值

时间:2018-03-08 18:08:40

标签: sql sql-server

我在SQL Server Management Studio中创建了以下T-SQL代码。在此,我试图从位于远程服务器770Main的每个客户管理站数据库中获取项目详细信息。

所有客户数据库都具有相同的数据库结构/设计。但是大约有60个客户。因此,使用T-SQL,我将整个脚本放入模板字符串中,使用{dbname}占位符并在循环内执行@SQLSCRIPT。当我执行此操作时,我收到警告:

  

通过聚合或其他SET操作消除空值。

但是,当我为一个客户单独执行SQL模板脚本(见下文)时,它运行绝对正常。

SELECT DISTINCT vend.contact
                   ,C.customername
                    ,item.vendoritem
                    ,item.description1
                    ,item.issuecost
                    ,bin.station
                    ,ST.CRIBSTTYPE
                    ,R.RBSTTYPE
                    ,Bin.packQty
                    ,item.orderqty
                    ,weeklysales.lastweeksales
                    ,weeklysales.wtdsales
                    ,st.cribmin 
                    ,st.cribmax
                    ,R.MYMIN
                    ,R.MYMAX
                    ,acbin.binqoh [AutoCrib QOH]
    FROM   [770main].{dbname}.dbo.bin Bin 

       LEFT OUTER JOIN [770main].{dbname}.dbo.item Item 
                    ON bin.item = item.code 
        inner join [770main].{dbname}.dbo.release as rls on rls.itemid=item.code and item.vendor=rls.vendorid
       LEFT OUTER JOIN [770main].{dbname}.dbo.company Company 
                    ON bin.aspprocessed = company.aspprocessed 
        left join (select sum(onhand) binqoh,aspprocessed,item from [770main].{dbname}.dbo.bin where station not like ''ods%''  group by aspprocessed,item ) acbin 
                    on acbin.item = Item.code and acbin.aspprocessed = Company.aspprocessed
       LEFT OUTER JOIN [770main].{dbname}.dbo.vend Vend 
                    ON item.vendor = vend.myno 
       LEFT OUTER JOIN customer C 
                    ON C.customercode collate SQL_Latin1_General_CP1_CI_AS  = vend.contact collate SQL_Latin1_General_CP1_CI_AS

        left join 
    (select productcode
           , ordh.companyid
           ,sum( CASE WHEN  (invoicedate >= dateadd(wk, datediff(wk, 0, getdate())-1, 0) and invoicedate <= DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6)) 
                   THEN (ordhl.NetPrice*ordhl.shippedquantity/ coalesce(ordhl.UnitOfMeasurePriceFactor,1) / coalesce(ordh.CurrencyRate,1))
                 ELSE Null END) lastweeksales
                 ,
        sum( CASE WHEN   (invoicedate >= dateadd(wk, datediff(wk, 0, getdate()), 0) and invoicedate <= getdate())
                 THEN (ordhl.NetPrice*ordhl.shippedquantity/ coalesce(ordhl.UnitOfMeasurePriceFactor,1) / coalesce(ordh.CurrencyRate,1))
                 ELSE Null END) wtdsales
     from orderhistory ordh inner join orderhistoryline ordhl on
     ordh.CompanyID = ordhl.companyid and
     ordh.ordernumber = ordhl.ordernumber
     where 
     (invoicedate >= dateadd(wk, datediff(wk, 0, getdate())-1, 0) and invoicedate <= getdate()) and
     ShippedQuantity <> 0 and ordh.CompanyID = ''002''
     group by productcode, ordh.companyid
     ) weeklysales on
     weeklysales.productcode = item.vendoritem 
    left join [770main].{dbname}.dbo.station s on bin.station = s.myno
    FULL JOIN STNMM St ON Bin.Item=St.ITEM 
    FULL JOIN ROBOMM R ON Bin.Item=R.ITEM
    WHERE 
    C.COMPANYID = ''002''
    GROUP BY vend.contact
                   ,C.customername
                    ,item.vendoritem
                    ,bin.station
                    ,ST.CRIBSTTYPE
                    ,R.RBSTTYPE
                    ,Bin.packQty
                    ,item.orderqty
                    ,weeklysales.lastweeksales
                    ,weeklysales.wtdsales
                    ,st.cribmin 
                    ,st.cribmax
                    ,R.MYMIN
                    ,R.MYMAX
                    ,item.description1
                    ,acbin.binqoh 
                    ,item.issuecost
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



IF (object_id('TMPProductbyCustomer')) IS NOT NULL
BEGIN
  DROP TABLE TMPProductbyCustomer
print('dropped')


END



DECLARE @sql_script VARCHAR(max)

DECLARE @template VARCHAR(max)
DECLARE @Dbname varchar(100)
DECLARE @table table(id int identity(1,1),dbname varchar(100))

insert into @table

SELECT 'Abipa'
union select 'ascoac'
union SELECT 'BourgaultE' 
UNION SELECT 'Avcorp'
UNION SELECT 'Blackcat'

DECLARE @COUNTER INT = 1
DECLARE @END int = (select count(*) from @table)

CREATE table TMPProductbyCustomer ( 
CustomerID varchar(6)
,CompanyName       varchar(100) null
,ItemVendorItem    varchar(100)
,Decription1 varchar(35) null
,IssueCost money 
,Station varchar(4)
,CribStationType int null
,RoboStationType int null
,PackQty int
,ORDERQTY int 
,LastWeekSales money null
,WTDSales money null
,CRIBMIN int null
,CRIBMAX int null
,MYMIN int null
,MYMAX int null
,OnHand int 
     )
WHILE @COUNTER <= @END

BEGIN

set @DBNAME = (select dbname from @table where ID = @COUNTER )
set @template ='
WITH STNMM (item, station, cribsttype,name,packqty,onhand,burnqty,cribmin,cribmax)

AS
(
    select
    bin.item
    ,bin.station
    ,station.[type] AS CRIBSTTYPE
    ,station.name
    ,bin.packqty
    ,bin.onhand
    ,bin.burnqty
    ,bin.minimum as cribmin
    ,bin.maximum as cribmax
    from [770main].{dbname}.dbo.bin as bin
    left join [770main].{dbname}.dbo.station as station on bin.station=station.myno
    where station.[type] <> 6
),
ROBOMM (item, station, RBSTTYPE,name,pkgqty,mymin,mymax)
AS
(
    SELECT 
    MM.ITEM
    ,MM.STATION
    ,station.[type] as RBSTTYPE
    ,STATION.NAME
    ,MM.PKGQTY
    ,MM.MYMIN
    ,MM.MYMAX
    FROM [770MAIN].{dbname}.DBO.ROBOMINMAX AS MM
    LEFT JOIN [770MAIN].{dbname}.DBO.STATION AS STATION ON MM.STATION=STATION.MYNO
    WHERE station.[type] = 6 )

INSERT INTO TMPProductbyCustomer SELECT DISTINCT vend.contact
               ,C.customername
               ,item.vendoritem
                ,item.description1
                ,item.description2
                ,item.issuecost
                ,bin.station
                ,ST.CRIBSTTYPE
                ,R.RBSTTYPE
                ,Bin.packQty
                ,item.orderqty
                ,weeklysales.lastweeksales
                ,weeklysales.wtdsales
                ,st.cribmin 
                ,st.cribmax
                ,R.MYMIN
                ,R.MYMAX
                ,acbin.binqoh [AutoCrib QOH]

FROM   [770main].{dbname}.dbo.bin Bin 

   LEFT OUTER JOIN [770main].{dbname}.dbo.item Item 
                ON bin.item = item.code 
    inner join [770main].{dbname}.dbo.release as rls on rls.itemid=item.code and item.vendor=rls.vendorid
   LEFT OUTER JOIN [770main].{dbname}.dbo.company Company 
                ON bin.aspprocessed = company.aspprocessed 
    left join (select sum(onhand) binqoh,aspprocessed,item from [770main].{dbname}.dbo.bin where station not like ''ods%''  group by aspprocessed,item ) acbin 
                on acbin.item = Item.code and acbin.aspprocessed = Company.aspprocessed
   LEFT OUTER JOIN [770main].{dbname}.dbo.vend Vend 
                ON item.vendor = vend.myno 
   LEFT OUTER JOIN customer C 
                ON C.customercode collate SQL_Latin1_General_CP1_CI_AS  = vend.contact collate SQL_Latin1_General_CP1_CI_AS
LEFT JOIN 
(select productcode
  , ordh.companyid
,       sum( CASE WHEN  (invoicedate >= dateadd(wk, datediff(wk, 0, getdate())-1, 0) and invoicedate <= DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6)) 
               THEN (ordhl.NetPrice*ordhl.shippedquantity/ coalesce(ordhl.UnitOfMeasurePriceFactor,1) / coalesce(ordh.CurrencyRate,1))
             ELSE 0 END) lastweeksales
             ,
    sum( CASE WHEN   (invoicedate >= dateadd(wk, datediff(wk, 0, getdate()), 0) and invoicedate <= getdate())
             THEN (ordhl.NetPrice*ordhl.shippedquantity/ coalesce(ordhl.UnitOfMeasurePriceFactor,1) / coalesce(ordh.CurrencyRate,1))
             ELSE 0 END) wtdsales
 from orderhistory ordh inner join orderhistoryline ordhl on
 ordh.CompanyID = ordhl.companyid and
 ordh.ordernumber = ordhl.ordernumber
 where 
 (invoicedate >= dateadd(wk, datediff(wk, 0, getdate())-1, 0) and invoicedate <= getdate()) and
 ShippedQuantity <> 0 and ordh.CompanyID = ''002''
 group by productcode, ordh.companyid
 ) weeklysales on
 weeklysales.productcode = item.vendoritem 
left join [770main].{dbname}.dbo.station s on bin.station = s.myno
FULL JOIN STNMM St ON Bin.Item=St.ITEM 
FULL JOIN ROBOMM R ON Bin.Item=R.ITEM
WHERE 
C.COMPANYID = ''002''
GROUP BY vend.contact
               ,C.customername
                ,item.vendoritem
                ,bin.station
                ,ST.CRIBSTTYPE
                ,R.RBSTTYPE
                ,Bin.packQty
                ,item.orderqty
                ,weeklysales.lastweeksales
                ,weeklysales.wtdsales
                ,st.cribmin 
                ,st.cribmax
                ,R.MYMIN
                ,R.MYMAX
                ,item.description1
                ,acbin.binqoh 
                ,item.issuecost
'
SET @SQL_SCRIPT = REPLACE(@template, '{dbname}', @DBNAME)

EXECUTE (@sql_script)

set @COUNTER = @COUNTER +1

end

select * from TMPProductbyCustomer 

2 个答案:

答案 0 :(得分:1)

这是很多代码。

我注意到这一行:

sum( 
    CASE 
    WHEN  (invoicedate >= dateadd(wk, datediff(wk, 0, getdate())-1, 0) 
    and invoicedate <= DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6)) 
    THEN (ordhl.NetPrice*ordhl.shippedquantity/ 
    coalesce(ordhl.UnitOfMeasurePriceFactor,1)/ 
    coalesce(ordh.CurrencyRate,1))
    ELSE Null 
 END) lastweeksales

我建议您将ELSE Null更改为ELSE 0,只要它不会引入除零错误

答案 1 :(得分:0)

在运行引发错误的代码之前,您可以为当前的调用上下文设置此选项。

SET ANSI_WARNINGS OFF;

您还需要确保null消除对您的案例实际上是正常的。这可能是一个有效的错误,显示您的查询中存在逻辑问题。