0% found this document useful (0 votes)
52 views

Tutorial On Stored Procedures (For Use in VB)

The document provides a tutorial on using stored procedures in SQL to retrieve and sort data from a database table. It includes: 1) Creating a table called "tblTeam" with sample data and columns for team name, leader name, email, and category ID. 2) Creating a stored procedure called "usp_getTeamInfo" that accepts a category ID parameter and selects records from tblTeam where the category ID matches the parameter. 3) Connecting to the database from an Excel VBA project and using the stored procedure to retrieve records by category, outputting the results to ranges to display the sorted data in Excel.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Tutorial On Stored Procedures (For Use in VB)

The document provides a tutorial on using stored procedures in SQL to retrieve and sort data from a database table. It includes: 1) Creating a table called "tblTeam" with sample data and columns for team name, leader name, email, and category ID. 2) Creating a stored procedure called "usp_getTeamInfo" that accepts a category ID parameter and selects records from tblTeam where the category ID matches the parameter. 3) Connecting to the database from an Excel VBA project and using the stored procedure to retrieve records by category, outputting the results to ranges to display the sorted data in Excel.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tutorial on Stored Procedures (SQL Codes included)

Stored procedures are procedures that are stored within the database, works the same as commands, but is triggered by the program that needs to use it. Stored procedures normally requires a variable that needs to be passed in from the object using it for it to be able to execute SQL Functions normally. In this tutorial, I will create a table in the SQL database called tblTeam, and attempt to get the records out, and sorting them via the categoryID (last column). I will do so by using 4 objRanges and a stored procedure.

Lets start the tutorial .


Create table tblTeam( teamName varchar(20), leaderName varchar(20), email varchar(20), categoryID int ) insert into tblTeam values ('Team A','Alvin','[email protected]',1) insert into tblTeam values ('Team A','Bernard', '[email protected]',1) insert into tblTeam values ('Team C', 'Calvin', '[email protected]',2)

First off, create the table and insert values into the table by adding the codes above.

Secondly, create a new Visual Studios Project (Example, Excel) Once thats done, go to View > Server Explorer. You should end up with something like this.

Right click the databases, then click Add Connection

Add a Microsoft SQL Server connection.

After so, select your server name and your database name. Once thats done, choose OK. You should have something like this

Expand the folders, right click on Stored Procedures then add a new stored procedure.

This is the screen to create a new stored procedure. By default it will create the procedure with the name of dbo.StoredProcedure1, Change the name into something more appropriate by using a suitable prefix (usp_ , [user stored procedure]) After the name of the stored procedure, it will be codes for the variables. Variables that are used in these stored procedures will have a prefix of @. Let us do a very simple Select statement here. Since there are variables that are not constant (categoryID), we will need to use the @ variable here. Modify the codes in the stored procedure so that it looks like
*If you havent save the procedure yet Create Procedure usp_getTeamInfo @CategoryID int AS Select * from tblTeam where CategoryID = @CategoryId

*If you have already saved the procedure Alter Procedure usp_getTeamInfo @CategoryID int As Select * from tblTeamInfo where CategoryID = @CategoryId

Once thats done, we have our stored procedures already. Now, lets go back to the excel file and start using it! Go to Thisworkbook.vbs codes and enter the following:
Dim objCn As New SqlConnection Dim objCmd As New SqlCommand Dim objDa As New SqlDataAdapter Dim objDs As New DataSet Dim objDr As DataRow Dim i As Integer = 1 Dim objSheets As Excel.Worksheet Dim objRange1 As Excel.Range Dim objRange2 As Excel.Range Dim objRange3 As Excel.Range Dim objRangeCategory As Excel.Range

objSheets = Me.Sheets(1) objRangeCategory = objSheets.Cells(1, 1) objRangeCategory.Value = "Category 1" objRange1 = objSheets.Cells(2, 1) objRange2 = objSheets.Cells(2, 2) objRange3 = objSheets.Cells(2, 3) objCn.ConnectionString = CONNSTR objCmd.Connection = objCn objCn.Open() objCmd.CommandType = CommandType.StoredProcedure objCmd.CommandText = "usp_getTeamInfo" objDa.SelectCommand = objCmd

Do Until i = 3 objCmd.Parameters.Add("@CategoryId", SqlDbType.Int) objCmd.Parameters("@CategoryId").Value = i objDa.SelectCommand = objCmd objDa.Fill(objDs, "Data")

For Each objDr In objDs.Tables("Data").Rows objRange1.Value = objDr.Item("TeamName") objRange1 = objRange1.Offset(1, 0) objRange2.Value = objDr.Item("LeaderName") objRange2 = objRange2.Offset(1, 0) objRange3.Value = objDr.Item("Email") objRange3.Value = objRange3.Offset(1, 0) Next

objRange1 = objRange1.Offset(2, 0) objRange2 = objRange2.Offset(2, 0) objRange3 = objRange3.Offset(2, 0) objRangeCategory = objSheets.Cells((objRange1.Row - 1), 1)

If i = 1 Then objRangeCategory.Value = "Category 2" End If objDs.Clear() objCmd.Parameters.Clear() i += 1 Loop

The first chunk is for you to start calling variables to enable interaction with the database. The second chunk is for you to start assigning values to certain cells in the excel worksheet The third chunk is for you to start connecting to the database. The fourth chunk is to start the do until loop, because we want to separate the data by the categoryId, therefore the use of the do while loop. If a stored procedure is being used, the parameters must be added. In this case in the stored procedure weve already assigned a variable (@CategoryId), we must add it into the command. If no parameters are added but is still created in the stored procedure, an error will occur. After adding a parameter in, we need to assign the parameter a value. The value would be i, in the loop, i will automatically +1 after the rows have been displayed. After that, i will loop until it has reached the limit. While looping, the stored procedure will be called, and the data will be passed into the objDss make-believe table called Data. Then , it will be taken apart and assigned into the various objRanges as values. After the data has been taken apart and assigned, the for loop ends. After that, the rows are automatically moved 2 rows down, thus the offset(2,0). The if statement is to prepare the category heading only. It has no other particular use.

You might also like