Place your output parameter
inside the quotes:
Code:
EXEC('Select @OutID = TestVorgangID from ' + @tableName)
Sometimes it's helpful to PRINT the string you are trying to execute; if you had, you would have seen the problem...
Note that this query will still likely fail, though for a different reason, unless there is only one row in the table whose name you are passing as a parameter.
Edit: Now that I think about it, this query
still won't work, as I'm sure there will be variable scoping problems. That is, the variable @OutID inside the exec is at a different scope that then the @OutID in the stored procedure.
See BOL for sp_executesql for an example of how to return a value via dynamic SQL.
See also
http://p2p.wrox.com/topic.asp?TOPIC_ID=4534 from some security pitfalls using dynamic sql.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com