473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

I need a little cursor help

CK
Good Morning All,
Can use use a variable for the FOR clause in a cursor?

Example
I have
DECLARE @a varchar(50), @b varchar(50), @c varchar(50)
DECLARE @sql varchar(255)
DECLARE @x varchar(50), @y varchar(50), @z varchar(50)

DELCARE outer_cursor CURSOR FOR
SELECT this, that, other
FROM sometable

OPEN outer_cursor
FETCH NEXT FROM outer_cursor
INTO @a, @b, @c

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = 'SELECT ' + @a + ', ' + @b + ', ' + @c + ' FROM
someothertable'

--Here is the question. Can I do this? If not how would I do this?
DELCARE inner_cursor CURSOR FOR @sql -- <~~Is this legal?
OPEN inner_cursor
FETCH NEXT FROM inner_cursor
INTO @x, @y, @z

--etc..

Is it legal to do this in SQL? Is there a better way? I know everybody hates
cursors, but I am just curious if this would work.

TIA,
~ck
Sep 11 '08 #1
4 1597
CK (c_**********@hotmail.com) writes:
DELCARE outer_cursor CURSOR FOR
Before you go any furher, make it a habit to create your cursors STATIC:

DECLARE outer_cursor CURSOR STATIC FOR

This can considerably improve the performance of your cursor operations,
and it also avoid surprises if you update rows in the cursor.
--Here is the question. Can I do this? If not how would I do this?
DELCARE inner_cursor CURSOR FOR @sql -- <~~Is this legal?
No that is not legal, but you can use dynamic SQL for the task. I have
a longer article on dynamic SQL on my web site. This link goes directly
to the cursor section:
http://www.sommarskog.se/dynamic_sql.html#cursor.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Sep 11 '08 #2
>Can you use a variable for the FOR clause in a cursor? <<

No. But a better question is why you are using a cursor at all.
Given OLAP functions, CASE expressions, etc. in current Standard SQL,
you should only use 1-2 in your entire career at most.
>Is there a better way? I know everybody hates cursors, but I am just curious if this would work. <<
Well, is there a WORSE way? Dynamic SQL declaring cursors is a
nightmare on soooo many levels. Saying SQL people hate cursors is a
mild understatement. What a cursor says is that either the product or
the programmer is so weak that we have to throw away 30 years of RDBMS
and revert to low-level procedural code. And 99.98% of the time when
you see dynamic SQL, it is an admission that the programmer never had
a basic software engineering course and does not know about cohesion.

What is the actual problem you are trying to solve? Or was this a "If
I poison my cattle, will they die?" kind of question?
Sep 11 '08 #3
CK wrote:
Can use use a variable for the FOR clause in a cursor?

Example
I have
DECLARE @a varchar(50), @b varchar(50), @c varchar(50)
DECLARE @sql varchar(255)
DECLARE @x varchar(50), @y varchar(50), @z varchar(50)

DELCARE outer_cursor CURSOR FOR
SELECT this, that, other
FROM sometable

OPEN outer_cursor
FETCH NEXT FROM outer_cursor
INTO @a, @b, @c

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = 'SELECT ' + @a + ', ' + @b + ', ' + @c + ' FROM
someothertable'

--Here is the question. Can I do this? If not how would I do this?
DELCARE inner_cursor CURSOR FOR @sql -- <~~Is this legal?
OPEN inner_cursor
FETCH NEXT FROM inner_cursor
INTO @x, @y, @z

--etc..

Is it legal to do this in SQL? Is there a better way? I know everybody hates
cursors, but I am just curious if this would work.
There's a reason everybody hates them; they're easy to screw up, and
slow even if you don't. Do you really need to process rows one at a
time? Even if you do, a simple workaround is to have the inner cursor
just pull all columns from someothertable, then dynamically copy the
relevant ones into your variables.

What is the real-world situation behind this example? Knowing that, we
may be able to give more specific suggestions on avoiding all this mess.
Sep 15 '08 #4
CK
How do I write a set based query? I have a groupSets table with fields
setId, idField, datasource, nameField, prefix, active
Data:
1,someIDfield, someTable, someField, pre1, 1
2,someotherIDfield, someTable, someotherField, pre2, 1
3,somethirdIDfield, someTable, somethirdField, pre3, 1
4,somefourthIDfield, someotherTable, somefourthField, pre4, 1

I need to generate records in another table by constructing queries from the
data in groups sets. I need to insert a record for each distinct result of
the query.
Example:
SELECT DISTINCT someIDfield FROM someTable WHERE someIDfield IS NOT NULL

then I need to do an insert for each result of the above query

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result1)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result2)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result3)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, result4)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (1, prefix + nameField, 1, resultN)

--next reord from groupSets
SELECT DISTINCT someotherIDfield FROM someTable WHERE someotherIDfield IS
NOT NULL
INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result1)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result2)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result3)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, result4)

INSERT INTO groups(setId, groupName, active, groupingEntityID)
VALUES (2, prefix + nameField, 1, resultN)
I basically want to do the same operation on each record in the groupSets
table. How can I accomplish this without a cursor? Any ideas?
Thanks for your help,
~ck

"--CELKO--" <jc*******@earthlink.netwrote in message
news:32**********************************@m3g2000h sc.googlegroups.com...
>>Can you use a variable for the FOR clause in a cursor? <<

No. But a better question is why you are using a cursor at all.
Given OLAP functions, CASE expressions, etc. in current Standard SQL,
you should only use 1-2 in your entire career at most.
>>Is there a better way? I know everybody hates cursors, but I am just
curious if this would work. <<

Well, is there a WORSE way? Dynamic SQL declaring cursors is a
nightmare on soooo many levels. Saying SQL people hate cursors is a
mild understatement. What a cursor says is that either the product or
the programmer is so weak that we have to throw away 30 years of RDBMS
and revert to low-level procedural code. And 99.98% of the time when
you see dynamic SQL, it is an admission that the programmer never had
a basic software engineering course and does not know about cohesion.

What is the actual problem you are trying to solve? Or was this a "If
I poison my cattle, will they die?" kind of question?

Sep 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antonio | last post by:
Hope that some perl guru will help me: I've implemented an inverted index using PERL + DB_File (using hash, and b-tree). Therefore, i have my indexer and my searcher (a little search engine,...
3
by: Fabri | last post by:
Do you know please why in Firefox there is this strange behavior? This work: --> <img src="mickey.jpg" width="200" height="150" onMouseOver="document.body.style.cursor ='move';"> But if...
30
by: nephish | last post by:
Hey there, i have tried about every graphing package for python i can get to work on my system. gnuplot, pychart, biggles, gdchart, etc.. (cant get matplot to work) so far, they all are working...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
3
by: ssb | last post by:
Hello, This may be very elementary, but, need help because I am new to access programming. (1) Say, I have a column EMPLOYEE_NAME. How do I fetch (maybe, cursor ?) the values one by one and...
2
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...
33
by: STILL LEARNING | last post by:
I'm not sure if this can even be done, but what prompts the question is my desire to be able to create an "Uber Link" script/code of some sort, such that even if the html page contains nothing but...
19
by: Ganesh J. Acharya | last post by:
Hi there, I want to redesign my website and make that look professional. I made this about 6 years ago with very little knowledge of internet. Today I am getting about 4000 visitors a day for...
22
by: Chuck Connors | last post by:
Hey guys. I'm working on a little program to help my wife catalog her/ our coupons. I found a good resource but need help formatting the text data so that I can import it into a mysql database....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...
0
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...
0
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...

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.