Groups | Blog | Home
all groups > sql server (alternate) > january 2004 >

sql server (alternate) : Stored Procedures With VB



Simon Hayes
1/7/2004 1:42:09 PM

[quoted text, click to view]

<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

tim felton
1/7/2004 1:52:45 PM
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

[quoted text, click to view]

Craig Kelly
1/7/2004 7:04:39 PM
[quoted text, click to view]

<snip>

[quoted text, click to view]

<snip>

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

Set Rstest = Rstest.NextRecordset

Craig

Jarrod Morrison
1/7/2004 8:08:55 PM
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

Jarrod Morrison
1/8/2004 1:01:34 AM
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


[quoted text, click to view]

Jarrod Morrison
1/8/2004 4:31:21 PM
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

[quoted text, click to view]

AddThis Social Bookmark Button