I need to learn how to build a stored procedure in SQL Server 2014 that will insert data into a table - but only if a condition is met.
I will use a basic example:
I have 2 tables, customers and orders with a simple one-to-many relationship on the CustomerID.
Customers table contains:
CustomerID, CustomerName, TermStartDate, TermEndDate
Orders table contains:
contains OrderID, CustomerID, ProductID, OrderDate
I would like my stored procedure to insert a new record into the orders table with the parameters CustomerID, ProductID, OrderDate
ASK:
However, I would only like to insert the record if the OrderDate is between the customer's TermStartDate and TermEndDate
I'm obviously a SQL stored procedure newbie and I don't understand my options in terms of what could happen if the condition is not met, e.g. the record is not inserted and an error message is returned(?). I would also like the stored procedure to consider whether another user is also making changes to the underlying data when the stored procedure is executing (if that needs considering)
-1; or a combination of the above.