Solved

SELECT 1     appears to not work

Posted on 2014-12-17
5
98 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 47

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

821 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