?
Solved

SELECT 1     appears to not work

Posted on 2014-12-17
5
Medium Priority
?
106 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 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 668 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 664 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 668 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

762 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