Select with a parameter instead of table name

Last Post 17 May 2007 08:26 PM by dactilifera. 4 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dactilifera
New Member
New Member

--
16 May 2007 03:21 AM
Hy , I've made a stored procedure with 1 parameter @cTableName and I want to return data from that table: select * from @cTableName , that is all. But it does not work. When I execute the stored procedurere I want to send the name of the table : execute MySP "MyTable". Is it possible? Thank you
dactilifera
New Member
New Member

--
16 May 2007 03:38 AM
To be more acqurate: I have a C# application that should insert, delete and update into some tables (in SQLServer database). For delete I was thinking to create a stored procedure (only one) for all my tables with a parameter @TableName varchar(20). In the body of the s.p. I write: delete from @TableName where ... .But this does not work, I can not execute this stored procedure because of this message: Must declare the table variable "@TableName".
skt5000
New Member
New Member

--
16 May 2007 04:48 AM
You can't do "Select cols FROM @TableName". You'll have to use dynamic SQL.


Jim
dactilifera
New Member
New Member

--
16 May 2007 09:41 PM
Thank you! I understand but : I have a stored procedure that delete from any table one row. So I have three parameter: @cTableName varchar(20), @cPK varchar(20), @cValue varchar(20),primary key is something like "abcdefg". I build the @SQL = 'delete from' + @cTableNAme+' where ' + @cPK + ' = ' + @cValue with Exec but I receive an error: MUST DECLARE THE SCALAR VARIABLE '@cValue ' !!! What is wrong? I've tried @SQL = .... @cPK + ' = ' +''' @cValue''' but nothing.
dactilifera
New Member
New Member

--
17 May 2007 08:26 PM
Thank you, It works.
You are not authorized to post a reply.

Acceptable Use Policy