Solved

SQL Append/Update columns from one table to another?

Posted on 2010-09-07
7
271 Views
Last Modified: 2012-05-10
Hi there,

I have four columns of data in one table (frequency) within the same database that I wish to append/update into another table (company). I have tried an insert to statement but that didn't work due to the statement trying to create rows of which some of the fields cannot accept a null value.

I have revised my approach by using the attached code as means of trying to match the counter columns values to the same field in my company table. This code managed to loop through (incrementally) the company table and from this I would like to know if it would be possible for me to use possibly an IF statement to populate the company table with the fields from the frequency table when they are equal to each other, as both tables have a common realtion via the counter field?

Your help is much appreciated in advance
DECLARE @counter int
SET @counter = 0
UPDATE company
SET @counter = counter = @counter + 1

Open in new window

0
Comment
Question by:databarracks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33615836
you want to update with a join?
http://www.experts-exchange.com/A_1517.html

not sure about your explanation(s) so far... a more concrete example might help ...
0
 

Author Comment

by:databarracks
ID: 33615993
Apologies for my rather confusing query. Yes I am trying to update existing rows and need the update to begin from the beginning of the row and work its way down. The below code from the link seems useful but I am not sure about the 'WHERE' clause? How would I write it to say that the 'counter' field in the frequency table equals the 'counter' field in the company table?


0
 

Author Comment

by:databarracks
ID: 33616050
To be honest all I am aiming to do is update the existing fields and rows in my company table with the values in the frequency table for the fields that are pervailent in both tables which are:

(contact_method, contact_frequency, contact_other, auto_email)
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33616059
can you please show sample data before and after the update?
0
 

Author Comment

by:databarracks
ID: 33616118
Hi,

Please find attached sample data. Kindly note that the (contact_method, contact_frequency, contact_other, auto_email) data values should be copied/appended/updated from the frequency table.
Sample-Data.xlsx
0
 

Author Comment

by:databarracks
ID: 33616195
I have found the solution from the link provided, thank you.
0
 

Author Closing Comment

by:databarracks
ID: 33616200
Helpful
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question