Solved

SELECT 1     appears to not work

Posted on 2014-12-17
5
101 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 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 49

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search query matching words 20 38
sql server stored procedure parameter 10 20
Need help with a query 3 36
SQL Server Compression Decision 5 37
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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

679 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