473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Insert Into..Select Problem

I have this stored proc that is to look for changes only between 2
tables and inserts the changes into one of my temp tables that I later
delete in my DTS flow.

I am running into a problem I think becasue in my query I am joining on
fields that may not exist yet in one of the tables.

Is there a way to compare the 2 tables in the query without joining
them? Here is my current query:

CREATE PROCEDURE [DBO].[ChangesOnly] AS

INSERT INTO ResultSet ( CustomerNumber, CustomerName, AddressLine1,
AddressLine2, AddressLine3,
City, State, Zipcode, Division )
SELECT A.CustomerNumber, A.CustomerName, A.AddressLine1,
A.AddressLine2, A.AddressLine3, A.city, A.State,A.Zipcode, A.Division
FROM MyClone As A INNER JOIN tCustomers As B ON a.CustomerNumber =
B.CustomerNumber AND
A.customerName = B.CustomerName And A.Division = B.Division
WHERE
A.AddressLine1 <> B.AddressLine1 OR
A.AddressLine2 <> B.AddressLine2 OR
A.AddressLine3 <> B.AddressLine3 OR
A.city <> B.city OR
A.State <> B.State OR
A.Zipcode <> B.Zipcode OR
A.Division <> B.Division;
GO


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
1 1470
Steve Bishop (st****@viper.com) writes:
I have this stored proc that is to look for changes only between 2
tables and inserts the changes into one of my temp tables that I later
delete in my DTS flow.

I am running into a problem I think becasue in my query I am joining on
fields that may not exist yet in one of the tables.

Is there a way to compare the 2 tables in the query without joining
them? Here is my current query:
Maybe there is, but for all such questions, it helps a lot if you
post:

o The CREATE TABLE statements for your tables (with the columns they
have
o INSERT statements with sample data.
o The output given the sample.

It goes without saying that it there is no column on which we can
match the tables to each other the comparison becomes somewhat difficult.
You can't compare if you don't know what to compare.
CREATE PROCEDURE [DBO].[ChangesOnly] AS

INSERT INTO ResultSet ( CustomerNumber, CustomerName, AddressLine1,
AddressLine2, AddressLine3,
City, State, Zipcode, Division )
SELECT A.CustomerNumber, A.CustomerName, A.AddressLine1,
A.AddressLine2, A.AddressLine3, A.city, A.State,A.Zipcode, A.Division
FROM MyClone As A INNER JOIN tCustomers As B ON a.CustomerNumber =
B.CustomerNumber AND
A.customerName = B.CustomerName And A.Division = B.Division
WHERE
A.AddressLine1 <> B.AddressLine1 OR
A.AddressLine2 <> B.AddressLine2 OR
A.AddressLine3 <> B.AddressLine3 OR
A.city <> B.city OR
A.State <> B.State OR
A.Zipcode <> B.Zipcode OR
A.Division <> B.Division;
GO


Note that this query only works if:

o All rows are in both tables.
o All columns in the WHERE clause are non-NULL.

Often when you need to compare tables, you may have differnt rows in
the tables, so you should use a FULL OUTER JOIN instead. As a consequence
of this, you should change the SELECT list to:

coalesce(A.CustomerNumber, B.CustomNumber) etc

And the WHERE clauses to:

(A.AddressLine1 <> B.AddressLine1 OR
A.AdressLine1 IS NULL AND B.AdderssLine1 IS NOT NULL OR
A.AdressLine1 IS NOT NULL AND B.AdderssLine1 IS NULL) OR
etc

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ImraneA | last post by:
Hi there Application : Access v2K/SQL 2K Jest : Using sproc to append records into SQL table Jest sproc : 1.Can have more than 1 record - so using ';' to separate each line from each other.
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
0
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on a single INSERT to a view. Either I get a 'too...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
12
by: Martin_Hurst | last post by:
Has some one come up with a similar type script that could be used in a Postgresql database? The script below was created for a SQLServer database. Thx, -Martin ...
2
by: Bill | last post by:
I'm having what seems to me to be an odd problem. Perhaps there is some explanation, but don't know at this point. Basically I have a form that tracks memberships and donations. The main form...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
9
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader>...
2
by: leedo | last post by:
Hi, I am almost going crazy with this. I have a table that I bulk insert into from another database using VS2005. I need to change the data in the records before committing the values in the...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.