Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 28, 2012 08:38 AM by Chen Yu - MSFT
Member
253 Points
367 Posts
May 26, 2012 11:49 AM|LINK
tablename1(fd1,fd2,fd3,fd4)
tablename2(fd1,fd2,fd3,fd4)
tablename3(fd1,fd2,fd3,fd4)
here i want to insert values into the tablename1 from tablename2 and tablename3.
tablename1(
fd1=fd1 of tablename2,
fd2=fd1 of tablename3,
fd3=fd2 of tablename2 * fd3 of tablename3,
fd4=fd4 of tablename3 + fd3 of tablename1)
i think i want to use sub query, how can i use?.
give me some sample query
188 Points
59 Posts
May 26, 2012 12:22 PM|LINK
Please add this queries
insert into tablename1
select * from tablename2
union all
select * from tablename3
196 Points
126 Posts
May 26, 2012 12:23 PM|LINK
USE MERGE STATEMENT TO COPY THE FIELDS FROM ONE TABLE TO ANOTHER TAble......
merge table on table 2
using table
when matched then
update(statement)// here if there is any existng data in table 2 is matched then ill update or move to insert
when notmatched then
insert(statement) // here u can insert the datas from one table to another table
Contributor
2522 Points
376 Posts
May 26, 2012 04:11 PM|LINK
The relationship of the three tables is done through which columns? I think you better post a sample of data from tables and the expected result.
All-Star
21600 Points
2493 Posts
Microsoft
May 28, 2012 08:38 AM|LINK
baskerganesan tablename1(fd1,fd2,fd3,fd4) tablename2(fd1,fd2,fd3,fd4) tablename3(fd1,fd2,fd3,fd4) here i want to insert values into the tablename1 from tablename2 and tablename3. tablename1( fd1=fd1 of tablename2, fd2=fd1 of tablename3, fd3=fd2 of tablename2 * fd3 of tablename3, fd4=fd4 of tablename3 + fd3 of tablename1) i think i want to use sub query, how can i use?. give me some sample query
Hi,
Did the tablename2 and tablename3 have any relationship? If not, you could use CROSS JOIN to join these two table, please check below code:
declare @tablename1 table (fd1 varchar(10),fd2 varchar(10),fd3 int,fd4 int) declare @tablename2 table (fd1 varchar(10),fd2 int,fd3 int,fd4 int) declare @tablename3 table (fd1 varchar(10),fd2 int,fd3 int,fd4 int) insert into @tablename2 select 'value=1',1,1,1 insert into @tablename3 select 'value=2',2,2,2 insert into @tablename2 select 'value=3',3,3,3 insert into @tablename3 select 'value=4',4,4,4 ;with cte (t2_fd1, t3_fd1, t2_fd2t3_fd3,t3_fd4t2_fd2t3_fd3) as ( select t2.fd1,t3.fd1,(t2.fd2*t3.fd3),(t3.fd4+t2.fd2*t3.fd3) from @tablename2 t2 cross join @tablename3 t3) insert into @tablename1 select * from cte select * from @tablename1
If so, I suppose your tables have same id values, please check below query.
declare @tablename1 table (id int,fd1 varchar(10),fd2 varchar(10),fd3 int,fd4 int) declare @tablename2 table (id int,fd1 varchar(10),fd2 int,fd3 int,fd4 int) declare @tablename3 table (id int,fd1 varchar(10),fd2 int,fd3 int,fd4 int) insert into @tablename2 select 1,'value=1',1,1,1 insert into @tablename3 select 1,'value=2',2,2,2 insert into @tablename2 select 2,'value=3',3,3,3 insert into @tablename3 select 2,'value=4',4,4,4 ;with cte (id,t2_fd1, t3_fd1, t2_fd2t3_fd3,t3_fd4t2_fd2t3_fd3) as ( select t2.id,t2.fd1,t3.fd1,(t2.fd2*t3.fd3),(t3.fd4+t2.fd2*t3.fd3) from @tablename2 t2 inner join @tablename3 t3 on t2.id= t3.id) insert into @tablename1 select * from cte select * from @tablename1
Thanks.
baskerganesa...
Member
253 Points
367 Posts
sql queries
May 26, 2012 11:49 AM|LINK
tablename1(fd1,fd2,fd3,fd4)
tablename2(fd1,fd2,fd3,fd4)
tablename3(fd1,fd2,fd3,fd4)
here i want to insert values into the tablename1 from tablename2 and tablename3.
tablename1(
fd1=fd1 of tablename2,
fd2=fd1 of tablename3,
fd3=fd2 of tablename2 * fd3 of tablename3,
fd4=fd4 of tablename3 + fd3 of tablename1)
i think i want to use sub query, how can i use?.
give me some sample query
sharanamma
Member
188 Points
59 Posts
Re: sql queries
May 26, 2012 12:22 PM|LINK
Please add this queries
insert into tablename1
select * from tablename2
union all
select * from tablename3
THINESH
Member
196 Points
126 Posts
Re: sql queries
May 26, 2012 12:23 PM|LINK
USE MERGE STATEMENT TO COPY THE FIELDS FROM ONE TABLE TO ANOTHER TAble......
merge table on table 2
using table
when matched then
update(statement)// here if there is any existng data in table 2 is matched then ill update or move to insert
when notmatched then
insert(statement) // here u can insert the datas from one table to another table
spapim
Contributor
2522 Points
376 Posts
Re: sql queries
May 26, 2012 04:11 PM|LINK
The relationship of the three tables is done through which columns?
I think you better post a sample of data from tables and the expected result.
www.imobiliariasemsuzano.com.br
Chen Yu - MS...
All-Star
21600 Points
2493 Posts
Microsoft
Re: sql queries
May 28, 2012 08:38 AM|LINK
Hi,
Did the tablename2 and tablename3 have any relationship? If not, you could use CROSS JOIN to join these two table, please check below code:
If so, I suppose your tables have same id values, please check below query.
Thanks.
Feedback to us
Develop and promote your apps in Windows Store