Tema: Cursor T-SQL
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2006, 08:24
Avatar de Andres95
Andres95
Colaborador
 
Fecha de Ingreso: diciembre-2004
Mensajes: 1.802
Antigüedad: 19 años, 5 meses
Puntos: 38
asi no se puede.

podrias hacer una tabla temporal. y en ella almacenar el contenido de tu sql dinamico, ejecutandolo con un EXECUTE (@strSQL) o un executesql.


Código:
Declare @sqltext varchar(8000)

If Object_id('tempdb..#tmpCursor') is not null
   Drop table #tmpCursor

Create table #tmpCursor 
(
    campo1 tipo1,
    campo2 tipo2
)

Set @sqltext = 'Insert into #tmpCursor select campo1, campo2 from tabla '

If @param = 'A'
    Set @sqltext = @sqltext + ' where campo_filtro = ''' + @varaux

Execute(@sqltext)

-- Cuando no tiene nada la tabla es mejor salir.
If (Select count(*) from #tmpCursor (nolock)) = 0
    goto Final

Declare cursor_aux cursor for 
Select * from #tmpCursor (nolock)

Open cursor_aux
fetch next from cursor_aux into @campo1, @campo2

While @@fetch_status=0
Begin
     -- Aqui deberia funcionar correcto el cursor.  
End

Final:

If Object_id('tempdb..#tmpCursor') is not null
   Drop table #tmpCursor