Hello friends!
I am creating a report, Abstract of Bid/Canvass, which will show all the supplier who bids. This also show what item is awarded to a certain supplier through showing the item row number and ranging it like 1-5.
I've already created stored procedure that will show the range(s) of item awarded to a certain supplier but it only show the correct range(s) if all item is awarded to one supplier.
What I problem now is how to get the row number of each item of a certain supplier?
Note: Row number of each item is the to all suppliers.
Any help is much appreciated.
This is my stored procedure used to display the range(s) of the items awarded to a bidder:
Create PROCEDURE [dbo].[spReport_i_BidAwardTo]
-- Add the parameters for the stored procedure here
@PR_No as varchar(50)
AS
BEGIN
create table #updatedata(
SuppName varchar(200),
ranges varchar(1000))
create table #item(
Item_No int,
Item_ID int,
Item_Desc varchar(200),
IsAward bit)
create table #num(num int)
create table #supplier(
supplier_ID int,
suppname varchar(1000))
insert into #supplier
SELECT Distinct i_Bidder_Dtl.Supplier_Id, m_Supplier.SuppName
FROM i_Bidder_Dtl INNER JOIN
m_Supplier ON i_Bidder_Dtl.Supplier_ID = m_Supplier.Supplier_Id INNER JOIN
i_Bidder_hdr ON i_Bidder_Dtl.BidHdr_ID = i_Bidder_hdr.BidHdr_ID
WHERE dbo.i_Bidder_Hdr.PR_No = @PR_No
declare @Item_No as varchar(100)
declare @Supplier_ID as int
declare @isAward as bit
declare @SuppName as varchar(200)
declare @prevNo as int
declare @currNo as int
declare @ranges as varchar(1000)
declare @minRange as varchar(100)
declare @maxRange as varchar(100)
set @prevNo=0
set @currNo=0
set @isAward=0
declare mySupp cursor fast_forward for
select Supplier_ID, suppname from #supplier
open mySupp
fetch next from mySupp into @Supplier_ID,@SuppName
while @@fetch_status=0
Begin
print 'Supplier Name: ' + cast(@Supplier_ID as varchar(50))+ ' ' + cast(@SuppName as varchar(500))
insert into #item
SELECT DISTINCT Row_Number() Over (order by @@rowcount) as item_No, i_Bidder_Dtl.Item_ID,
m_item.Item_Desc, i_Bidder_dtl.is_award
FROM i_Bidder_Dtl INNER JOIN
m_item ON i_Bidder_Dtl.Item_ID = m_item.Item_ID INNER JOIN
i_Bidder_hdr ON i_Bidder_Dtl.BidHdr_ID = i_Bidder_hdr.BidHdr_ID
WHERE i_Bidder_Dtl.Supplier_ID=@Supplier_ID and i_bidder_hdr.PR_No=@PR_No
declare myItem cursor fast_forward for
select Item_No, isaward from #item
open myItem
fetch next from myItem into @Item_No,@isAward
-- print 'Item NO:'+ cast(@item_No as varchar(50))
-- print 'Is award: ' + cast(@isaward as varchar(50))
while @@fetch_status=0
Begin
--if an item is not awarded to a supplier
-- if @isAward = 0
-- -- set the @ranges to null
-- set @ranges = null
-- --else if an item is awarded to a supplier
-- Else
print 'Item NO:'+ cast(@item_No as varchar(50))
print 'Is award: ' + cast(@isaward as varchar(50))
if @isAward = 1
Begin
-- set current number to item number
set @currNo = @Item_No
--if ranges is empty
if @ranges is null
Begin
set @ranges = @currNo--cast(@Item_No as varchar(500))
insert into #num
Select @currNo
End
else
Begin
if @currNo <> (@prevNo + 1)
Begin
if @ranges is not null
Begin
set @ranges = @ranges + ',' + @minRange
--empty table #numRange
delete #num
insert into #num
Select @currNo
set @minRange = (Select min(num) from #num)
set @maxRange = (Select max(num) from #num)
End
End
else
Begin
insert into #num
Select @currNo
set @minRange = (Select min(num) from #num)
set @maxRange = (Select max(num) from #num)
if @minRange=@maxRange
set @ranges = @maxRange
else
set @ranges = @minRange + '-'+ @maxRange
End
End
print 'Current_No:' + cast(@currNo as varchar(50))
print 'Previous_No:' + cast(@prevNo as varchar(50))
print 'range:' + cast(@ranges as varchar(100))
-- insert into #num
-- Select @currNo
End
set @prevNo = @currNo
fetch next from myItem into @Item_No,@isAward
End
close myItem
deallocate myItem
insert into #updatedata
Select @SuppName, @ranges
set @currNo = 0
set @prevNo = @currNo
set @ranges = null
print 'ranges: new supplier'
-- set @ranges = @ranges + ' - ' + @SuppName
fetch next from mySupp into @Supplier_ID,@SuppName
End
close mySupp
deallocate mySupp
Select * from #updateData WHERE ranges IS NOT NULL
END
Result for this stored procedure:
Supplier Name Ranges
fderwe 1-5
Expected Result:
Supplier Name Ranges
fgdfhg 1-3,8
rdgfdg 4-6
eosjg 7