如何插入记录存在于新记录数据但不同的ID? [英] How to insert record exist to new record data but different id ?

查看:63
本文介绍了如何插入记录存在于新记录数据但不同的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题


如何插入 记录存在于新记录数据但不同的ID?


我有表格部门如下


deaprtmentid int pk not auto increment


departmentname  nvarchar 50


branchcode int


companyname nvarchar(50)


假设我有以下记录

 

deaprtmentid departmentname branchcode companyname
4购买1 almazyed

5销售1 motayla

我需要将上面的这一行插入到表中,但具有不同的部门ID但是所有字段都存在相同的数据


,因此新记录将在插入后显示如下:

 deaprtmentid departmentname branchcode companyname 
6购买1 almazyed

查询我需要申请是


插入表部门select * from department其中departmentid = 4


但我需要插入  deprtment id为6 


我得到6,因为在我的场景中我得到最大离开+1


并且我的最高部门ID为5






解决方案

< blockquote>






插入部门

select(select max(departmentid)+ 1来自部门),出发名称,分行代码,同名人员来自部门的


其中,离开部门= 4周
$
请注明作为答案,这解决了问题。谢谢。






problem

How to insert  record exist to new record data but different id ?

I have table department as following

deaprtmentid int pk not auto increment

departmentname  nvarchar 50

branchcode int

companyname nvarchar(50)

suppose i have record as following

deaprtmentid departmentname branchcode companyname 4 purchase 1 almazyed

5 sales 1 motayla

I need to insert this row above to table but with different department id but same data

exist on all fields so that new record will be after insert as following :

deaprtmentid departmentname  branchcode companyname 
6              purchase         1         almazyed

query i need to apply is 

insert into table department select * from department where departmentid=4

but i need to insert as  deprtment id as 6 

I get 6 because in my scenario i get max departmentid +1

and in my max department id is 5

解决方案

Hi,

insert into department
select (select max(departmentid)+1 from department), departname, branchcode, compannyname
from department
where departmentid=4

Please mark as Answer as this solves the problem. Thanks.




这篇关于如何插入记录存在于新记录数据但不同的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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