Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error/problems with using UNION in combo with CURSOR Expand / Collapse
Author
Message
Posted Tuesday, November 10, 2009 8:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 30, 2010 9:36 AM
Points: 5, 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

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
Post #816608
Posted Tuesday, November 10, 2009 9:11 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, November 14, 2014 2:01 PM
Points: 1,207, Visits: 1,277
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.
Post #816621
Posted Tuesday, November 10, 2009 9:13 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:00 AM
Points: 20,808, Visits: 32,747
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

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.



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)
Post #816624
Posted Tuesday, November 10, 2009 10:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 30, 2010 9:36 AM
Points: 5, 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

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.
Post #816676
Posted Tuesday, November 10, 2009 10:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 30, 2010 9:36 AM
Points: 5, 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 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.
Post #816678
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse