Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
See more:
tabel1

SQL
name   date       amount  phoneno   addresss chequeno bank 
   abc      10/09/2014 100000  145278    ncbdh    h2634    BSH
   bcjd     09/05/2014 52222   9822014   nhdhdd   A103    ING


table2 
name      date        amount     phoneno    address   chequeno    bank  remark    statement 
 bcjd     09/05/2014  52222      9822014    nhdhdd    A103        ING   cleared   payment done


this row is not available in table2
so this row should insert :
SQL
abc    10/09/2014 100000  145278    ncbdh    h2634    BSH

which is not available in table2

if it is already available then no change for that .
otherwise insert .
Posted
Updated 19-Sep-14 1:01am
v2

Hello ,
I don't know your table structure .
but according to your requirement you can try this code

insert into table2
select * from table1 where  table1.name not in (select name from table2)


here i insert data in table2 from table1 on the basis of name . It is better if there is any unique or primary key like Id(Auto int)

hope it helps you.

thanks
 
Share this answer
 
v2
Comments
10923679 19-Sep-14 7:00am    
well that is the problem... i dont have anything unique here...

thats why i need to compare the whole row..

or i have compare like


name =name
date = date
phone=phone

if same then no update
otherwise insert ...
Hi,


Check this...



SQL
insert into table2 t2
select * from table1 t1 
where t2.name <> t1.name 
and t2.date <> t1.date
and t2.amount <> t1.amount
and t2.chequeno <> t1.chequeno


above i am considering less possible columns which may provide uniqueness of record.

Hope this will help you.


Cheers.
 
Share this answer
 
Comments
10923679 19-Sep-14 7:08am    
okay if i want to insert selecting the columns then ???
i mean only few entry like

name amount date and cheque to table2

then?
Magic Wonder 19-Sep-14 7:33am    
Specify those columns names in insert into & Select statements.
10923679 19-Sep-14 7:38am    
thank you
Magic Wonder 19-Sep-14 7:39am    
Your welcome.
Try this

SQL
INSERT INTO Table2
SELECT *
FROM  Table1 T1
      LEFT OUTER JOIN Table1 T2 ON T1.NAME = T2.NAME
WHERE T2.Name is null
 
Share this answer
 
Comments
10923679 19-Sep-14 7:40am    
thank you
Hi,

You may use MERGE statemet for better performance. Here is sample script according to your example:

SQL
MERGE INTO Table2 AS TARGET
USING (
            SELECT Name, date, amount, chequeno
            FROM  Table1
) AS SOURCE
ON TARGET.Name = SOURCE.Name

WHEN MATCHED
    THEN UPDATE SET TARGET.date  = SOURCE.date ,
    TARGET.amount   = SOURCE.amount  ,
    TARGET.chequeno   = SOURCE.chequeno

WHEN NOT MATCHED BY TARGET THEN
INSERT (Name, date, amount, chequeno ) VALUES (SOURCE.Name, SOURCE.date, SOURCE.amount, SOURCE.chequeno)
;



Thanks,
Baliram Suryawanshi
 
Share this answer
 
SQL
DECLARE 

@IDate DATETIME,
@Count	INT;


SELECT @Count=0;
SELECT @Count=count(*) FROM table2
IF @Count>0
BEGIN

SELECT TOP 1 @IDate=date FROM table2 ORDER BY table2.date DESC
	insert into table2
	select * from table1
	WHERE date>@IDate
END
ELSE IF @Count=0
BEGIN 
	INSERT INTO table2 
	select * from table1
END
 
Share this answer
 

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