Hi All
Im trying to use the code at the bottom of this message inside my stored
procedure and when i execute the procedure in query analyzer i get the
following error:
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'H'.
This error happens a few times and im pretty sure it is because the select
statement needs the ' around the data that is being searched. This is what
the select statement looks like for me:
SELECT * FROM Locations WHERE GroupID = H OR GroupID = HM OR GroupID = HMS
OR GroupID = HMSS OR GroupID = HMSSR OR GroupID = HMSSRV
And im pretty sure it is failing because sql wants the select statement to
look like this:
SELECT * FROM Locations WHERE GroupID = 'H' OR GroupID = 'HM' OR GroupID =
'HMS' OR GroupID = 'HMSS' OR GroupID = 'HMSSR' OR GroupID = 'HMSSRV'
Am i thinking along the right lines with this ? If so does anybody know of a
way that i can put the ' mark around the the data that is being searched for
? Any help is greatly appreciated
Thanks
CREATE PROCEDURE [dbo].[TestSP]
@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(@MachineNam e)
SET @SqlStr = 'SELECT * FROM Locations WHERE GroupID = '
WHILE @SrchInt <= @MachineLength
BEGIN
SET @CurrMach = LEFT(@MachineNa me,@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
PRINT @SqlStr
END
EXEC (@SqlStr)
GO
Im trying to use the code at the bottom of this message inside my stored
procedure and when i execute the procedure in query analyzer i get the
following error:
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'H'.
This error happens a few times and im pretty sure it is because the select
statement needs the ' around the data that is being searched. This is what
the select statement looks like for me:
SELECT * FROM Locations WHERE GroupID = H OR GroupID = HM OR GroupID = HMS
OR GroupID = HMSS OR GroupID = HMSSR OR GroupID = HMSSRV
And im pretty sure it is failing because sql wants the select statement to
look like this:
SELECT * FROM Locations WHERE GroupID = 'H' OR GroupID = 'HM' OR GroupID =
'HMS' OR GroupID = 'HMSS' OR GroupID = 'HMSSR' OR GroupID = 'HMSSRV'
Am i thinking along the right lines with this ? If so does anybody know of a
way that i can put the ' mark around the the data that is being searched for
? Any help is greatly appreciated
Thanks
CREATE PROCEDURE [dbo].[TestSP]
@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(@MachineNam e)
SET @SqlStr = 'SELECT * FROM Locations WHERE GroupID = '
WHILE @SrchInt <= @MachineLength
BEGIN
SET @CurrMach = LEFT(@MachineNa me,@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
PRINT @SqlStr
END
EXEC (@SqlStr)
GO
Comment