| Author |
Topic |
|
user45
Starting Member
10 Posts |
Posted - 2011-10-03 : 07:20:47
|
| Hi All,I have created a stored procedure which creates a temporary table and inserts data. Within this stored procedure I have declared a @daterange parameter with values = 1,2,3,4.I want to run a job where all values for the daterange parameter are updated onto the table.I have tried:exec MCMStockAgeingSP @DateRange = 1exec MCMStockAgeingSP @DateRange = 2exec MCMStockAgeingSP @DateRange = 3exec MCMStockAgeingSP @DateRange = 4but this executes with daterange = 1 and then gets overridden with the values when daterange = 2 and so on....so the values i'm left with in the table are the ones with daterange = 4. But I want ALL values in the table.Can this be done? |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2011-10-03 : 07:27:12
|
| Can you post your SP and expected output?Senthil.C------------------------------------------------------MCTS - [Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
user45
Starting Member
10 Posts |
Posted - 2011-10-03 : 07:48:12
|
| CREATE Procedure [dbo].[MCMStockAgeingSP]@DateRange intASTruncate Table [MCMStocksAging]Set NoCount OnBEGINDeclare @Branch varchar (50)Declare @ProductCode varchar (50)Declare @Description varchar (255) Declare @DocYear varchar(10)Declare @Month varchar(10)Declare @NumberOfDays varchar(10)Declare @DateReceived smalldatetimeDECLARE @today smalldatetimeDECLARE @fromdate smalldatetimeDECLARE @todate smalldatetimeDECLARE @PeriodDesc varchar(10)SET @today = GETDATE()SET @fromdate = DATEADD(D,-91,GETDATE()) SET @todate = DATEADD(D,1,GETDATE()) SET @PeriodDesc='0-3'IF @DateRange = 2 BEGIN SET @fromdate = DATEADD(D,-365,GETDATE()) SET @todate = DATEADD(D,-91,GETDATE()) SET @PeriodDesc='4-12' ENDIF @DateRange = 3 BEGIN SET @fromdate = DATEADD(D,-729,GETDATE()) SET @todate = DATEADD(D,-365,GETDATE()) SET @PeriodDesc='13-24' ENDIF @DateRange = 4 BEGIN SET @fromdate = DATEADD(D,-3650,GETDATE()) SET @todate = DATEADD(D,-729,GETDATE()) SET @PeriodDesc='24+' ENDDECLARE curProduct CURSORFORSelect Branch.Name as Branch,Product.ProductCode as ProductCode,Product.description as ProductDescription, (YEAR(datereceived)) AS DocYear, (MONTH(datereceived)) AS DocMonth,DATEDIFF(day, (DateReceived), @today) AS NumberOfDays,@PeriodDesc AS Period,StockReceiptCost.DateReceived, from Product WITH (NOLOCK) left join StockReceiptCost on Product.ProductID=StockReceiptCost.ProductIDleft Join Branch with(nolock) on StockReceiptCost.BranchID=Branch.BranchID whereand StockReceiptCost.DateReceived >= @fromdate AND StockReceiptCost.DateReceived <@todateFOR READ ONLYOPEN curProductFETCH NEXT FROM curProduct INTO @Branch,@ProductCode,@Description, @DocYear,@Month,@NumberOfDays,@PeriodDesc,@DateReceived,WHILE (@@FETCH_STATUS = 0) BEGIN Insert Into [MCMStocksAging] (Branch,ProductCode,Description, DocYear,Month,NumberOfDays,PeriodDesc,DateReceived) Values (@Branch,@ProductCode,@Description, @DocYear,@Month,@NumberOfDays,@PeriodDesc,@DateReceived)FETCH NEXT FROM curProduct INTO @Branch,@ProductCode,@Description, @DocYear,@Month,@NumberOfDays,@PeriodDesc,@DateReceivedENDCLOSE curProductDEALLOCATE curProductSet NoCount OFF ENDI want to fill the table with values when daterange = 1, daterange = 2 etc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-03 : 07:53:11
|
| so you want to replicate all daterange values in your table? ie. each record inserted once for ech daterange?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
user45
Starting Member
10 Posts |
Posted - 2011-10-03 : 07:57:15
|
| yes I want to insert all records for each daterange |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2011-10-03 : 08:00:06
|
| You have truncate statement in your SP, Obviously it will empty the table when ever u execute the SP...Senthil.C------------------------------------------------------MCTS - [Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-03 : 08:22:37
|
you can do it in one shotCREATE Procedure [dbo].[MCMStockAgeingSP]@DateRange varchar(100)ASTruncate Table [MCMStocksAging]Set NoCount OnBEGINDECLARE @Dates table(StartDate datetime,ENdDate datetime,PeriodDesc varchar(10))INSERT @DatesSELECT DATEADD(D,-91,GETDATE()), DATEADD(D,1,GETDATE()) ,'0-3'WHERE ',' + @DateRange + ',' LIKE '%,1,%'UNION ALLSELECT DATEADD(D,-365,GETDATE()), DATEADD(D,-91,GETDATE()) ,'4-12'WHERE ',' + @DateRange + ',' LIKE '%,2,%'UNION ALLSELECT DATEADD(D,-729,GETDATE()), DATEADD(D,-365,GETDATE()) ,'13-24'WHERE ',' + @DateRange + ',' LIKE '%,3,%'UNION ALLSELECT DATEADD(D,-3650,GETDATE()), DATEADD(D,-729,GETDATE()) ,'24+'WHERE ',' + @DateRange + ',' LIKE '%,4,%'Select Branch.Name as Branch,Product.ProductCode as ProductCode,Product.description as ProductDescription, (YEAR(datereceived)) AS DocYear, (MONTH(datereceived)) AS DocMonth,DATEDIFF(day, (DateReceived), @today) AS NumberOfDays,d.PeriodDesc AS Period,StockReceiptCost.DateReceived,from Product WITH (NOLOCK) left join StockReceiptCost on Product.ProductID=StockReceiptCost.ProductIDleft Join Branch with(nolock) on StockReceiptCost.BranchID=Branch.BranchID left join @Dates don StockReceiptCost.DateReceived >= d.startdate AND StockReceiptCost.DateReceived < d.endtodate in your original post its included in where but you're doing a left join hence effectively it will reduee to inner join. if you want to filter it based on dates then make it inner join. alternatively if you want to use date filter only for getting matches use the above suggestion itself.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|