Hello. I'm am trying to write a cursor in SQL Server 2000 that looks in a table with a list of tablenames (UpdateTables), looks up the number of rows in that table and then enters the number of rows in the second column of the lookup table against the correct tablename. I have written the following but am getting an error message saying that I need to declare the variable @tblName
GO
SET QUOTED_IDENTIFI ER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTables Update]
as
DECLARE CrExtractUpdate Count CURSOR FOR SELECT Tablename FROM dbo.UpdateTable s
DECLARE @tblName char(50)
OPEN CrExtractUpdate Count
FETCH NEXT FROM CrExtractUpdate Count
INTO @tblName
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @tblName)
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdate Count
INTO @tblName
END
CLOSE CrExtractUpdate Count
DEALLOCATE CrExtractUpdate Count
GO
SET QUOTED_IDENTIFI ER OFF
GO
SET ANSI_NULLS ON
GO
any help much appreciated.
GO
SET QUOTED_IDENTIFI ER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTables Update]
as
DECLARE CrExtractUpdate Count CURSOR FOR SELECT Tablename FROM dbo.UpdateTable s
DECLARE @tblName char(50)
OPEN CrExtractUpdate Count
FETCH NEXT FROM CrExtractUpdate Count
INTO @tblName
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @tblName)
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdate Count
INTO @tblName
END
CLOSE CrExtractUpdate Count
DEALLOCATE CrExtractUpdate Count
GO
SET QUOTED_IDENTIFI ER OFF
GO
SET ANSI_NULLS ON
GO
any help much appreciated.
Comment