Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 NOT EXISTS

Author  Topic 

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-11-08 : 14:05:39
When I run this:

Select CaseNumber from tblParadox
Where CaseNumber = '011-281226'

I get no cases, which is what I want.

When I run this:

Select CaseNumber from tblWebWhacker
Where CaseNumber = '011-281226'

And I get 1 case, is what I want.

But when I run this, I don't get anything:

Select CaseNumber from tblWebWhacker
Where Not Exists (Select CaseNumber From tblParadox)
Order by CaseNumber

Shouldn't it show the cases that are in tblWebWhacker and not in tblParadox?

Brenda

If it weren't for you guys, where would I be?

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2004-11-08 : 14:10:06
The stuff inside of the exists is wrong. Your where clause could be translated to "Where NOT anything in tblParadox".

In order for it to work you need to reference the web whacker table inside of the exists query.


Select CaseNumber from tblWebWhacker
Where Not Exists (Select * FROM tblParadox WHERE tblParadox.CaseNumber = tblWebWhacker.CaseNumber)
Order by CaseNumber


Dustin Michaels
Go to Top of Page

VIG
Yak Posting Veteran

86 Posts

Posted - 2004-11-08 : 14:13:47
Select CaseNumber
from tblWebWhacker w
Where Not Exists (Select CaseNumber From tblParadox p where p.CaseNumber=w.CaseNumber )
Order by CaseNumber

or

Select CaseNumber from tblWebWhacker
Where CaseNumber Not IN (Select CaseNumber From tblParadox)
Order by CaseNumber

or

Select CaseNumber
from tblWebWhacker w
LEFT JOIN
tblParadox p
ON w.CaseNumber=p.CaseNumber
WHERE p.CaseNumber IS NULL
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-11-08 : 14:20:24
Thanks! That is perfect!

Brenda

If it weren't for you guys, where would I be?
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-11-08 : 15:45:55
One more question. If I wanted to insert the data into a new table, how would I do it? This doesn't work:


INSERT INTO tblCurrentMonth (CaseNumber) Values (Select CaseNumber from tblWebWhacker Where not exists (Select CaseNumber From tblParadox Where tblParadox.CaseNumber = tblWebWhacker.CaseNumber) And refund > 1000


Thanks!

Brenda

If it weren't for you guys, where would I be?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-11-08 : 15:49:17
INSERT INTO tblCurrentMonth (CaseNumber)
Select CaseNumber
from tblWebWhacker
Where not exists (Select CaseNumber From tblParadox Where tblParadox.CaseNumber = tblWebWhacker.CaseNumber) And refund > 1000




Tara
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-11-08 : 17:56:50
Hi Tara

Why do you not use VALUES?

Brenda

If it weren't for you guys, where would I be?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-11-08 : 17:59:17
From BOL:

quote:


Inserting a Row Using INSERT...Values
The VALUES keyword specifies the values for one row of a table. The values are specified as a comma-separated list of scalar expressions whose data type, precision, and scale must be the same as or implicitly convertible to the corresponding column in the column list. If a column list is not specified, the values must be specified in the same sequence as the columns in the table or view.

For example, this statement inserts a new shipper into the Shippers table using the VALUES clause:

INSERT INTO Northwind.dbo.Shippers (CompanyName, Phone)
VALUES (N'Snowflake Shipping', N'(503)555-7233')

A column list is required for this insert because the ShipperID column has the IDENTITY property; therefore, values cannot be inserted into it.





quote:


Inserting Rows Using INSERT...SELECT
The SELECT subquery in the INSERT statement can be used to add values into a table from one or more other tables or views. Using a SELECT subquery also lets more than one row be inserted at one time.

This INSERT statement inserts into a separate table some of the data from all the rows in titles whose type is modern cooking:

USE pubs
INSERT INTO MyBooks
SELECT title_id, title, type
FROM titles
WHERE type = 'mod_cook'

The select list of the subquery must match the column list of the INSERT statement. If no column list is specified, the select list must match the columns in the table or view being inserted into.

Another use of the INSERT...SELECT statement is to insert data from a source outside of Microsoft® SQL Server™. The SELECT in the INSERT statement can:

Reference a remote table on a linked server by using a four-part name. For more information, Identifying a Data Source Using a Linked Server Name.


Reference a remote table using OPENROWSET. For more information, see Identifying a Data Source Using the Ad Hoc Name.


Use the result set of a query executed on a remote server. For more information, see Using Pass-through Queries as Tables.




Tara
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-11-09 : 14:43:13
Thanks Tara!

Brenda

If it weren't for you guys, where would I be?
Go to Top of Page
   

- Advertisement -