Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dears,

I have a table called CustomerA, with the below fields

Id(Pk,Identity) SenderName SenderMobileNo SendToName SendToMobile

1 PersonA 438983948 PersonB 3984938949

2 PersonC 343434343 PersonD 3984938948


And another table called CustomerB, with the below fields

Id(PK, Identity) Name MobileNo


I want to put Sendername, mobile and sendToName, Mobile from CustomerA table to the name,mobileNo on the customerB table.

Help me with a query how to do it.

Thanks

Regards
Azeem
Posted
Comments
ankur789 21-Jun-13 23:47pm    
select CustomerA.sendername,CustomerA.sendermobileno,CustomerA.sendtoname,customerb.name,customerb.mobileno from CustomerA join customerB on customerA.id=customerB.id
Ahamed Azeem 22-Jun-13 0:17am    
Hi Ankur,

I want to insert the sendername, mobileno, sendtoname mobileno to the name and mobile number on the other table. actually splitting a row into 2 and insert to the other table

regards
ankur789 22-Jun-13 0:20am    
Table1
-------------
ID Name
1 A
2 B
3 C


Table2
--------
ID Name
1 Z
I need to insert data from Table1 to Table2 and I can use following sytax for the same:-

INSERT INTO Table2(Id, Name) SELECT Id, Name FROM Table1
However, In my case duplicate Ids might exist in Table2 (In my case Its Just "1") and I dont want to copy that again as that would throw an error.

I can write something like this:-

IF NOT EXISTS(SELECT 1 FROM Table2 WHERE Id=1)
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1
ELSE
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 WHERE Table1.Id<>1
Ahamed Azeem 22-Jun-13 0:27am    
I think you are confused, I have two different structure in both tables

Table1
---------
Id SenderName SenderMobile SendToName SendToMobile

1 PersonA 053433434 PersonB 0343943893
2 PersonC 343434343 PersonC 9348398893

Table2
--------
Id Name Mobile

Now I want Table2 Id column should have 1, 2, 3, 4, and name should have personA, personB, PersonC, Person D, mobile should have 4 values from the table1

1 solution

Hope this helps

CREATE TABLE Test
(
	Id INT IDENTITY, SenderName VARCHAR(30),SenderMobile INT,
	SendToName VARCHAR(30),SendToMobile INT
)
INSERT Test VALUES
('PersonA',53433423,'PersonB',34394388),
('PersonC',43434324,'PersonD',93483988)
SELECT * FROM Test

Id	SenderName	SenderMobile	SendToName	SendToMobile
1	PersonA		53433423	PersonB		34394388
2	PersonC		43434324	PersonD		93483988

SELECT IDENTITY(INT,1,1) AS Id, A.[Sender Name],A.[Sender Mobile] INTO #NewTab
FROM
(
	SELECT T.SenderName [Sender Name],T.SenderMobile [Sender Mobile] FROM Test T
	UNION ALL
	SELECT T.SendToName [Sender Name],T.SendToMobile [Sender Mobile] FROM Test T
)A

SELECT * FROM #NewTab

Id	Sender Name	Sender Mobile
1	PersonA		53433423
2	PersonC		43434324
3	PersonB		34394388
4	PersonD		93483988
 
Share this answer
 
v2
Comments
Ahamed Azeem 24-Jun-13 15:13pm    
Thanks a lot, Perfect solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900