You are on page 1of 15

Hands-on Activity 6.

1 - Stored Procedures
Musni, Christian Marc M. 10/07/2021
CPE21S4 Engr. Roman M. Richard

Procedure:

Figure 1.0: Creating Stored Procedure


In this figure, you can see that I am creating a procedure by expanding your desire database, expand
programmability, right click on stored procedure, and then click New Stored procedure, after that a window
will pop up like in the figure above. I have observed that there are guides that you can rely on and you can
also specify the value automatically by clicking specify values for template parameters in the query tab.

Figure 1.1: Creating Stored Procedure using T-SQL


From what I have observed, creating a stored procedure is also relatively easy using a T-SQL or creating a
query, you can just put the commands, you can even put the parameters, name of the procedure, etc. And
you can see in the figure above is an example of a query where you can create a procedure.
Figure 1.2: Executing a stored procedure
I have observed that to execute a stored procedure, you have to use T-SQL and write the necessary
command such as EXEC space and then name of the procedure you want to execute just like what is
shown in the figure above.

Figure 1.3: Modifying a stored procedure


To easily modify a stored procedure, you just have to right click the procedure you want to modify and
simply click the modify.

Figure 1.4: Specifying parameters


In this figure, you can see a query that creates a procedure with parameters where @departmentid is set to
int and we specified what data or information that will be selected.
Figure 1.5: Returning a data
As you can see in this figure, this query or T-SQL is different from previous one because there is a data
that is returned. And from what I have observed, there is a begin and end statement in this query.

Database Output:

Figure 2.0 Stored procedure output


As you can see in the figure above, the stored procedure was successfully created using the method that is
shown in the procedure previously. I can execute It now whenever I want to use it.

Figure 2.1 Stored procedure using T-SQL Output


As you can see in this figure, the stored procedure was also successfully created even if it is was created
using T-SQL.
Figure 2.2: Executing a stored procedure output
As you can see in this output, I successfully executed the stored procedure called displayEmployees
using T-SQL and using the statement called EXEC.

Figure 2.3: Modifying a code

Figure 2.4: Output of modified code


As you can see in the figure 2.3, we have modified the code that we have created before, and we have
added a parameter and executed it. You can see in the figure 2.4 that the modified procedure was
executed successfully and even if you modify the code, it will still work.
Figure 2.5: Stored procedure with specified parameters
As you can see in the figure above, the stored procedure that has specified parameters is executed
differently than stored procedure with no parameters. If you want to execute a stored procedure that has
specified parameters, you have to call or set the condition for the parameters, for example you want to call
or select that has an integer of 1 in their department id, you have to set the condition that @departmentid =
1.

Figure 2.6: Returning a data in Stored Procedure


As you can see in the figure above, we have executed the stored procedure that we made that has a return
statement and it works the same way as the previous one where we have set a parameter. But the
difference is that it will return a data depending on the condition you have set in exec for example if you
have set that @departmentid = 1 it will return the data that matches your condition.

Figure 2.7: Deleting a stored procedure


As you can see in the figure above, you can easily delete a stored procedure by right clicking the stored
procedure you want to remove and click delete, then this window will pop up and you just have to click ok
and the stored procedure will be deleted.
Supplementary Activities:

Figure 3.0: Creating the database and tables


As you can see in this figure, I have created a script where the query will check if there is already a
database called TruckingDB in the SQL and if there is one it will drop it and make a new TruckingDB
database. Just like the in the databases, i also set the script that it will drop the table if there is already a
table named as the one, I am creating and create a new one. I already set the appropriate data types and
keys in this script, given values inside the table are the only one missing.

Figure 3.1: Creating the database and tables output


As you can see in this figure, after executing the script, I have successfully created the database and the
tables. The data types and keys of the tables are also correct.
Figure 3.2: Inserting values
As you can see in this figure, I have created a code where it will automatically insert the values that I have
inputted in the script using insert column_name values(values you will input).

Figure 3.3: Inserting values output


As you can see in the figure above, it displayed the data inside the table and the data are successfully
inserted in the tables.
Figure 3.4: Truck per code Base
For the problem that I need to display the total number of trucks based on the base code, I made a stored
procedure that has a parameter for base code so that, to execute the stored procedure, the user just have
to put the base code and the stored procedure will display the total number of trucks with that base code
and using this script it will display the truck number, base code and base manager.

Figure 3.5: Truck per base code output


As you can see in the figure above, after executing the stored procedure with a parameter of 501, it
displayed the trucks that has a base code of 501 and displayed the total number of trucks.
Figure 3.6: Truck per type code
Just like the previous stored procedure, I also inputted a parameter to solve the problem. I set the type
code as the parameter so that, the user will just have to put the type code of the truck and it will
automatically output the trucks that has that code and using this script it will also display the truck number,
type code, and type description.

Figure 3.7: Truck per type code output


As you can see in the figure above, I executed it by inputting the name of the stored procedure and
inputted the required parameter wich is the type code and I set it to 1 so that only trucks that has a type
code of 1 will be displayed. And you will see that the stored procedure worked perfectly and even
displayed the total number of trucks.
Figure 3.8: Display certain columns
So, for this problem, I must display only 4 columns from 3 different tables. And to solve this problem, I
simply created a script where the stored procedure will only display the truck_num, base_city,
base_manager and type_description from truck, base, type as shown in the figure above and I also set
different conditions that is equal with different columns so that they will be joined if they have the same
code.

Figure 3.9: Display certain columns output


As you can see in the figure above, the only necessary columns are only displayed after executing the
stored procedure.
Figure 3.10: Trucks by month
For this problem, I have inputted a parameter month so that the stored procedure will only be executed if
the user put the month of the truck buy date. Since the date has day, month, and year I used a function
called month(date) so that only the month is required for the parameter/

Figure 3.11: Trucks by month output


As you can see in this output, first the stored procedure will display the total number of trucks per month
and then also display the total number of trucks for the month that you want.
Figure 3.12: Truck miles
For this problem, I only need to set a condition of the truck miles that will be displayed. So I created a script
like this so that after the user executed the stored procedure, it will only display the truck miles that is
between 5000 and 40000.

Figure 3.13: Truck miles output


After executing the stored procedure, it only displayed the trucks that has a truck miles ranges from 10000
to 40000 just like the problem said.
Figure 3.14: Truck by year
As you can see in the script above, there’s a parameter called year. This parameter will be used to execute
the stored procedure, the user must put the year of the trucks he/she want to be displayed. And I also used
inner join to join truck and type table.

Figure 3.15: Truck by year output


As you can see in the figure above, I have set the parameters to be 2007 so that it will only display the
trucks that is bought in 2007. And after executing, it properly displayed the trucks that is bought in the year
2007.
Conclusion:
I therefore conclude that I learned a lot in this activity especially about stored procedure. Through
this activity, I learned how to properly implement a stored procedure and how to execute them. I learned that
stored procedure can be helpful in executing SQL statements especially if it is a long one because in stored
procedure, we are grouping the SQL statements into one and then execute them at once, so it is easier. I
also learned that you can also use a stored procedure in selecting specific values, and it will be easier to call
or execute because it is saved as a stored procedure, you just have to execute it.

Overall, I learned a lot in this activity and gained a lot of knowledge about stored procedure. This
activity also helped me practiced and improve my skills in executing statements and using SQL in general. I
hope this knowledge will stay in me and can be applied in future activities.

“ I affirm that I shall not give or receive any unauthorized help on this assignment and that all work
is my own”

Assessment Rubric:

You might also like