Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
insert into tbl_1 (username,password) select username,password from tbl_2 where user_id=user_id

each table consist the user_id ,

i tried this,but its not working ? ??
Posted

If the row already exists in both the tables for the same user_id, you will not do an insert.
You will do an update.

For e.g. update tbl1 set tbl1.username = tbl2.username, tbl1.password = tbl2.password where tbl1.user_id = tbl2.user_id
 
Share this answer
 
Comments
bluesathish 13-Nov-13 2:12am    
My vote +5.
Abhinav S 13-Nov-13 2:32am    
Thank you.
Hi Walker,

Here is the query you can try
SQL
IF EXISTS
  (SELECT username,password
   FROM tb_1)
BEGIN
UPDATE tbl_1
SET tbl_1.username = tbl_2.username,
    tbl_1.password = tbl_2.password
WHERE tbl_1.user_id = tbl_2.user_id 
END
 ELSE
  BEGIN
   INSERT INTO tbl_1 (username,password)
   SELECT username,
         password
   FROM tbl_2 WHERE user_id=user_id
  END

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
 
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