473,405 Members | 2,154 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,405 software developers and data experts.

Updating one table from another

Hi you wonderful helpers who generously give your time away to help the less competent :)

I thought this would be an easy task but I'm not an advanced SQL guy and don't know how to use an inner join, which apparently I need to use.

I have two tables

tblEmp
fname (text)
lname (text)
balance (money)

beginning_balances
fname (text)
lname (text)
balance (money)

I want to update the balances from beginning_balances to tblEmp.

Here's my attempt to recreate the example I was tyring to follow which yeilded zero results although I know that there are exact fname and lname matches:

Expand|Select|Wrap|Line Numbers
  1. Update tblemp
  2. Set    tblemp.balance = beginning_balances.balance
  3. From   tblemp
  4.        Inner Join beginning_balances
  5.          On tblemp.fname = beginning_balances.fname
  6.          And tblemp.lname = beginning_balances.lname
  7. Where  tblemp.balance = 0
  8.  
Am I completely off here?
Jul 21 '07 #1
7 1990
vijaii
15
Hi you wonderful helpers who generously give your time away to help the less competent :)

I thought this would be an easy task but I'm not an advanced SQL guy and don't know how to use an inner join, which apparently I need to use.

I have two tables

tblEmp
fname (text)
lname (text)
balance (money)

beginning_balances
fname (text)
lname (text)
balance (money)

I want to update the balances from beginning_balances to tblEmp.

Here's my attempt to recreate the example I was tyring to follow which yeilded zero results although I know that there are exact fname and lname matches:

Expand|Select|Wrap|Line Numbers
  1. Update tblemp
  2. Set    tblemp.balance = beginning_balances.balance
  3. From   tblemp
  4.        Inner Join beginning_balances
  5.          On tblemp.fname = beginning_balances.fname
  6.          And tblemp.lname = beginning_balances.lname
  7. Where  tblemp.balance = 0
  8.  
Am I completely off here?
The query is correct but for what purpose you are using

Where tblemp.balance = 0

This means it will update only records which as 'balance' as '0' in 'tblemp' table.
Jul 21 '07 #2
Vidhura
99
Hi you wonderful helpers who generously give your time away to help the less competent :)

I thought this would be an easy task but I'm not an advanced SQL guy and don't know how to use an inner join, which apparently I need to use.

I have two tables

tblEmp
fname (text)
lname (text)
balance (money)

beginning_balances
fname (text)
lname (text)
balance (money)

I want to update the balances from beginning_balances to tblEmp.

Here's my attempt to recreate the example I was tyring to follow which yeilded zero results although I know that there are exact fname and lname matches:

Expand|Select|Wrap|Line Numbers
  1. Update tblemp
  2. Set    tblemp.balance = beginning_balances.balance
  3. From   tblemp
  4.        Inner Join beginning_balances
  5.          On tblemp.fname = beginning_balances.fname
  6.          And tblemp.lname = beginning_balances.lname
  7. Where  tblemp.balance = 0
  8.  
Am I completely off here?
The data types text are incompatible in the equal to operator.(
tblemp.fname = beginning_balances.fname)

The problem is a datatype of fname and lname.

Can't use varchar instead of text.? Refer the following link

Text datatype
Jul 21 '07 #3
hariharanmca
1,977 1GB
Hi you wonderful helpers who generously give your time away to help the less competent :)

I thought this would be an easy task but I'm not an advanced SQL guy and don't know how to use an inner join, which apparently I need to use.

I have two tables

tblEmp
fname (text)
lname (text)
balance (money)

beginning_balances
fname (text)
lname (text)
balance (money)

I want to update the balances from beginning_balances to tblEmp.

Here's my attempt to recreate the example I was tyring to follow which yeilded zero results although I know that there are exact fname and lname matches:

Expand|Select|Wrap|Line Numbers
  1. Update tblemp
  2. Set    tblemp.balance = beginning_balances.balance
  3. From   tblemp
  4.        Inner Join beginning_balances
  5.          On tblemp.fname = beginning_balances.fname
  6.          And tblemp.lname = beginning_balances.lname
  7. Where  tblemp.balance = 0
  8.  
Am I completely off here?

You can learn Select Statement and JOIN here
Jul 21 '07 #4
The query is correct but for what purpose you are using

Where tblemp.balance = 0

This means it will update only records which as 'balance' as '0' in 'tblemp' table.

Actually every balance in tblemp is at zero so it is unnecessary. I should have left that out. sorry.
Jul 21 '07 #5
The data types text are incompatible in the equal to operator.(
tblemp.fname = beginning_balances.fname)

The problem is a datatype of fname and lname.

Can't use varchar instead of text.? Refer the following link

Text datatype

I apologize. I double checked and they are all nvarchar. I tried to change the OP to reflect this but the edit button is missing.

Heres what I found out. I tried to even just get records to come back:

Expand|Select|Wrap|Line Numbers
  1. SELECT t.fname, t.lname, t.balance, b.fname, b.lname, b.balance
  2. FROM tblemp t
  3. INNER JOIN beginning_balances b
  4. ON t.fname = b.fname AND t.lname = b.lname
but it returned nothing so I dug deeper to find if I do a simple select statement on a name I know is in both tables like:
Expand|Select|Wrap|Line Numbers
  1. Select fname, lname from beginning_balances where lname = 'proctor' 
it brings back 1 record with:

JAMIE PROCTOR
but if I try
Expand|Select|Wrap|Line Numbers
  1. Select fname, lname from beginning_balances where fname= 'jamie' 
it doesn't come back with any records like it does on tblemp. I imported this list of names from an excel file if that helps any. Is there an easy way to check if there are hidden leading or trailing spaces and get rid of them perhaps or just account for them?
Jul 21 '07 #6
UGH! I just confirmed that all the fnames in beginning_balances have leading spaces in them! What's the quickest way to get rid of them? Change it in Excel somehow and then reimport? Create a new table and write to it while stripping out the leading spaces.... erm... somehow.
Jul 21 '07 #7
Yay, I accomplished my goal with help from someone:

Expand|Select|Wrap|Line Numbers
  1. UPDATE beginning_balances SET fname = rtrim(ltrim(fname))
and then got the balance update finished with this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblemp
  2. SET tblEmp.balance = b.balance
  3. FROM tblemp INNER JOIN 
  4.     (SELECT beginning_balances.fname, beginning_balances.lname, beginning_balances.balance FROM beginning_balances) AS b
  5. ON tblEmp.fname = b.fname AND tblEmp.lname = b.lname
Jul 21 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
3
by: Bernard André | last post by:
Hi All, context: I am using Access 97 tablkes with VB. I can see records in the MDB, using Adodc and datagrid. No problem. But when doing: rsprivate.AddNew rsprivate!For =...
6
by: Hasanain F. Esmail | last post by:
Hi all, I sincerly thank you all in advance for your help to solve this problem. I have been trying to find a solution to this problem for sometime now but have failed. I am working on a...
1
by: Chris Jackson | last post by:
I'm a novice Access user and am not sure how to solve the following problem. Any help with the following would be greatly appreciated! I have two tables with identical structures, the first holds...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
5
by: aaron.m.johnson | last post by:
I have an application which contains an Access database with linked tables that point to another database within the application. The problem I have is that when the user installs the application,...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
9
by: hrreece | last post by:
I have an Access 2002 database that has a form that can be used to review individual records. At the bottom of the form are buttons that are linked to functions that allow the user to "Find a record...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.