This example shows how to create a stored procedure in an SQL Server database which can select data from various tables, combine the results as a jon query, and make that data available as a table object in memory for further processing. Additionaly a paramter is passed to the query from the code to influence the results.
The example uses the AdventureworksLT database.
The first code block is the stored procedure that query’s some data and builds a result set.
create procedure sp_demoquery(@ignorecust int)
as
-- Discard temp tables if they still exist in this session
IF Object_ID('tempdb..#selection1') IS NOT NULL DROP TABLE #selection1
IF Object_ID('tempdb..#selection2') IS NOT NULL DROP TABLE #selection2
-- Query 1 deliverig the first half of our data
create table #selection1 (salesorderid int, orderdate datetime, salesordernumber nvarchar(25))
insert into #selection1 (salesorderid, orderdate, salesordernumber)
select SalesOrderID, OrderDate,SalesOrderNumber from SalesLT.SalesOrderHeader
where CustomerID <> @ignorecust
-- Query 2 delivering the second half of our data
create table #selection2 (salesid int, billid int, shipid int)
insert into #selection2 (salesid, billid, shipid)
select SalesOrderID as salesid, BillToAddressID as billid, ShipToAddressID as shipid from SalesLT.SalesOrderHeader
where CustomerID <> @ignorecust
-- The resulting join dataset to be returned from the stored procedure
select * from #selection1 as s1
join #selection2 as s2 on s1.salesorderid = s2.salesid
order by salesorderid
The result of the procedure as shown in the sql server manager:

Finaly, the code to execute the stored procedure in the database, and construct the table object holding the resturned data. In it’s simples form we could construct and type a table manualy and then use a datareader to loop the resultsets and fill the table up. This would result in this code:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' --- Create a table to hold the result set
Dim oTable As New DataTable
oTable.Columns.Add("salesordid", Type.GetType("System.Int32"))
oTable.Columns.Add("orderdate", Type.GetType("System.Date"))
oTable.Columns.Add("salesordernumber", Type.GetType("System.String"))
oTable.Columns.Add("salesid", Type.GetType("System.Int32"))
oTable.Columns.Add("billid", Type.GetType("System.Int32"))
oTable.Columns.Add("shipid", Type.GetType("System.Int32"))
' //
' --- Run the stored procedure and retrieve the data
Using oCon As New SqlConnection(My.Settings.ConString)
oCon.Open()
Using oCmd As New SqlCommand("sp_demoquery")
oCmd.CommandType = CommandType.StoredProcedure
oCmd.Parameters.AddWithValue("@ignorecust", 31100)
Using oReader As SqlDataReader = oCmd.ExecuteReader
If oReader.HasRows = True Then
While oReader.Read
' --- Create a new row for our table and retrieve data from the query
Dim oRow As DataRow = oTable.NewRow
oRow("salesordid") = oReader("salesorderid").ToString
oRow("orderdate") = oReader("orderdate").ToString
oRow("salesordernumber") = oReader("salesordernumber").ToString
oRow("salesid") = oReader("salesid").ToString
oRow("billid") = oReader("billid").ToString
oRow("shipid") = oReader("shipid").ToString
oTable.Rows.Add(oRow)
End While
End If
End Using
End Using
End Using
' --- oTable now holds the results of the stored procedure
' Perform your processing here
' //
oTable.Dispose()
End Sub
End Class
Continuing along the same line it is also possible to retrieve the results as a generic datatable object which you can use for further processing. We can also pass parameters to the stored procedure if they are required. To retrieve the output of a strored procedure taking two parameters and return the results as a datatable the code would look as follows:
Using oCon As New SqlConnection(My.Settings.ConString)
oCon.Open()
Using oDa As New SqlDataAdapter
oDa.SelectCommand = New SqlCommand("sp_myprocedure", oCon)
oDa.SelectCommand.CommandType = CommandType.StoredProcedure
oDa.SelectCommand.Parameters.AddWithValue("@parm1", nSomeVal)
oDa.SelectCommand.Parameters.AddWithValue("@parm2", nSomeVal)
Using oDs As New DataSet
oDa.Fill(oDs, "results")
' --- Processing here
' MicroTools.ExcelWriter.Write(oDs)
End Using
End Using
End Using