为什么来自 SQL 服务器存储过程的特定比较 return 记录,但包含相同值 return 的 IN 子句什么都没有?
Why would a specific comparison return records from a SQL Server Stored Proc but an IN clause containing the same value return nothing?
我有两个相同的存储过程,除了两点:
(a) 一个有效(returns 条记录),另一个无效(returns 无记录)
(b) 工作人员搜索单个匹配项,而非工作人员则撒下更广泛的网络,包括所寻求的额外价值
两个存储过程之间的唯一区别(除了 SP 名称)是:
1)
工作存储过程包含以下内容的地方:
where UPPER(up.Unit) = 'ROCK BOTTOM'
...非工作存储过程包含以下内容:
where UPPER(up.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
2)
工作存储过程包含以下内容:
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
...非工作存储过程包含以下内容:
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
3)
最后,工作存储过程包含以下内容:
and Unit = ''ROCK BOTTOM'' and
...非工作存储过程包含以下内容:
and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
为什么搜索 'Rock Bottom' 和其他值会失败,而单独搜索该值会成功?
更新
应评论者 Kritner 的要求,这里是完整的存储过程;第一个是有效的:
IF OBJECT_ID ( 'sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom', 'P' ) IS NOT NULL
DROP PROCEDURE sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom;
GO
CREATE Procedure [dbo].[sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where UPPER(up.Unit) = 'ROCK BOTTOM'
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >=
@CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit = ''ROCK BOTTOM'' and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
...这是 returns 什么都没有的:
IF OBJECT_ID ( 'sp_ViewPriceMatrix_Variance_RockBottomExtended', 'P' ) IS NOT NULL
DROP PROCEDURE sp_ViewPriceMatrix_Variance_RockBottomExtended;
GO
CREATE Procedure [dbo].[sp_ViewPriceMatrix_Variance_RockBottomExtended]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where UPPER(up.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
这两个查询还有一个区别...
第一次查询:
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >=
第二次查询:
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO',
您引用的是另一个 table。这有什么区别吗?请注意,在您引用 up
的第一个(工作)查询中,第二个是 mp
.
我有两个相同的存储过程,除了两点:
(a) 一个有效(returns 条记录),另一个无效(returns 无记录)
(b) 工作人员搜索单个匹配项,而非工作人员则撒下更广泛的网络,包括所寻求的额外价值
两个存储过程之间的唯一区别(除了 SP 名称)是:
1) 工作存储过程包含以下内容的地方:
where UPPER(up.Unit) = 'ROCK BOTTOM'
...非工作存储过程包含以下内容:
where UPPER(up.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
2)
工作存储过程包含以下内容:
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
...非工作存储过程包含以下内容:
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
3)
最后,工作存储过程包含以下内容:
and Unit = ''ROCK BOTTOM'' and
...非工作存储过程包含以下内容:
and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
为什么搜索 'Rock Bottom' 和其他值会失败,而单独搜索该值会成功?
更新
应评论者 Kritner 的要求,这里是完整的存储过程;第一个是有效的:
IF OBJECT_ID ( 'sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom', 'P' ) IS NOT NULL
DROP PROCEDURE sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom;
GO
CREATE Procedure [dbo].[sp_ViewPriceMatrix_Variance_RockBottomRollup_RockBottom]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where UPPER(up.Unit) = 'ROCK BOTTOM'
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >=
@CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit = ''ROCK BOTTOM'' and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
...这是 returns 什么都没有的:
IF OBJECT_ID ( 'sp_ViewPriceMatrix_Variance_RockBottomExtended', 'P' ) IS NOT NULL
DROP PROCEDURE sp_ViewPriceMatrix_Variance_RockBottomExtended;
GO
CREATE Procedure [dbo].[sp_ViewPriceMatrix_Variance_RockBottomExtended]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where UPPER(up.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
这两个查询还有一个区别...
第一次查询:
where UPPER(up.Unit) = 'ROCK BOTTOM' and C.BeginDate <= @CalendarEndDate and C.EndDate >=
第二次查询:
where UPPER(mp.Unit) IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO',
您引用的是另一个 table。这有什么区别吗?请注意,在您引用 up
的第一个(工作)查询中,第二个是 mp
.