Link to home
Start Free TrialLog in
Avatar of vijay11
vijay11

asked on

Can some one write this with out cursor

Can some one write this sql with out cursor

declare @state_id int , @num varchar(100), @all varchar(2000)

                  declare cur_state cursor for
                  select state_id
                  from state

                    open cur_state
                  while (1=1)
                  begin
                        fetch cur_inst into @inst_id
                        if @@fetch_status<>0 break


                        declare cur_acct cursor for
                        select ltrim(rtrim(data))
                        from view_values v
                        where v.type='P'
                        and v.status='A'
                        and v.name='Acc'
                        and v.state_id=@state_id

                        select @all =''
                        open cur_acct
                        while (1=1)
                        begin
                              fetch cur_acct into @num
                              if @@fetch_status <> 0 break
               
                              select @all = @all + ' / '  + @num
                        end
                        close cur_acct
                        deallocate cur_acct

                        if @all <> ''
                        begin
                              
                              select @all = substring(@all, 4,len(@all) - 3)

                              update country
                              set acct_no = @all
                              where state_id=@state_id
                        end
                   
                  end
                  close cur_state
                  deallocate cur_state
            end

This should be compatible ith sql 2000 also

Thanks in advance


ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Create a function using attached code, after which you can update using the command "update country set acct_no = joindata(state_id)"

 
create function joindata (@stateid int)
returns varchar(2000) 
as BEGIN
	declare @all varchar(2000)
	set @all = '/'
	while exists(select top 1 ltrim(rtrim(data)) from view_values v where v.type='P' and v.status='A' and v.name='Acc' and v.state_id=@state_id and @all not like '%/'+ltrim(rtrim(data))+'/%' )
	BEGIN
		set @all = @all + (select top 1 ltrim(rtrim(data))+'/' from view_values v where v.type='P' and v.status='A' and v.name='Acc' and v.state_id=@state_id and @all not like '%/'+ltrim(rtrim(data))+'/%' )
	END
	if len(@all)>1 set @all = left(@all, len(@all)-1)		-- to drop ending /
	if len(@all)>1 set @all = right(@all, len(@all)-1)	    -- to drop starting /
	return (@all)
END

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kevin,

>>In case your other question is going down a similar<<
I think you mean questions, here is a third duplicate question:
https://www.experts-exchange.com/questions/27028021/can-some-one-help-with-this-logic.html

Anthony