ALTER PROCEDURE [dbo].[SP_DateAndBarcodeWiseSalesDetailsSuppWise]
@supplier nvarchar(100),
@group nvarchar(100),
@product nvarchar(100),
@brand nvarchar(100),
@startDT nvarchar(20),
@endDate nvarchar(20),
@sMonth int,
@sYear int,
@eMonth int,
@eYear int
AS
BEGIN
SET NOCOUNT ON;
declare @m int,
@y int,
@cYear int,
@month int,
@tblname nvarchar(100),
@tblSaleReturn nvarchar(100),
@stMonth int,
@SQL nvarchar(2000),
@retSQL nvarchar(2000)
create table #tmp
(
a int
)
CREATE TABLE #Sale(
[SupID] [nvarchar](4) NULL,
[Qty] [money] NULL,
[sBarCode] [nvarchar](15) NULL,
[BarCode] [nvarchar](18) NULL,
[SQty] [money] NULL,
[Invoice] [nvarchar](30) NULL
)
CREATE TABLE #SaleReturn(
[Invoice] [nvarchar](30) NULL,
[cInvoice] [nvarchar](30) NULL,
[SupID] [nvarchar](4) NULL,
[Qty] [money] NULL,
[sBarCode] [nvarchar](15) NULL,
[BarCode] [nvarchar](18) NULL,
[SQty] [money] NULL,
[rQty] [money] NULL,
[Returned] [nvarchar](1) NULL
)
SET @m = @sMonth
SET @y = @sYear
set @stMonth=@sMonth
while(@sYear<=@eYear)
begin
-- print convert(nvarchar(50),@sYear,101)
if(@stMonth=13)
begin
set @stMonth=1
end
while(@stMonth<=12)
begin
Set @m = 100+@stMonth
set @tblname='Sale'+convert(nvarchar(50),@sYear,101)+SUBSTRING(Cast(@m as nvarchar(4)),2,3)
set @tblSaleReturn = 'SaleReturn' + convert(nvarchar(50),@sYear,101)+SUBSTRING(Cast(@m as nvarchar(4)),2,3)
--print @tblname
--print @tblSaleReturn
declare
@sY nvarchar(50),
@sM nvarchar(50),
@sDT nvarchar(50),
@eDT nvarchar(50),
@foundTbl int,
@returnFound int
set @sY=cast(@sYear as nvarchar(50))
set @sM=cast(@stMonth as nvarchar(50) )
set @sDT=convert(nvarchar(50),@startDT,101)
set @eDT=convert(nvarchar(50),@endDate,101)
set @SQL='insert into #tmp SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=''BASE TABLE'' AND TABLE_NAME= '''+@tblname+'''and TABLE_SCHEMA=''dbo'''
--print @SQL
exec(@SQL)
set @foundTbl = (select a from #tmp)
delete from #tmp
if(@foundTbl = 1)
begin
print ':)'
print @tblname
print @tblSaleReturn
set @SQL='INSERT INTO #Sale
(
[SupID],
[Qty],
[sBarCode],
[BarCode],
[SQty],
[Invoice]
)
SELECT
s2.[SupID],
s2.[Qty],
s2.[sBarCode],
s2.[BarCode],
s2.[SQty],
s2.[Invoice]
FROM '+@tblname+' s2
inner join StyleSize ss
on ss.Barcode = s2.BarCode
WHERE (SaleDt>='''+@sDT+''' and SaleDt<='''+@eDT+''') and ((s2.SupID='''+@supplier+''' and '''+@supplier+'''<>''All'')or('''+@supplier+'''=''All''))'
--print @SQL
set @retSQL = 'INSERT INTO #SaleReturn
(
[Invoice],
[cInvoice],
[SupID],
[Qty],
[sBarCode],
[BarCode],
[SQty],
[rQty],
[Returned]
)
SELECT
s2.[Invoice],
s2.[cInvoice],
s2.[SupID],
s2.[Qty],
s2.[sBarCode],
s2.[BarCode],
s2.[SQty],
s2.[rQty],
s2.[Returned]
FROM '+@tblSaleReturn+' s2
inner join StyleSize ss
on ss.Barcode = s2.BarCode
WHERE (ReturnedDt>='''+@sDT+''' and ReturnedDt<='''+@eDT+''') and ((s2.SupID='''+@supplier+''' and '''+@supplier+'''<>''All'')or('''+@supplier+'''=''All''))'
--print @retSQL
end
else
begin
set @SQL=''
set @retSQL=''
print ':('
end
print @SQL
print @retSQL
exec(@sql)
exec(@retSQL)
if(@stMonth=@eMonth and @sYear=@eYear)
begin
set @stMonth=13
end
else
begin
set @stMonth=@stMonth+1
end
end
set @sYear=@sYear+1
end
--select SUM(sQty) from #Sale
--select SUM(rQty) from #SaleReturn