-1

I created a procedure successfully but I have an issue on using parameter while executing the procedure

CREATE PROCEDURE TestProc 
     @table varchar(200)    
AS   
     UPDATE [test].[dbo].[@table]           
     SET [Column 9] = [Column 10]
     WHERE [Column 9] = 'Example1' 
        OR [Column 9] = 'Example2'     
GO

When the procedure is executed like this

EXEC TestProc @table= '2015-07'

I want to update the table [test].[dbo].[2015-07].

Does anyone have an idea how to solve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ibrahim D
  • 79
  • 2
  • 6
  • similar problem [Dynamic update statement with variable column names](http://stackoverflow.com/q/12846743/4275342) – Roman Marusyk Sep 08 '15 at 15:19
  • You can't pass the table as a parameter. Using dynamic SQL in a stored procedure doesn't offer any benefits over writing the statement on the client either. In fact, it's *safer* to create a parameterized query on the client and execute it on the server – Panagiotis Kanavos Sep 08 '15 at 15:22

1 Answers1

0

If I proper urderstood you want to pass name of table to procedure and in procedure update this table. Try to use code below but it will work only for table that have Column 9 and Column 10

CREATE PROCEDURE TestProc @table varchar(200)    
AS   
DECLARE @SQL varchar(500)
SET @SQL = 'UPDATE [test].[dbo].[' + @table + ']           
            SET [Column 9]=[Column 10]
            WHERE [Column 9]= ''Example1'' OR [Column 9]=''Example2''' 
EXEC (@SQL) 
GO
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116