Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 double select

Author  Topic 

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-05-31 : 14:56:31
How can I do that in one sentence:

SELECT * FROM qqq WHERE condition=1
SELECT * FROM www WHERE condition=1

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-05-31 : 14:58:55
Select * From qqq where condition=1
UNION ALL
Select * From www where condition=1

Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-05-31 : 15:02:16
Hum, not that way because with UNION you need to have the same number of rows
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-31 : 15:17:35
Columns, same number of Columns....

SELECT Col1, Col2, Col3 FROM myTable00
UNION ALL
SELECT Col1, Col2, Null AS Col3 FROM myTable99




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

1fred
Posting Yak Master

158 Posts

Posted - 2005-05-31 : 15:22:13
And why do you need data that do not have the same column name and number to be returned in the same dataset?

If one is have 4 column and the other 5, you can add blank field.

select c1, c2, c3, c4, '' as c5 from qqq where condition = 1
union
select c1, c2, c3, c4, c5 from www where condition = 1

Maybe if you can tell us more about what you want to do we can help you finding the right solution.
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-05-31 : 15:22:34
Good one but not enough for me

Then how can you figure out where come from each row



Does something like that exist:

SELECT * FROM qqq INNER JOIN (SELECT * FROM www WHERE condition=1) WHERE condition=2

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-05-31 : 15:26:32
scrap -- how about you actually tell us what you are trying to do. It's kind of hard to guess. An example with what data you have and what you want to return with some logic to describe it would be helpful.

- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-31 : 15:32:46
[code]
SELECT 'myTable00' AS Source, Col1, Col2, Col3 FROM myTable00
UNION ALL
SELECT 'myTable99' AS Source, Col1, Col2, Null AS Col3 FROM myTable99
[/code]

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-31 : 15:33:14
quote:
Originally posted by jsmith8858

scrap -- how about you actually tell us what you are trying to do. It's kind of hard to guess. An example with what data you have and what you want to return with some logic to describe it would be helpful.

- Jeff



Have you seen my Hint?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-05-31 : 15:33:53
as for distinguishing...

select 'qqq' as src, c1, c2, c3, c4, '' as c5 from qqq where condition = 1
union
select 'www' as src, c1, c2, c3, c4, c5 from www where condition = 1

Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-05-31 : 15:36:04
How can you insert a view in

Select * from QQQ where Condition =2

When I insert the View, everything screw up

Sorry I do not want to show the code.
I know it makes it hard to debug and I guess if there were a solution somebody would have already wrote it.
Go to Top of Page

scrap
Yak Posting Veteran

57 Posts

Posted - 2005-05-31 : 15:44:32
Thanx everyone especially to Seventhnight, 1fred and X002548.
I'm now gone for a few test!
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-05-31 : 15:56:43
as a note when you get back... Brett's hint is not to get to your code. You develop an example that mimics the exact behavior you are trying to build in you production code. Then we help solve the example. You then take that solution and apply it to your production code.

[Trying not to sound arrogant]
We don't need your code, especially if we are helping you with it.
[/Trying not to sound arrogant]

Good luck with it!

Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-31 : 16:07:02
quote:
Originally posted by Seventhnight

[Trying not to sound arrogant]
We don't need your code, especially if we are helping you with it.
[/Trying not to sound arrogant]



Thanks for the chuckle....



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -