If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > adding values to columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-17-12, 08:23
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
adding values to columns

Hi guys,

I have a table with 6 columns. which we can call a, b, c, d, e, f
what I want to achieve is to put data in column d and e and then split this result in column f.

The data I want to put in column d and e is already exported and executed from a table called exp_data, which is from a period of november.


So this is what i have so far but is not working:

update split_table set d =
select amount from exp_data
where period = '1111'
and exp_data.account = split_table.b
and exp_data.company = split_table.a

the error I get is incorrect syntax near select any ideas?

Thank in advance.

Fixed the issue by adding a parenthesis before the select until the end, thanks anyhow

Last edited by buzmay; 01-17-12 at 08:29. Reason: fixed
Reply With Quote
  #2 (permalink)  
Old 01-17-12, 09:48
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Try this:
Code:
update U
set d = exp_data.amount
FROM split_table AS U
	INNER JOIN exp_data ON
		U.b = exp_data.account
		and U.a = exp_data.company
where period = '1111'
Can you put your code between [CODE] [/CODE ] tags? It makes things easier to read for us.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Last edited by Wim; 01-17-12 at 10:27.
Reply With Quote
  #3 (permalink)  
Old 01-17-12, 09:55
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Sorry I didnt know but I will do that from now on.

What do you mean by Update u??

Is it update the table name?
Reply With Quote
  #4 (permalink)  
Old 01-17-12, 10:23
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Yes, it's an Alias...and you NEED to do it that way to do a join update
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #5 (permalink)  
Old 01-17-12, 10:30
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
thanks

char 5
Reply With Quote
  #6 (permalink)  
Old 01-17-12, 11:07
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Quote:
What do you mean by Update u??
(I have corrected an error in my previous post. )

A basic UPDATE statement is something like this:
Code:
UPDATE TableName
SET AColumn = 'A New Value'
WHERE SomeCondition = 'True'
In your case, one first needs to do a JOIN to retrieve the new values of the column that you want to update. If we would write the JOIN with a SELECT statement, we get: (*)
Code:
SELECT exp_data.amount
FROM split_table 
	INNER JOIN exp_data ON
		split_table.b = exp_data.account
		and split_table.a = exp_data.company
where period = '1111'
When working with tables in SQL, we can give them an "alias" name. In some occasions it is done to make the table names shorter or more meaningful. Assigning alias names is optional here.
In some cases we need to JOIN a table with itself (e.g. if the table Employee would also have a column "Boss_ID", that references the Employee who is someone's boss), in that case we would have to make a distinction somehow to tell the Employee that we consider as a Boss apart from the Employee that we consider as the Minion. Here we MUST use aliases: e.g. Minion and Boss.
Code:
SELECT Minion.Employee_ID, Minion.Name, 
      Boss.Employee_ID, Boss.Name
FROM Employee AS Minion 
   INNER JOIN Employee AS Boss ON 
      Minion.Boss_ID = Boss.Employee_ID
We could rewrite the above code (*) as:
Code:
SELECT exp_data.amount
FROM split_table as U 
	INNER JOIN exp_data ON
		U.b = exp_data.account
		and U.a = exp_data.company
where period = '1111'
without changing anything of how the query performs.

We can now rewrite this as an UPDATE statement:
Code:
UPDATE U
SET U.d = exp_data.amount
FROM split_table AS U
	INNER JOIN exp_data ON
		U.b = exp_data.account
		and U.a = exp_data.company
where period = '1111'
It is best practice to use an alias for the table that has to be updated and name it "U". So it is very clear for you and all others who will ever see that code, what table will be updated and how it is JOINED with the other table(s).
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Last edited by Wim; 01-17-12 at 11:10. Reason: Sorry, didn't knew Brett had already anserwed it.
Reply With Quote
  #7 (permalink)  
Old 01-17-12, 11:33
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Wim, million thanks I appreciate this a lot
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On