473,659 Members | 2,690 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_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.
Sep 25 '06 #1
4 2995
Senthil
10 New Member
Hi,

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

DECLARE @tblName char(50)
set @tblName='Emplo yee' --Table name
DECLARE @count char(500)
set @count='(select count (*) from ' + rtrim(isnull(@t blName,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(@t blName,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_UpdateTables Update]') and OBJECTPROPERTY( id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTables Update]
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)
DECLARE @NoRows 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


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_UpdateTables Update]') and OBJECTPROPERTY( id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTables Update]
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)
DECLARE @NoRows 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

Which is giving error
Server: Msg 137, Level 15, State 2, Procedure sp_UpdateTables Update, 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_UpdateTables Update]') and OBJECTPROPERTY( id,

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


SET QUOTED_IDENTIFI ER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[cp_UpdateTables Update]
as
DECLARE CrExtractUpdate Count CURSOR FOR SELECT Tablename FROM

dbo.UpdateTable s
DECLARE @tblName char(50)
DECLARE @NoRows char(50)
DECLARE @NoRowsC char(50)

OPEN CrExtractUpdate Count
FETCH NEXT FROM CrExtractUpdate Count
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
set @NoRows = 'select count (*) from ' + @tblName
insert into temprows exec (@NoRows)
UPDATE UpdateTables
SET Number_Rows = temprows.execre turn
from temprows
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

exec cp_UpdateTables Update
Sep 29 '06 #5

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

Similar topics

2
6936
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 program is running. I've found and have been working with Knowledge Base Article 179192 (http://support.microsoft.com/default.aspx?scid=kb;en-us;179192) which limits the mouse movement to inside a form, but I'd like to adapt this to be limited...
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 going on, an is likely to try clicking things multiple times which could result in an unwanted outcome. Is there any way to change the mouse pointer to an hourglass or other such icon to signal the user that the request is being processed? Thanks ...
0
1740
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 unwritten rule of the bugs list? I would really like to see an answer to this. Thanks a lot! -w ---
4
2680
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 is the best way to keep the cursor from jumping to another non-visible control when the user tabs past the (currently designated) last control on the form. I've had some luck using the Leave event but the last visible control can change...
5
14443
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 in the main thread while the work is done in a background thread. I managed to do it except for one minor problem: I want the cursor to be hourglass when it is over the main window and be normal (arrow) when it is over the dialog box. I cannot do...
8
13996
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 STORE_NAME OPEN @RETCUR
2
1551
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 cursor. For the reasons I explain below I wouldn't expect this code to behave as it does, so why does it work? Public Class Form1 Dim OldCursor As Cursor Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
12
31001
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 create cursor? i'm using sql 2000 and .net 2.0 thanks,
4
2555
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; EXEC SQL PREPARE SCURS FROM :S_QRY; #endif
2
2490
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 default in the div, function & onmouseover but it stil does that <div id="hoverText2" class="main" style="padding: 5px; position: absolute; display: none; width: 200px; cursor : default;"></div>
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
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.