Solved

SELECT 1     appears to not work

Posted on 2014-12-17
5
102 Views
Last Modified: 2014-12-19
I am working with a sql stored procedure that uses the WHERE NOT EXISTS clause. The stored procedure keeps getting stuck.  Below is an example of the WHERE NOT EXIST clause that is being used.
In looking at this, I am thinking that the SELECT 1 FROM BstCustomer is the problem. Wouldn't is be better to do something like "SELECT TOP 1 * from BstCustomer where BstCustomer = inId). This is a sql server stored procedure.




WHERE NOT EXISTS ( SELECT 1 FROM BstCustomer where BstCustomer = inId)
(


_
0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 40506381
Post your entire proc or the block of code that is causing the problem.
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40506414
The proc is quite large, and I would have to type it in, because I don't have copy access to the source. Anyway, I kind of pseudo coded it below. I don't know if that helps. However in the final analysis, SELECT 1 seems like a terrible command?


BEGIN INSERT INTO Table
   TableId
 SELECT
   ST.TableId
 From MsterTable
 WHERE NOT EXISTS (
  SELECT 1
  FROM
             AnotherTable t
 WHERE t.Id = TableId.id
)
0
 
LVL 50

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 167 total points
ID: 40506477
WHERE NOT EXISTS ( SELECT 1 FROM BstCustomer where BstCustomer = inId)
Usually you need to link a field in the subselect with a field in the master select. Like this we can't guess which of the fields are in the detail table and which one is in the master table (BstCustomer = inId).
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 166 total points
ID: 40506491
NOT EXISTS  is also known as "Left Anti Semi Join" and more here

It does not need to retrieve data like a conventional select does and in some respects the select clause is retained for syntax reasons (i.e. you must start with select). Any of the following may be used and are equivalent (in the context of not exists):

SELECT 1
SELECT NULL
SELECT *

NOT EXISTS is quite unlike IN () where the select clause IS more important.

I'd suggest it isn't that select clause that is your problem. You need to inspect your data to ascertain if there are any matches from:

"(SELECT TOP 1 * from BstCustomer where BstCustomer = inId)"

NOTE: you don't need TOP 1 and I would recommend you don't use it.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 167 total points
ID: 40507433
The "SELECT 1" is fine; that's not your problem.

The issue is almost certainly that columns "BstCustomer" and "inId" both exist in the "BstCustomer" table.  Therefore, a matching row always exists.  In such queries, you should always qualify all columns with a table alias:

FROM data_table dt
WHERE NOT EXISTS ( SELECT 1 FROM BstCustomer bc where bc.BstCustomer = dt.inId)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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