473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Stored Procedures With VB

Hi All

Im unsure of how to use vb to read the results of my stored procedure. Ive
included the stored procedure at the end of this message for reference.
Basically the stored procedure will first of all construct a select string
based on the your computer name and perform this select on a table named
Locations, next it will perform a similar search on a table called
CustLocations. Normally it will return two recordsets in query analyzer that
for example would look like:

LocationId
------------
001

LocationId
-----------
002
005

Now under vb i can access the first result set simpy enough and use a loop
that will go until it reaches EOF to read the recordest, but i cant seem to
figure out how to get vb to see the second result set. Now i may be going
about this the wrong way, and there may be a way to make the stored
procedure show only 1 result set for both searches but im not sure of how to
do that. So any help here is greatly appreciated. Ive also included the vb
code im using for testing, its a little rough.

Thanks In Advance


-----------
VB CODE
------------

Public Sub TestSP()

'On Error Resume Next

Dim PtrlCmd As New ADODB.Command

Dim Rstest As ADODB.Recordset

PtrlCmd.ActiveConnection = CPDBase
PtrlCmd.CommandText = "sp_Test"
PtrlCmd.CommandType = adCmdStoredProc

PtrlCmd.Parameters.Append PtrlCmd.CreateParameter("MachineName",
adVarChar, adParamInput, 50, "HMSSRV")
PtrlCmd.Parameters.Append PtrlCmd.CreateParameter("UserName", adVarChar,
adParamInput, 50, "ADMINISTRATOR")

Set Rstest = PtrlCmd.Execute

' Break Before This Point

Do Until Rstest.EOF = True
MsgBox Rstest.Fields(0)
Rstest.MoveNext
Loop

Rstest.NextRecordset

Do Until Rstest.EOF = True
MsgBox Rstest.Fields(0)
Rstest.MoveNext
Loop

Set PtrlCmd.ActiveConnection = Nothing

End Sub

----------
SQL Stored Procedure
------------

CREATE PROCEDURE [dbo].[sp_Test]

@MachineName VarChar(50),
@UserName VarChar(50)

AS

DECLARE @MachineLength Char(2) /* Local Machine Name Length */
DECLARE @SrchInt Char(1) /* Search Loop Integer Counter */
DECLARE @SqlStr VarChar(300) /* SQL Select String */
DECLARE @CurrMach VarChar(50) /* Local Machine Name Counter */

SET @SrchInt = 1

SET @MachineLength = Len(@MachineName)
SET @SqlStr = 'SELECT LocationID FROM Locations WHERE GroupID = '''

WHILE @SrchInt <= @MachineLength

BEGIN

SET @CurrMach =LEFT(@MachineName,@SrchInt)
IF @SrchInt = 1

BEGIN
SET @SqlStr = @SqlStr + LEFT(@MachineName,1) + ''''
END

IF @SrchInt > 1

BEGIN
SET @SqlStr = @SqlStr + ' OR GroupID = ' + '''' + @CurrMach + ''''
END

SET @SrchInt = @SrchInt + 1

END

EXEC (@SqlStr)

SELECT LocationID FROM CustLocations WHERE MachineName LIKE @MachineName

GO
Jul 20 '05 #1
5 3756

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bt**********@lust.ihug.co.nz...
Hi All

Im unsure of how to use vb to read the results of my stored procedure. Ive
included the stored procedure at the end of this message for reference.
Basically the stored procedure will first of all construct a select string
based on the your computer name and perform this select on a table named
Locations, next it will perform a similar search on a table called
CustLocations. Normally it will return two recordsets in query analyzer that for example would look like:


<snip>

I don't have enough VB knowledge to respond to the issue of multiple
recordsets, but one possible approach would be to return a single result set
from your procedure:

EXEC (@SqlStr + ' UNION ALL SELECT LocationID FROM CustLocations WHERE
MachineName = @MachineName')

You may want to use UNION instead of UNION ALL, if you want to remove
duplicate values (see Books Online). Note that this approach means you have
no way to tell the difference between LocationID values from Locations and
those from CustLocations. If this is important, then you can add a dummy
flag column to your query to indicate the source:

/* L for Location */

SET @SqlStr = 'SELECT ''L'' AS ''Source'', LocationID FROM Locations WHERE
GroupID = '''

/* C for CustLocation */

EXEC (@SqlStr + ' UNION ALL SELECT ''C'', LocationID FROM CustLocations
WHERE MachineName = @MachineName ORDER BY ''Source'' DESC')
Simon
Jul 20 '05 #2
I believe there is a next recordset method that will fetch you the next rs,
if you choose to keep your SQL the same.

Check it out in your object browser F2 and search for NextRecordset

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bt**********@lust.ihug.co.nz...
Hi All

Im unsure of how to use vb to read the results of my stored procedure. Ive
included the stored procedure at the end of this message for reference.
Basically the stored procedure will first of all construct a select string
based on the your computer name and perform this select on a table named
Locations, next it will perform a similar search on a table called
CustLocations. Normally it will return two recordsets in query analyzer that for example would look like:

LocationId
------------
001

LocationId
-----------
002
005

Now under vb i can access the first result set simpy enough and use a loop
that will go until it reaches EOF to read the recordest, but i cant seem to figure out how to get vb to see the second result set. Now i may be going
about this the wrong way, and there may be a way to make the stored
procedure show only 1 result set for both searches but im not sure of how to do that. So any help here is greatly appreciated. Ive also included the vb
code im using for testing, its a little rough.

Thanks In Advance


-----------
VB CODE
------------

Public Sub TestSP()

'On Error Resume Next

Dim PtrlCmd As New ADODB.Command

Dim Rstest As ADODB.Recordset

PtrlCmd.ActiveConnection = CPDBase
PtrlCmd.CommandText = "sp_Test"
PtrlCmd.CommandType = adCmdStoredProc

PtrlCmd.Parameters.Append PtrlCmd.CreateParameter("MachineName",
adVarChar, adParamInput, 50, "HMSSRV")
PtrlCmd.Parameters.Append PtrlCmd.CreateParameter("UserName", adVarChar, adParamInput, 50, "ADMINISTRATOR")

Set Rstest = PtrlCmd.Execute

' Break Before This Point

Do Until Rstest.EOF = True
MsgBox Rstest.Fields(0)
Rstest.MoveNext
Loop

Rstest.NextRecordset

Do Until Rstest.EOF = True
MsgBox Rstest.Fields(0)
Rstest.MoveNext
Loop

Set PtrlCmd.ActiveConnection = Nothing

End Sub

----------
SQL Stored Procedure
------------

CREATE PROCEDURE [dbo].[sp_Test]

@MachineName VarChar(50),
@UserName VarChar(50)

AS

DECLARE @MachineLength Char(2) /* Local Machine Name Length */
DECLARE @SrchInt Char(1) /* Search Loop Integer Counter */
DECLARE @SqlStr VarChar(300) /* SQL Select String */
DECLARE @CurrMach VarChar(50) /* Local Machine Name Counter */

SET @SrchInt = 1

SET @MachineLength = Len(@MachineName)
SET @SqlStr = 'SELECT LocationID FROM Locations WHERE GroupID = '''

WHILE @SrchInt <= @MachineLength

BEGIN

SET @CurrMach =LEFT(@MachineName,@SrchInt)
IF @SrchInt = 1

BEGIN
SET @SqlStr = @SqlStr + LEFT(@MachineName,1) + ''''
END

IF @SrchInt > 1

BEGIN
SET @SqlStr = @SqlStr + ' OR GroupID = ' + '''' + @CurrMach + ''''
END

SET @SrchInt = @SrchInt + 1

END

EXEC (@SqlStr)

SELECT LocationID FROM CustLocations WHERE MachineName LIKE @MachineName

GO

Jul 20 '05 #3
Hi Simon

Thanks heaps for the reply, it works perfectly, i had to change the syntax
slightly because the procedure was using @MachineName as the data to search
for instead of the contents of the variable, so it looked like this

EXEC (@SqlStr + ' UNION ALL SELECT LocationID FROM CustLocations WHERE
MachineName = ' + '''' + @MachineName + '''')

Thanks again
"Simon Hayes" <sq*@hayes.ch> wrote in message
news:3f**********@news.bluewin.ch...

"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bt**********@lust.ihug.co.nz...
Hi All

Im unsure of how to use vb to read the results of my stored procedure. Ive included the stored procedure at the end of this message for reference.
Basically the stored procedure will first of all construct a select string based on the your computer name and perform this select on a table named
Locations, next it will perform a similar search on a table called
CustLocations. Normally it will return two recordsets in query analyzer that
for example would look like:


<snip>

I don't have enough VB knowledge to respond to the issue of multiple
recordsets, but one possible approach would be to return a single result

set from your procedure:

EXEC (@SqlStr + ' UNION ALL SELECT LocationID FROM CustLocations WHERE
MachineName = @MachineName')

You may want to use UNION instead of UNION ALL, if you want to remove
duplicate values (see Books Online). Note that this approach means you have no way to tell the difference between LocationID values from Locations and
those from CustLocations. If this is important, then you can add a dummy
flag column to your query to indicate the source:

/* L for Location */

SET @SqlStr = 'SELECT ''L'' AS ''Source'', LocationID FROM Locations WHERE
GroupID = '''

/* C for CustLocation */

EXEC (@SqlStr + ' UNION ALL SELECT ''C'', LocationID FROM CustLocations
WHERE MachineName = @MachineName ORDER BY ''Source'' DESC')
Simon

Jul 20 '05 #4
"Jarrod Morrison" <ja*****@ihug.com.au> wrote:

<snip>
Rstest.NextRecordset


<snip>

Although I like the use of UNION mentioned already better, to get this to
work, you would use....

Set Rstest = Rstest.NextRecordset

Craig
Jul 20 '05 #5
Hi Craig

Thanks for the reply, its handy to know how to use the .nextrecordset
function anyhow. I tried to use it before but i had the syntax wrong.

Thanks again

"Craig Kelly" <cn*****@worldnet.att.net> wrote in message
news:bL*******************@bgtnsc05-news.ops.worldnet.att.net...
"Jarrod Morrison" <ja*****@ihug.com.au> wrote:

<snip>
Rstest.NextRecordset


<snip>

Although I like the use of UNION mentioned already better, to get this to
work, you would use....

Set Rstest = Rstest.NextRecordset

Craig

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: jrefactors | last post by:
I want to know the differences between SQL Server 2000 stored procedures and oracle stored procedures? Do they have different syntax? The concept should be the same that the stored procedures...
2
by: scott | last post by:
Hi, Just wondering what sort of problems and advantages people have found using stored procedures. I have an app developed in VB6 & VB.NET and our developers are starting to re-write some of the...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: Rhino | last post by:
I am trying to determine the behaviour of stored procedures in DB2 V8.2.x in Windows/Unix/Linux and how I can control that behaviour. Some documentation in the manuals is confusing the issue...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.