Solved

SELECT 1     appears to not work

Posted on 2014-12-17
5
95 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
5 Comments
 
LVL 40

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 45

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:
ScottPletcher 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how the fundamental information of how to create a table.

743 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

13 Experts available now in Live!

Get 1:1 Help Now