Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Looping with cursor

$
0
0

Beleive that I can use cursors to loop though a list.

I have a table that lists table names which I want to copy from one db to another.  The code I am trying is as follows:

USE Finance
Declare @ID int
Declare @ActiveTable nchar(30)
Declare c Cursor For Select Distinct ID_IHDBTableName From dbo.IHDBTableNames
Open c


Fetch next From c into @ID



While @@Fetch_Status=0 Begin
   SET @ActiveTable = Select distinct IHDBTableName FROM IHDBTableNames Where ID_IHDBTableName=@ID
   DELETE FROM [dbo].@ActiveTable
   INSERT Finance.[dbo].@ActiveTable SELECT * FROM IHDBNew7.[dbo].@ActiveTable
   Fetch next From c into @ID
End

Close c
Deallocate c

However it falls over at my SET statement.

Any suggestions?

NB I dont want to use the Import \ Export program as I can not edit the dts package it creates.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>