Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 problem with Dynamic sql in cursor

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-03-02 : 07:56:10
Sheeja M. writes "I want to pass a tablename (tbl_comm_param_new) to this cursor. I tried, and almost got there, but getting the following error


Server: Msg 214, Level 16, State 3, Procedure sp_executesql, Line 20
Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'.


my cursor is-


DECLARE @my_cur CURSOR,
@tblv nvarchar,
@QRYTORUN nvarchar,
@ccount int,
@circle nvarchar;

begin

set @tblv = 'tbl_comm_param_new'

select @QRYTORUN = 'SET @my_cur = CURSOR FOR select param_id from ' + @tblv + ' ; OPEN @my_cur'
select @QRYTORUN = @QRYTORUN + '@my_cur cursor OUTPUT'

-- EXEC sp_executesql
-- N'SET @my_cur = CURSOR FOR select param_id from ' + @tblv + ' ; OPEN @my_cur',
-- N'@my_cur cursor OUTPUT', @my_cur OUTPUT



EXEC sp_executesql
@QRYTORUN , @my_cur OUTPUT


-- FETCH NEXT FROM @my_cur
FETCH @my_cur
INTO
@ccount

WHILE @@FETCH_STATUS = 0
BEGIN --loop
print @ccount
end

end




regards,
Sheeja M."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-02 : 08:07:05
Give more details on what you are trying to do. There can be better method to do that than you think. Anyway refer this to know about Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -