Insert Data into a Table if a condition is met with SQL Server Stored Procedure
<p>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.</p>

<p>I will use a basic example:   </p>

<p>I have 2 tables, <code>customers</code> and <code>orders</code> with a simple one-to-many relationship on the CustomerID.</p>

<p><strong>Customers table contains:</strong><br>
 CustomerID, CustomerName, TermStartDate, TermEndDate</p>

<p><strong>Orders table contains:</strong><br>
contains OrderID, CustomerID, ProductID, OrderDate</p>

<p>I would like my stored procedure to insert a new record into the orders table with the parameters CustomerID, ProductID, OrderDate</p>

<p><strong>ASK:</strong><br>
However, I would only like to insert the record if the OrderDate is between the customer's TermStartDate and TermEndDate</p>

<p>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)</p>