Solved

so only one query will return select * from orders

Posted on 2011-03-21
6
233 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

14 Experts available now in Live!

Get 1:1 Help Now