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

sql server (alternate) : Stored Procedure Syntax


Jarrod Morrison
5/13/2004 8:34:37 PM
Hi All

Im using a stored procedure on my sql server and am unsure of the syntax
that i should use in it. Im pretty sure that there is a way to do what i
want, but as yet i havent been able to find much info on it. Basically the
procedure takes the machinename and username supplied and searches a table
or two for some matches and this part works great. The only problem i have
is that with the app that ties in with the procedure returns some strange
errors when no matches are found IE a blank recordset is returned. I know
that i can change the program to trap this error but i would prefer to be
able to basically say in the stored procedure, if the result of the SELECT
statement returns no records i want to set the output to be something
instead of nothing if possible. At the moment when i match is found it will
be a 3 digit number IE 001 002 003 etc and i would like to basically say
that if nothing is found make the output to be 000 if possible. Any help is
greatly appreciated

Thanks In Advance


CODE:

CREATE PROCEDURE [dbo].[Memberships]

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

AS

DECLARE @MachineLength Int /* Local Machine Name Length */
DECLARE @SrchInt Int /* Search Loop Integer Counter */
DECLARE @SqlStr VarChar(500) /* 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

SET @SqlStr = @SqlStr + 'UNION SELECT LocationID FROM CustLocations WHERE
MachineName = ' + '''' + @MachineName + ''''

EXEC (@SqlStr)

GO

Bruce Loving
5/14/2004 2:44:43 PM
simple add after the select

if @@rowcount = 0
select '000'



On Thu, 13 May 2004 20:34:37 +1000, "Jarrod Morrison"
[quoted text, click to view]
Joe Celko
5/15/2004 5:16:30 PM
If you'd post DDL and some specs, you can almost certainly write this as
one compiled SELECT statement, instead of this pile of procedural code
and dynamic SQL.

I hope that the VARCHAR(50) datatypes are the results of actual research
and planning. A lot of ACCESS users and newbies use that automatically
and screw up their data integrity. Since @user_name is never used, why
is it a parameter?

Writing parsing loops like this is a really bad coding technique. You
can find a lots of kludges (including one of mine -- Hey, I can write
crappy code, too!) for this in the FAQ for the newsgroup. But the real
answer should look something like this:

SELECT location_id
FROM Locations
WHERE group_id
IN (SELECT machine_name FROM MachineList)
UNION ALL
SELECT location_id
FROM Custlocations
WHERE machine_name
IN (SELECT machine_name FROM MachineList);

I am not sure why Locations and CustLocations are logically different,
but I will assume that they are. Likewise, what is the logical
difference in a group_id and a machine_name? If machines are grouped in
some way, then there might be data design problems.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
John Bell
5/15/2004 6:32:20 PM
Hi

Using Joe's SQL then I think you require something like:

SELECT location_id
FROM Locations
WHERE group_id
IN (SELECT machine_name FROM MachineList)
UNION ALL
SELECT location_id
FROM Custlocations
WHERE machine_name
IN (SELECT machine_name FROM MachineList)
UNION ALL
SELECT '000'
FROM MachineList
WHERE machine_name
NOT IN (SELECT machine_name FROM Custlocations
UNION ALL
SELECT group_id FROM Locations )


or maybe:


SELECT ISNULL(l.location_id , '000' )
FROM MachineList m LEFT JOIN
( SELECT location_id, machine_name
FROM Custlocations
UNION ALL
SELECT location_id, group_id
FROM Locations ) l ON l.machine_name = m.machine_name


John

[quoted text, click to view]

Jarrod Morrison
5/15/2004 10:05:41 PM
Hi Bruce

Thanks for the reply, this sort of does what i wanted, it does return the
000 but it returns it as a second recordset, so i get a blank recordset and
then another one with the 000. The vb program which interfaces with this
procedure returns an error as before because it doesnt see any data as being
returned and returns a BOF or EOF error. If i run the procedure in query
analyzer it returns

LocationID
-Blank-
(No Column Name)
000

Is there a way to make the 000 appear under LocationID ?

Thanks again

[quoted text, click to view]

AddThis Social Bookmark Button