473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cursor help please

4 New Member
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_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARE CrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @tblName char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @tblName)
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


any help much appreciated.
Sep 25 '06 #1
4 2990
Senthil
10 New Member
Hi,

Variable should be handled as follows.
go throu. the following lines for example..

DECLARE @tblName char(50)
set @tblName='Employee' --Table name
DECLARE @count char(500)
set @count='(select count (*) from ' + rtrim(isnull(@tblName,0)) + ')'
PRINT @count

Then Use the variable, @count to update the second column.
Hope U understand.

Regards,
Sharmila
Sep 26 '06 #2
jazzpies
4 New Member
Thanks for help but am now getting bad syntax errors around
set @count='(select count (*) from ' + rtrim(isnull(@tblName,0)) + ')'
Sep 28 '06 #3
jazzpies
4 New Member
The two SQL statements I have tried now are:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateTablesUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTablesUpdate]
GO


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARE CrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @tblName char(50)
DECLARE @NoRows char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = 'select count (*) from @tblName'
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


The above piece of code simply puts the text select count (*) from @tblName in each row in destination table.
There fore I tried:


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateTablesUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTablesUpdate]
GO


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARE CrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @tblName char(50)
DECLARE @NoRows char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @tblName)
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Which is giving error
Server: Msg 137, Level 15, State 2, Procedure sp_UpdateTablesUpdate, Line 14
Must declare the variable '@tblName'.

I don't understand this as the variable is working perfectly in the next line.

I also tried putting the select statement into a variable called @NoRows which didn't work either (same two results as above)

Anymore help much appreciated.
Sep 28 '06 #4
jazzpies
4 New Member
Thanks for the help.
Managed to crack it by putting @NoRows into a temp table temprows then updating UpdateTables from this. As follows

if exists (select * from dbo.sysobjects where id =

object_id(N'[dbo].[cp_UpdateTablesUpdate]') and OBJECTPROPERTY(id,

N'IsProcedure') = 1)
drop procedure [dbo].[cp_UpdateTablesUpdate]
GO


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[cp_UpdateTablesUpdate]
as
DECLARE CrExtractUpdateCount CURSOR FOR SELECT Tablename FROM

dbo.UpdateTables
DECLARE @tblName char(50)
DECLARE @NoRows char(50)
DECLARE @NoRowsC char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
set @NoRows = 'select count (*) from ' + @tblName
insert into temprows exec (@NoRows)
UPDATE UpdateTables
SET Number_Rows = temprows.execreturn
from temprows
WHERE dbo.[UpdateTables].[Tablename] = @tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

exec cp_UpdateTablesUpdate
Sep 29 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6923
by: Robin Shuff | last post by:
Hi, I'm trying to limit the movement of the mouse cursor in using a VB app. The idea is to stop the cursor straying on to the second monitor of a dual screen set-up (i.e. a projector) while this...
2
383
by: malbanese | last post by:
I am building an ASP.Net application that occasionaly "stalls" while running a server side process like a large database update. During this tine the user does not get any feedback as to what is...
0
1730
by: washingtonirving | last post by:
Hi. I sent this bug to the bugs mailing list. But the email never turned up on the list. Could somebody here tell me what I'm doing wrong, and why the email won't show up? Am I breaking some...
4
2662
by: BBFrost | last post by:
We have a data entry application written within Framework 1.0 that contains 20+ custom user controls. The controls overlay each other so that only one custom control is visible at a time. What...
5
14420
by: Vlad Simionescu | last post by:
Hello I'm trying to let my Windows Form application perform a lengthy operation while displaying a progress bar in a modal dialog window; the dialog has a cancel button. The dialog is displayed...
8
13976
by: Yusuf INCEKARA | last post by:
I have a stored procedure : CREATE PROCEDURE STP_GETSTORELIST @RETCUR CURSOR VARYING OUTPUT AS set @RETCUR = CURSOR FORWARD_ONLY STATIC FOR SELECT ID,STORE_NAME FROM T_INF_STORE ORDER BY...
2
1547
by: Alex | last post by:
In the code below, clicking on the button ButtonChangeCursor changes the form's cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the form's cursor back to its original...
12
30978
by: Lucky | last post by:
Hi guys! i want to create one cursor in the t-sql. the problem is i want to use stored procedure instead of select command in cursor. can anyone tell me how can i use stored procedure's o/p to...
4
2538
by: invy | last post by:
Hi, I'm experiencing a problem while declaring scroll cursor. I'm using Embeded SQL with C. my code is given below. #ifdef SQLSRVR_RDBMS EXEC SQL DECLARE CUR_DWNLD SCROLL CURSOR FOR SCURS;...
2
2472
by: ismailc | last post by:
Hi, I need help please! Onmouseover it calls a function, but onmouseover the image it changes the cursor from default ot busy the whole time. I want it to stay to default. I have set it o...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.