Solved

so only one query will return select * from orders

Posted on 2011-03-21
6
220 Views
Last Modified: 2012-05-11
select * from orders where (select top 1(name) from users = 'System')

 Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.



the two tables are not joined and have nothing in common

i want the first table to return results only if the second condition is true

select * from orders where (select top 1(name) from users = 'System')
select * from orders where (select top 1(name) from users != 'System')

so only one query will return select * from orders

0
Comment
Question by:rgb192
6 Comments
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 35182442
You are not telling the first query what to search for. Try something like below.

select * from orders where name in (select top 1(name) from users = 'System')

Open in new window

0
 
LVL 2

Expert Comment

by:chedlin
ID: 35182469
I'm sure there are clearer ways to do this, but you can fix these queries by moving your )

select * from orders where (select top 1(name) from users = 'System')
select * from orders where (select top 1(name) from users != 'System')

become

select * from orders where (select top 1(name) from users) = 'System'
select * from orders where (select top 1(name) from users) != 'System'

0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 35182511
have you tried:
select * from orders where yourcolumnnamegoeshere  = (select top 1 name from users where yourcolumnnamegoeshere = 'System')
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 35183813
try like this.
SELECT * 
  FROM orders 
 WHERE 1 = (SELECT TOP 1 1 
              FROM users 
             WHERE name = 'System')

Open in new window

0
 
LVL 2

Expert Comment

by:chedlin
ID: 35184929
If you don't need it to return an empty result set I would move the logic into an if statement.   It would be much clearer.

if ((SELECT TOP 1 name from users) = 'System')
   select * from orders

Not having an order by on the users could cause some unexpected behavior.  The order is never guaranteed this way and you could get a nasty surprise if the clustered index gets changed (not that this is the only way there could be issues).
0
 

Author Closing Comment

by:rgb192
ID: 35191930
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now