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(@MachineNam e)
SET @SqlStr = 'SELECT LocationID FROM Locations WHERE GroupID = '''
WHILE @SrchInt <= @MachineLength
BEGIN
SET @CurrMach =LEFT(@MachineN ame,@SrchInt)
IF @SrchInt = 1
BEGIN
SET @SqlStr = @SqlStr + LEFT(@MachineNa me,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
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(@MachineNam e)
SET @SqlStr = 'SELECT LocationID FROM Locations WHERE GroupID = '''
WHILE @SrchInt <= @MachineLength
BEGIN
SET @CurrMach =LEFT(@MachineN ame,@SrchInt)
IF @SrchInt = 1
BEGIN
SET @SqlStr = @SqlStr + LEFT(@MachineNa me,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
Comment