将一个表的同一字段更新为另一表 [英] update same field of one table to another table

查看:73
本文介绍了将一个表的同一字段更新为另一表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个表,tablemain和tablesecond



在主表中,字段名rollno有20条记录.

现在我达到了tablesecond. ,表格秒也具有相同的字段名称rollno.

我需要从第一个表的卷号更新表第二的卷号.

我如何实现这一点,请帮忙!

解决方案

我无法更改约束,能否给我子查询
没有人要求您更改任何约束条件.您的设计错误,要求您对其进行更正.显然,这应该是外键候选人.

您应该修正表设计并确保主表rollno是tablesecond的外键,而不是每次都尝试更新/更正两个表中的rollno字段.
但是,如果您要将表1的滚动编号更新为表2,则首先需要在表2中获取缺少的滚动编号,然后将其插入.

 选择 Rollno  FROM 主表其中 rollno   IN (>选择 Rollno 来自 TableSecond)


无论是SP还是代码,都需要遍历这些rollno来插入和匹配两个表.


使用外键概念可以解决您的问题.确保将第一个表的rollno列用作主键.


使用AFTER UPDATE触发器/AFTRE INSERT触发器可解决此问题
有关触发器的更多详细信息,请参见 Article [ 解决方案

I cant change constraint,, can you give me sub query
No body is asking you to change any constraint. You have a wrong design and you are being asked to correct it. This clearly should be Foreign Key candidate.

Instead of trying to update/correct the rollno field in both tables everytime, you should fix the table design and make sure that main table rollno is a foreign key of tablesecond.

Still, if you want to update roll no from table 1 to table 2, then first you need to get the missing rollno in table2 and then insert them.

Select Rollno FROM mainTable Where rollno NOT IN (Select Rollno from TableSecond)


Either in SP or code, you need to loop through these rollno to insert and match the two tables.


Use Foreign Key concept u''ll be able to solve your problem. make sure the rollno column of first table is made as primary key.


use AFTER UPDATE Trigger/AFTRE INSERT Trigger for this problem
for more detail for trigger look this
Article[^]


这篇关于将一个表的同一字段更新为另一表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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