SQL syntax

in my CRM I have 3 SQL tables
customer a (key custnum), contact b (key contNum), customer_contact (custnum, contnum cross reference) connecting customers to contacts.
Historically some contact imported from a previews CRM  do not have any cross  reference.
both customer and contact table have address, city, state,zip, phone info. table c has
I am looking  SQL syntax to create cross reference records for contacts which do have same address info (address, city, state, zip as customer) and don't already have a  cross reference record in table c.
leop1212Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Chances are you may want stripped down versions of zip and phone (dashes, parentheses, spaces removed) when you compare the fields. You can also add more REPLACE functions in the Address and/or City, if you want to remove spaces, commas, periods (anything you think might skew a potential match):

INSERT INTO @Customer_Contact
SELECT  a.CustNum, b.ContNum
FROM    Customer a
JOIN    Contact  b
ON      b.Address = a.Address
AND     b.City    = a.City
AND     b.State   = a.State
AND     REPLACE(REPLACE(REPLACE(b.Zip, '-', ''), ' ', ''), '.', '') =
        REPLACE(REPLACE(REPLACE(a.Zip, '-', ''), ' ', ''), '.', '')
AND     REPLACE(REPLACE(REPLACE(REPLACE(b.Phone, '-', ''), '(', ''), ')', ''), ' ', '') =
        REPLACE(REPLACE(REPLACE(REPLACE(a.Phone, '-', ''), '(', ''), ')', ''), ' ', '') =
WHERE NOT EXISTS (SELECT 1 FROM Customer_Contact c
                  WHERE  c.CustNum = a.CustNum
                  AND    c.ContNum = b.ContNum)

Open in new window

0
leop1212Author Commented:
I am getting this error
 Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.
there are few more fields in customer_contact like
createdate, created by and rowpointer.
I assume that error message is  referring to missing data for  these fields.
what would be correct syntax add static values for adddate, createby ? Not sure what should be done with row pointer
here is my current code below


Open in new window

INSERT INTO Customer_Contact
SELECT  a.Cust_num,a.cust_seq, b.contact_id
FROM    custaddr a
JOIN    Contact  b
ON      b.company = a.name
AND     b.City    = a.City
AND     b.State   = a.State
WHERE NOT EXISTS (SELECT 1 FROM Customer_Contact c
                  WHERE  c.cust_num = a.cust_num
                  AND       c.cust_seq      = a.cust_seq
                  AND    c.contact_id = b.contact_id)
0
dsackerContract ERP Admin/ConsultantCommented:
I would think your Customer_Contact only has two fields (custnum, contnum). That's what you said in your opening post.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

leop1212Author Commented:
my bad.  how do I assign createdate = '2015-06-29'  and what to do with rowpointer?
0
dsackerContract ERP Admin/ConsultantCommented:
You should be able to leave those alone (usually a trigger will fill those in, if this is a standard ERP model). Explicitly name your fields in your INSERT statement. Assuming the following are the correct field names. If you also need the custseq, add that in there:

INSERT INTO Customer_Contact (custnum, contnum)
Which ERP are you working on? They all are patterned after a standard ERP model, and I've worked on both SAP and Syteline for years.
0
leop1212Author Commented:
syteline
0
dsackerContract ERP Admin/ConsultantCommented:
That's my expertise. You'll want to INSERT INTO customer_contact (cust_num, cust_seq, contact_id), and use your query for the three fields.

Of course you're piloting on a test database, right? :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leop1212Author Commented:
yes I am running on the test db and its getting better but now getting another error .please see below the code


Open in new window

INSERT INTO Customer_Contact(cust_num, cust_seq, contact_id)
SELECT  a.Cust_num,a.cust_seq, b.contact_id
FROM    custaddr a
JOIN    Contact  b
ON      b.company = a.name
AND     b.City    = a.City
AND     b.State   = a.State
WHERE NOT EXISTS (SELECT 1 FROM Customer_Contact c
                  WHERE  c.cust_num = a.cust_num
                  AND       c.cust_seq      = a.cust_seq
                  AND    c.contact_id = b.contact_id)





Msg 10314, Level 16, State 11, Procedure customer_contactInsert, Line 10
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65598. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:
System.IO.FileLoadException: Could not load file or assembly 'timezone, Version=7.1.0.0, Culture=neutral, PublicKeyToken=87d3698afe01ef37' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
   at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
0
dsackerContract ERP Admin/ConsultantCommented:
Did you set the database as trustworthy after you restored it?

You may have to reboot that test machine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.