如何将行从一个SQL Server表复制到另一个SQL Server表 [英] How to copy a row from one SQL Server table to another

查看:83
本文介绍了如何将行从一个SQL Server表复制到另一个SQL Server表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

插入tbl_1(用户名,密码)选择用户名,密码来自tbl_2,其中user_id = user_id



每个表包含user_id,



i尝试了这个,但它没有用? ??

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 ? ??

推荐答案

如果同一个user_id的两个表中已存在该行,则不会进行插入。

您将做一个更新。



例如更新tbl1 set tbl1.username = tbl2.username,tbl1.password = tbl2.password其中tbl1.user_id = tbl2.user_id
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


嗨沃克,



以下是您可以尝试的查询

Hi Walker,

Here is the query you can try
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



希望这对您有所帮助。 />


问候,

RK


Hope this helps you a bit.

Regards,
RK


访问这里......

< br $> b $ b

如何复制SQL Server表? [ ^ ]
visit here ...


How to Copy a SQL Server Table?[^]


这篇关于如何将行从一个SQL Server表复制到另一个SQL Server表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆