Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I have an input paramater in a stored procedure be a table name?

Status
Not open for further replies.

Cardshark

Programmer
Sep 7, 2000
2
US
Can I have an input paramater in a stored procedure be a table name?
If so, how do I use the paramater in the 'FOR' clause to set a cursor.
Example:
DECLARE curs CURSOR
FOR SELECT @pTableName WHERE ...

and so fourth?
 
Well guys i answered my own question:
The trick is to put the whole thing into one statement.
if anyone knows a better way, please post it ;)


declare @xxx varchar (5000)
declare @pTableName varchar (10)
declare @pNew varchar(6)
declare @pOld varchar(6)
declare @ID1 as varchar (6)

set @ID1 = 'ID1'
set @pTableName = 'JoshTemp'
set @pOld = 'josh2'
set @pNew = 'josh'
set @xxx = "


declare @ID2 varchar (6)
declare TmpCur cursor

for " +

"Select " + @ID1 + " from " + @pTableName + " where " + @id1 + " = '" + @pOld + "'" +

" open TmpCur

fetch next from TmpCur into @ID2

while (@@fetch_status <> -1)
begin
update &quot; + @pTableName +
&quot; set &quot; + @ID1 + &quot; = '&quot; + @pNew + &quot;'

where current of TmpCur

fetch next from TmpCur into @Id2

end
deallocate TmpCur&quot;
exec( @xxx)
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top