Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

SQL - error in using a parameter in SQL Stored procedure

CREATE PROCEDURE FindDuplicateRecordIn
      -- Add the parameters for the stored procedure here
      @table nvarchar(256) = ' ',
      @column nvarchar(256) = ' '

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
      SELECT * FROM @table GROUP BY @column
END

When I compile, I have the following error:

Msg 137, Level 15, State 2, Procedure FindDuplicateRecordIn, Line 16
Must declare the variable '@table'.
GO

I am not sure why, @table is pass down as an argument from the store procedure. I change the @table to @table1.  Same error
Avatar of Sharath S
Sharath S
Flag of United States of America image

I think you want to pass the table name and column name to the SP and get the result grouped on column name. try like this.
CREATE PROCEDURE FindDuplicateRecordIn
      -- Add the parameters for the stored procedure here
      @table nvarchar(256) = ' ', 
      @column nvarchar(256) = ' '

AS
BEGIN
     declare @sql nvarchar(500)
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
      set @sql = 'SELECT * FROM ' + @table + 'GROUP BY ' + @column
      exec(@sql)
END

Open in new window

SOLUTION
Avatar of Eyal
Eyal
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommym121

ASKER

Thanks
Did you try my post? The accepted post and mine are same except I used EXEC. Anyway lets move on.