Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 howto make an append query

Author  Topic 

mr_bad_guy
Starting Member

7 Posts

Posted - 2008-08-06 : 09:25:32
I want to make a query to update an existing table but dont know howto.

I will write down what the situation is.

Table One
- ID
- Column 1
- Column 2

Table Two
- ID
- Column 1
- Column 2

What I want to do is the following.

Table One the column 2 is empty no data this I want to fill with the data of Table Two, column 2
The ID of table one is coresponding with Column 1 of table two.

Can someone help??

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-06 : 09:33:39
do you mean...?

update a set column2 = b.column2
from Table1 a join Table2 b on a.ID = b.column1

Em
Go to Top of Page

mr_bad_guy
Starting Member

7 Posts

Posted - 2008-08-06 : 09:41:15
hmmmm i don;t get you.. but thats maybe i am a newbie..

what i want is..

i have data in table two in column 2

this data i want to append in table 1 in column 2

the id of table 1 is the same as table 2 column 2..

I hope this will be a better description.
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-06 : 09:43:40
not really a better description actually no

try what i put. if it doesn't work, give us some sample data and an example of the result you want. that will help a lot more

Em
Go to Top of Page

mr_bad_guy
Starting Member

7 Posts

Posted - 2008-08-06 : 09:54:30
Table 1
ID Column 1 Column 2
1 hi
2 bye
3 why
4 no
5 yes


Table 2
ID Column 1 Column 2
6 1 nokia
7 2 sony
8 3 canon
9 4 philips
10 5 pioneer


Result in Table 1
ID Column 1 Column 2
1 hi nokia
2 bye sony
3 why canon
4 no philips
5 yes pioneer


This is the basis priciple about what I mean.


Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-06 : 09:55:57
and did you try the query i gave you?

Em
Go to Top of Page

mr_bad_guy
Starting Member

7 Posts

Posted - 2008-08-06 : 10:12:34
yes i am trying.. exately the same.. but he says no rows are effected
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-08-06 : 10:14:44
well from your sample data....


declare @table1 table (id int,column1 varchar(20),column2 varchar(20))
declare @table2 table (id int,column1 int,column2 varchar(20))


insert into @table1
select 1, 'hi',null
union all select 2, 'bye',null
union all select 3, 'why', null
union all select 4, 'no',null
union all select 5, 'yes', null


insert into @Table2
select 6, 1, 'nokia'
union all select 7, 2, 'sony'
union all select 8, 3, 'canon'
union all select 9, 4, 'philips'
union all select 10, 5, 'pioneer'

update a set column2 = b.column2
from @Table1 a join @Table2 b on a.ID = b.column1

select * from @table1


(5 row(s) affected)
id column1 column2
----------- -------------------- --------------------
1 hi nokia
2 bye sony
3 why canon
4 no philips
5 yes pioneer




Em
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-06 : 13:09:54
quote:
Originally posted by mr_bad_guy

yes i am trying.. exately the same.. but he says no rows are effected


post your query used then
Go to Top of Page
   

- Advertisement -