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 2008 Forums
 Transact-SQL (2008)
 strange query

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2012-01-05 : 01:42:47
i have 2 tables

Products:ProductCode
Sales:ItemCode

DELETE Products
WHERE NOT EXISTS
(SELECT ProductCode FROM Sales
WHERE ItemCode=Products.ProductCode)

How is it run, ProductCode is non colum in Sales table?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-05 : 01:46:08
[code]ItemCode=Products.ProductCode[/code]

It is matching ProductCode from Products table with ItemCode from Sales table


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2012-01-05 : 02:02:45
i do not understand,
ProductCode it is not belonging to sales table.
(SELECT ProductCode FROM Sales)--> it isn't TRUE??
and the match it come after the SELECT
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-05 : 02:11:55
* nothing here, move on *
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-05 : 02:14:57
[code]
(SELECT ProductCode FROM Sales
WHERE ItemCode=Products.ProductCode)
[/code]

You are selecting the ProductCode from Products table in this query. It is the same as the ProductCode in the WHERE statement. If you removed the table prefix Products it will still work.

But for good programming practice, it is better to prefix all your column.

Also for exists, it does not matter the column list in the SELECT does not really matter, a "*" will work.
[code]
(SELECT * FROM Sales
WHERE ItemCode=Products.ProductCode)
[/code]



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2012-01-05 : 02:28:37
i understand that the Products.ProductCode it is the same column (ItemCode)

what is Operation orders of query (in the back)?
is'nt engine look for fist the ProductCode in sales table ?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-01-05 : 02:30:43
if the ProductCode column only exists in one of the table, it will not give error. If not, you will have to prefix with the table name


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-01-06 : 01:33:01
This is correllated subqueries .To undestand this Please go through this

http://msdn.microsoft.com/en-us/library/ms187638.aspx
Go to Top of Page
   

- Advertisement -