Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error/problems with using UNION in combo with CURSOR


Error/problems with using UNION in combo with CURSOR

Author
Message
EK rook
EK rook
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 15
Hey everbody,

I have(or better yet tried to) create a cursor which acquires data from a table created from the union of two other tables in the same database, this is for all row that have column1 in table1 matching column2 in table2. Unfortunately the table formed doesn't seem to include the data gathered from the second select Unsure

My Code for doing this is as below:


DECLARE CURS0 CURSOR FOR
SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, subqueryalias.col4, subqueryalias.col5, subqueryalias.col6
FROM (
SELECT col1, col2, col3 FROM Table1 WHERE (EXISTS (SELECT col5 FROM Table2 WHERE col1 =c5))
UNION ALL
SELECT col4, col5, col6 FROM Table2
) as subqueryalias
WHERE subqueryalias.col1 = subqueryalias.col5



This causes Error 207: Invalid columns names 'col4','col5','col6'.
Col5 is flagged twice because its used in the where clause

Once I get rid of the invalid columns; so the SELECT Statement is just SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, the error disappears , but when i test the contents of the cursor, there is no data found.

I've been working at this for practically the whole day, so any help with overcoming the problem would be greatly appreciated. Thanks in advanced :-)
matt6288
matt6288
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1238 Visits: 1351
A union generates more rows not more columns. Does this return what you expect?


SELECT col1, col2, col3 FROM Table1 WHERE (EXISTS (SELECT col5 FROM Table2 WHERE col1 =c5))
UNION ALL
SELECT col4, col5, col6 FROM Table2



There will only be 3 columns returned from this statement. Their headings would be col1, col2, col3. The data for col4, col5, col6 will be there but the first heaing will be used.
Lynn Pettis
Lynn Pettis
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27471 Visits: 38151
EK rook (11/10/2009)
Hey everbody,

I have(or better yet tried to) create a cursor which acquires data from a table created from the union of two other tables in the same database, this is for all row that have column1 in table1 matching column2 in table2. Unfortunately the table formed doesn't seem to include the data gathered from the second select Unsure

My Code for doing this is as below:


DECLARE CURS0 CURSOR FOR
SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, subqueryalias.col4, subqueryalias.col5, subqueryalias.col6
FROM (
SELECT col1, col2, col3 FROM Table1 WHERE (EXISTS (SELECT col5 FROM Table2 WHERE col1 =c5))
UNION ALL
SELECT col4, col5, col6 FROM Table2
) as subqueryalias
WHERE subqueryalias.col1 = subqueryalias.col5



This causes Error 207: Invalid columns names 'col4','col5','col6'.
Col5 is flagged twice because its used in the where clause

Once I get rid of the invalid columns; so the SELECT Statement is just SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, the error disappears , but when i test the contents of the cursor, there is no data found.

I've been working at this for practically the whole day, so any help with overcoming the problem would be greatly appreciated. Thanks in advanced :-)


First of all, there is no col4, col5, col6 returned from the subquery. There are only 3 columns; col1, col2, col3.

You may want to take some time read a little more about UNION in BOL (MS SQL Server Books Online, the help system).

It may help if you would fully explain what it is you are trying to accomplish. Also, is a cursor really needed? There may be a set-based solution to your problem that will work better and be more scalable as well.

I'd also like to recommend that you read the first article I reference below in my signature block regarding asking for assistance. The more information you can provide, the better answers you will get in return.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
EK rook
EK rook
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 15
Lynn Pettis (11/10/2009)
EK rook (11/10/2009)
Hey everbody,

I have(or better yet tried to) create a cursor which acquires data from a table created from the union of two other tables in the same database, this is for all row that have column1 in table1 matching column2 in table2. Unfortunately the table formed doesn't seem to include the data gathered from the second select Unsure

My Code for doing this is as below:


DECLARE CURS0 CURSOR FOR
SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, subqueryalias.col4, subqueryalias.col5, subqueryalias.col6
FROM (
SELECT col1, col2, col3 FROM Table1 WHERE (EXISTS (SELECT col5 FROM Table2 WHERE col1 =c5))
UNION ALL
SELECT col4, col5, col6 FROM Table2
) as subqueryalias
WHERE subqueryalias.col1 = subqueryalias.col5



This causes Error 207: Invalid columns names 'col4','col5','col6'.
Col5 is flagged twice because its used in the where clause

Once I get rid of the invalid columns; so the SELECT Statement is just SELECT subqueryalias.col1, subqueryalias.col2, subqueryalias.col3, the error disappears , but when i test the contents of the cursor, there is no data found.

I've been working at this for practically the whole day, so any help with overcoming the problem would be greatly appreciated. Thanks in advanced :-)


First of all, there is no col4, col5, col6 returned from the subquery. There are only 3 columns; col1, col2, col3.

You may want to take some time read a little more about UNION in BOL (MS SQL Server Books Online, the help system).

It may help if you would fully explain what it is you are trying to accomplish. Also, is a cursor really needed? There may be a set-based solution to your problem that will work better and be more scalable as well.

I'd also like to recommend that you read the first article I reference below in my signature block regarding asking for assistance. The more information you can provide, the better answers you will get in return.


Ahh I wasn't aware the UNION only would used the columns just to return more rows. I must re think what how I am going to do this now . I will also look at the articles you referenced, thanks.
EK rook
EK rook
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 15
Matt Wilhoite (11/10/2009)
A union generates more rows not more columns. Does this return what you expect?


SELECT col1, col2, col3 FROM Table1 WHERE (EXISTS (SELECT col5 FROM Table2 WHERE col1 =c5))
UNION ALL
SELECT col4, col5, col6 FROM Table2



There will only be 3 columns returned from this statement. Their headings would be col1, col2, col3. The data for col4, col5, col6 will be there but the first heaing will be used.


No Crying it doesn't return what i was expecting hmm I was expecting a combination of all 6 columns but since UNION doesn't do this I will have to find another way, thanks for your help.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search