Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using the northwind table create a stored procedure for inserting a record for t

ID: 3570459 • Letter: U

Question

using the northwind table

create a stored procedure for inserting a record for the orderDetails table.

a stored procedures is needed to insert a record for the orderDetails table. the stored procedure must define parameters for each field used in the insert statment. vladiate the data before the insert statement is executed. use the following validations rules:

OrderID must exist in the Orders table

ProductID must exist in the Products table

UnitePrice must be greater than 0

Quantity must be greater than 1

Discount cannot be less than 0

if any of the above are true then an error number will be returned

Explanation / Answer

The following example is for creating a simple insert stored procedure. You can run it through an explicit call from a host language program or directly from a DBMS query execution shell like SQL Server Management Studio or dbOrchestra.

You will note that just like in a INSERT statement you do not have to use all of the columns available when creating a stored procedure. You must however populate all columnar data associated with the PK (primary key), and columns associated with unique indexes (note: there are exceptions to this, but they will not addressed here), and columns defined in the ddl as "NOT NULL".

Executing the sql insert stored procedure

Execute insert stored procedure

To run the insert stored procedure you need to supply a value to the student_id variable as well as populate all required data. In this example I have included a cross section of columns for your reference, as well as the datatype associated with the columns in our SQL INSERT.

A few thing to point out regarding the code above. Datetime and character data is entered using single quoted string. Integer data is entered without quotes. I also included all columns that were defined as NOT NULL. I also included the password column which is part of a unique index. You will note that I did not include columns for some of the non-unique indexes (soc_sec_num, other_id_num, and driver_license_num). I did this intentionally to demonstrate that they are not required columns. Having said this, in the "real world" one would only put indexes on columns where the intent was to actually collect the data for that column. I just wanted to make a technical point here. The pragmatic point is that you would want to expose columns that are part of indexes.

What stored row looks like after the SQL INSERT

I want to call you attention to the fact that all columns that are not referenced in the query get set to null. Also, if the schema had default values defined these would also get stored for that column when the rows gets inserted.

Link to schema for Students table