SQLTeam.com | Weblogs | Forums

Create a Stored Procedure to merge 2 tables into one

I am using SSMS 2018. I need to take a production Table and Merge it to a forecast Table and insert into a new table called Forecast using a stored procedure. So if my last production for a well is 10/2021 I want to merge it to my forecast table starting in 11/2021 forward and insert that into a new table. I created a New table called Production_Forecast
My Forecast Table has this
API
Well
Lease
Operator
Basin
P_Date
Oil
Gas

My Forecast Table has

API
Well
Lease
Operator
Basin
Outdate
Gross_Oil
Gas_Gas

I have tried Merge and insert into but it is not working right I want Production first and Forecast only after production stops. Is there an easy way to do this?

without DDL and sample data, I can't give you working code, but I would start with a select statement off the Forecast table that shows the data you want, then you'll have to manipulate the dates to roll them forward to what you want. i.e. 10/2021 and you want to add 1 month to it to get to 11/2021. Then you could merge your new select statement into the new table