I have 2 tables tbl_salesData and tbl_purchasedata. I want to get the recent purchase price from table tbl_purchasedata into tbl_salesData based on the col_salesdate column. The purchase price must be before the sales date.
I need purchase price based on sales date as recent purchase date.
ex. for item Mouse the sales date is 24-07-2020 and the recent purchase price will be just before the sales date i.e, 22-07-2020 so the purchase price will be 250. The output table is like this below.
I tried but this didn't work as I am new to stored procedures and other things.
Declare @total_Count int
Declare @row_num int
set @total_Count = (select Count(*) from vw_Final_Data)
set @row_num = 1
while @total_Count < @row_num
begin
/****Some code here*****/
end
I want this to be created as stored procedure and run using scheduler.
Please help!



WHILE, in SQL, should (almost) always be your last choice. SQL is a set based language, and thus is performs awfully at iterative operations (like aWHILE). If you want to use a loop in SQL, you almost certainly are making the wrong design choice. (There are rare exceptions to this rule, such as when doing bulk operations, this doesn't look to be one of them.)text(please don't use the new table markdown, it's not great for copying) is also far more helpful than an image of text.