I want to use cursors in my script.
Want to use the results of the first cursor as 'where' clause parameters in the second cursor.
However, I'm having issues declaring these cursors as when I try and refer a local variable in my cursor definition it complains it can't see the variable.
Here's the code snippet:

/*
variable declaration
*/
declare
@l_b_account_cd varchar(20),
@l_w_account_cd varchar(20),
@c_b_prim_sys_cd varchar(4),
@c_w_prim_sys_cd varchar(4),
@l_counter int

/*
initialize variables
*/
select @c_bnyl_prim_sys_cd = 'ABCD'
select @c_wss_prim_sys_cd = 'WXYZ'

begin

/*
tweaked cursor definition
*/
declare c_get_b_accounts cursor for
select a.b_account_cd
,a.w_account_cd
from a..a a
for read only
go

/*
tweaked cursor definition
*/
declare c_check_b_portfolio cursor for
select 1
from a..a a
where substring(a.portfolio_cd, 5, 6) = @l_b_account_cd
for read only
go

/*
open the cursor, fetch data from it and loop through this data
*/
open c_get_b_accounts
fetch c_get_b_accounts into @l_b_account_cd, @l_w_account_cd

while @@sqlstatus = 0 -- loop through the cursor while data is being fetched
begin
/*
Check if the account retrieved exists in table a
*/
open c_check_b_portfolio
fetch c_check_b_portfolio into @l_portfolio

if @@sqlstatus = 0 -- there is atleast one matching account
begin

/*
update matching portfolios such that:
*/
........

Can someone please advise what I mjight be doing wrong here. I get errors from the declare cursoe clause. Could use temp tables but rather use cursors provided I can get them to work!

Thanks in anticipation