Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / November 2007

Tip: Looking for answers? Try searching our database.

SELECT then DELETE versus extra clause in SELECT

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
metaperl - 29 Nov 2007 15:12 GMT
Far below (in section "original 3 steps"), you see the following:
1. a temp table is created
2. some data is inserted into this table
3. some of the inserted data is removed based on a join with the same
table that the original select was made from

In my opinion, there is no way that the join could produce more rows
than were originally retrieved from viewD. Hence,  we could get rid of
the DELETE step by simply changing the query to be:

INSERT INTO #details ( rec_id, orig_corr, bene_corr )
SELECT rec_id, 0, 0
FROM   viewD
WHERE  SOURCE_SYS NOT IN ( 'G', 'K' )
AND    MONTH( VALUE_DATE_A8 ) = MONTH( @date )
AND    YEAR( VALUE_DATE_A8 )  = YEAR( @date )
AND  INMESS NOT LIKE '2__'    ---- the added line

===== original 3 steps (mentioned above) =====

CREATE TABLE #details (
   rec_id  UNIQUEIDENTIFIER PRIMARY KEY NOT NULL,
   orig  VARCHAR(35) NULL,
   bene  VARCHAR(35) NULL,
   orig_corr   TINYINT     NULL,
   bene_corr   TINYINT     NULL
)

INSERT INTO #details ( rec_id, orig_corr, bene_corr )
SELECT rec_id, 0, 0
FROM   viewD
WHERE  SOURCE_SYS NOT IN ( 'G', 'K' )
AND    MONTH( VALUE_DATE_A8 ) = MONTH( @date )
AND    YEAR( VALUE_DATE_A8 )  = YEAR( @date )

DELETE d
FROM   #details d
JOIN   viewD v ON ( d.rec_id = v.rec_id )
WHERE  INMESS  LIKE '2__'
Erland Sommarskog - 29 Nov 2007 22:03 GMT
> Far below (in section "original 3 steps"), you see the following:
> 1. a temp table is created
[quoted text clipped - 13 lines]
> AND    YEAR( VALUE_DATE_A8 )  = YEAR( @date )
> AND  INMESS NOT LIKE '2__'    ---- the added line

Almost. If INMESS can be NULL your INSERT will not include those rows,
while the original scheme would retain them.


Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.