正常的傳遞 @SendStationID='''1'',''2''' 是無效,改用 @SendStationID='1,2,3,003,002' 調用以下的存儲過程可以實現in 查詢效果
USE [ztwl]
GO
/****** Object: StoredProcedure [dbo].[SelectPage_StationTransferFee_Second] Script Date: 04/11/2019 14:17:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SelectPage_StationTransferFee_Second]
@begincount varchar(50), --開始條數
@PageSize varchar(50) , --每頁多少條記錄
@Where nvarchar(4000) =N'' , --條件語句
@StartStationID varchar(50), --始發分撥網點
@SendStationID varchar(4000), --發站網點
@InStationID varchar(50) --入庫網點
as
BEGIN
--------begin--組合發站參數臨時表-----begin-----
DECLARE @Temp_Array varchar(max) set @Temp_Array=@SendStationID declare @Temp_Variable varchar(max) create table #Temp_SendTable(Item varchar(max)) ---定義發站ID臨時表 while(LEN(@Temp_Array) > 0) begin if(CHARINDEX(',',@Temp_Array) = 0) begin set @Temp_Variable = @Temp_Array set @Temp_Array = '' end else begin set @Temp_Variable = LEFT(@Temp_Array,CHARINDEX(',',@Temp_Array)-1) set @Temp_Array = RIGHT(@Temp_Array,LEN(@Temp_Array)-LEN(@Temp_Variable)-1) end insert into #Temp_SendTable(Item) values(@Temp_Variable) end
------------組合發站參數------end--------------------
--- 第一部分:區域網點中轉費
SELECT * INTO #Bas_FN_Area_StationConfig_Temp FROM (SELECT * from Bas_FN_Area_StationConfig_Daily WHERE 1=2 ) as x
--1.1用始發分撥+發站網點+入庫網點查詢區域網點中轉費報表;
IF LEN(@StartStationID)>0
BEGIN
Insert into #Bas_FN_Area_StationConfig_Temp
SELECT * from Bas_FN_Area_StationConfig_Daily WHERE UpBelongCenterID= @StartStationID
and exists(select 1 from #Temp_SendTable(nolock) where #Temp_SendTable.Item=StartStationID) and InStationID=@InStationID and IfDel=0
-- PRINT '有--始發分撥查詢'
end
else
BEGIN
Insert into #Bas_FN_Area_StationConfig_Temp
SELECT * from Bas_FN_Area_StationConfig_Daily WHERE exists(select 1 from #Temp_SendTable(nolock) where #Temp_SendTable.Item=StartStationID) and InStationID=@InStationID and IfDel=0
-- PRINT '無--始發分撥查詢'
end
--1.2創建已有的+產品列表臨時表
SELECT * INTO #cityList_Temp FROM (SELECT a.FinalPlaceCityID , a.FinalPlaceCity,a.ProductID from #Bas_FN_Area_StationConfig_Temp a ) AS y
-- SELECT * from #Bas_FN_Area_StationConfig_Temp
--- 第二部分:網點中轉費
--1.2發站網點+入庫網點詢+排除網點中轉費重復的城市和產品
SELECT * INTO #Bas_FN_StationConfig_Temp FROM (
SELECT * from Bas_FN_StationConfig a WHERE exists(select 1 from #Temp_SendTable(nolock) where #Temp_SendTable.Item=a.UpStationID) and a.StartStationID=@InStationID and a.IfDel=0
and NOT EXISTS (SELECT b.FinalPlaceCityID from #cityList_Temp b WHERE a.FinalPlaceCityID=b.FinalPlaceCityID and a.ProductID=b.productID )
) as xx
-- select count(*) from #cityList_Temp
--1.3 合並區域和網點費用 UpBelongCenterID,UpBelongCenter,QuoteBelongCenterID,QuoteBelongCenter,
SELECT * INTO #Bas_FN_StationConfig_Finally_Temp FROM
(
SELECT AreaCode,AreaName,StartStation as UpStation ,StartStationID as UpStationID ,InStation as StartStation ,InStationID as StartStationID,FinalPlaceCity,FinalPlaceCityID,FinalPlaceProvince,FinalPlaceProvinceID ,Product,ProductID,
InOutType,Limit,StartWeightOne,EndWeightOne,WeightPriceOne,VolumnPriceOne,StartWeightTwo,EndWeightTwo,WeightPriceTwo,VolumnPriceTwo,StartWeightThree,EndWeightThree,WeightPriceThree,VolumnPriceThree,StartWeightFour,EndWeightFour,WeightPriceFour,VolumnPriceFour,StartWeightFive,EndWeightFive,WeightPriceFive,VolumnPriceFive,StartWeightSix,EndWeightSix,WeightPriceSix,VolumnPriceSix,
UpBelongCenterID, UpBelongCenter, QuoteBelongCenterID, QuoteBelongCenter
from #Bas_FN_Area_StationConfig_Temp
UNION ALL
SELECT '' AS AreaCode,'' as AreaName, UpStation,UpStationID,StartStation,StartStationID,FinalPlaceCity,FinalPlaceCityID,FinalPlaceProvince,FinalPlaceProvinceID ,Product,ProductID,
CASE Status WHEN 0 THEN '出港網點中轉費' ELSE '進港網點中轉費' END as InOutType,Limit,StartWeightOne,EndWeightOne,WeightPriceOne,VolumnPriceOne,StartWeightTwo,EndWeightTwo,WeightPriceTwo,VolumnPriceTwo,StartWeightThree,EndWeightThree,WeightPriceThree,VolumnPriceThree,StartWeightFour,EndWeightFour,WeightPriceFour,VolumnPriceFour,StartWeightFive,EndWeightFive,WeightPriceFive,VolumnPriceFive,StartWeightSix,EndWeightSix,WeightPriceSix,VolumnPriceSix,b.BelongCenterID as UpBelongCenterID,b.BelongCenter as UpBelongCenter,c.BelongCenterID as QuoteBelongCenterID,c.BelongCenter as QuoteBelongCenter
FROM
(SELECT a.*,
(SELECT top 1 aa.EndStationID from Bas_FinalPlace aa WHERE aa.ifDelID=0 AND aa.QuoteCityID=a.FinalPlaceCityID) as EndStationID
from #Bas_FN_StationConfig_Temp a ) as aaa
LEFT JOIN Bas_StationInformation b on aaa.UpStationID=b.StationID and b.IfDel=0
LEFT JOIN Bas_StationInformation c on aaa.EndStationID=c.StationID and c.IfDel=0
) AS xyz
--分頁
DECLARE @sql nvarchar(4000)
set @sql=' select * from ( SELECT top '+@PageSize+' ROW_NUMBER() Over(order by AreaCode desc ) as rownumid, a.* from #Bas_FN_StationConfig_Finally_Temp a
'+@where+' ) as pagetable where rownumid>'+@begincount
PRINT @sql
EXEC(@sql)
--總數
set @sql=' SELECT COUNT(*) from #Bas_FN_StationConfig_Finally_Temp '+@where+''
EXEC(@sql)
DROP TABLE #Bas_FN_Area_StationConfig_Temp
DROP TABLE #cityList_Temp
DROP TABLE #Bas_FN_StationConfig_Temp
DROP TABLE #Bas_FN_StationConfig_Finally_Temp
DROP TABLE #Temp_SendTable
END
