datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to synchronize tables with correlated subquery?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to demonstrate the delete of records in a table which are not in another table.


-- SQL select into table create for test data
USE tempdb;
SELECT SalesOrderID = CONVERT(INT,SalesOrderID),
       OrderDate,
       TotalDue
INTO   SOH
FROM   AdventureWorks.Sales.SalesOrderHeader
GO
-- (31465 row(s) affected)
 
-- SQL insert - add extra records with non-matching SalesOrderID-s
-- MSSQL insert select - T-SQL top function
INSERT SOH
SELECT TOP 1000 SalesOrderID + 100000,
                OrderDate + 2,
                TotalDue + 1000
FROM   AdventureWorks.Sales.SalesOrderHeader
GO
-- (1000 row(s) affected)
 
-- SQL delete table - delete non-matching records
DELETE x
FROM   SOH AS x
WHERE  NOT EXISTS (SELECT 1
                   FROM   AdventureWorks.Sales.SalesOrderHeader y
                   WHERE  x.SalesOrderID = y.SalesOrderID)
GO
-- (1000 row(s) affected)

 
-- SQL alternate delete syntax
-- MSSQL delete table - delete non-matching records
DELETE SOH
WHERE  NOT EXISTS (SELECT *
                   FROM   AdventureWorks.Sales.SalesOrderHeader y
                   WHERE  SOH.SalesOrderID = y.SalesOrderID)
GO

 
 
-- Cleanup
DROP TABLE SOH

GO
 

------------

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.