cVz 19 Junior Poster

Hi there,

I am trying to write a stored procedure that will basically loop through the database, get allthe tables in the database and then for each table in the database it will take an entry of that database and just insert it into that same table again... i am really getting it , the oly problem comes with how to do the insert really ...

Here is my code

ALTER Procedure [dbo].[e_Duplicate] 
(
	@id_Old INT,
	@id_New INT
)
As

-- =============================================
-- Author:		 xxxxxx
-- Create date: xxxxxx
-- Description:	 xxxxxx
-- =============================================

BEGIN
-- Create a table to store all table names that are used
DECLARE @Table_For_Tables AS  TABLE
( PK INT  IDENTITY(1,1) NOT NULL, Table_Names VARCHAR(500) )

-- Insert into that table
INSERT INTO @Table_For_Tables
( Table_Names )
SELECT DISTINCT Table_Name 
FROM dbo.Items -- This table has a list of all tables
WHERE Table_Name != ''
ORDER BY Table_Name

-- Create a cursor to loop through the tables and update them
DECLARE @tableID INT -- For use with sorting in the loop
DECLARE myCursor CURSOR FORWARD_ONLY FOR SELECT PK 
FROM @Table_For_Tables ORDER BY PK ASC;
	OPEN myCursor
		FETCH NEXT from myCursor INTO @tableID
			WHILE (@@fetch_status = 0)
			BEGIN

			DECLARE @myTableName VARCHAR(500)
			SET @myTableName = (SELECT TOP(1) Table_Names FROM @Table_For_Tables WHERE PK = @tableID)
			
			INSERT INTO @myTableName -- THIS IS WHERE I CANT FIGURE OUT THE NEXT MOVE
			SELECT TOP(1) * FROM @myTableName WHERE Esid = @Esid_Old

			UPDATE @myTableName SET TOP(1) Esid = @Esid_New

			FETCH NEXT FROM myCursor INTO @tableID;	
			END;
		CLOSE myCursor;
	DEALLOCATE myCursor;


SELECT * FROM @Table_For_Tables

END

Does anyone have the missing link ??? hehe ...

Thanks