Sql server 在SQL Server中将表中的数据插入到另一个表中

Sql server 在SQL Server中将表中的数据插入到另一个表中,sql-server,insert,Sql Server,Insert,我尝试将数据从一个表插入到另一个表中 表1(我从中选择数据) 更新 divid divname -------------- 1 abc 2 def 4 xyz 5 fgh 6 ekg 8 sdf 表2(从中插入数据的表) 更新: 我想要这样的数据 divdw_id divid 001 1 002 2 003

我尝试将数据从一个表插入到另一个表中

表1(我从中选择数据)

更新

   divid  divname
    --------------
    1      abc
    2      def
    4      xyz
    5       fgh
    6        ekg
    8        sdf
表2(从中插入数据的表)

更新: 我想要这样的数据

divdw_id    divid
001          1
002          2
003          4
004          5
005          6
006          8 
(No column name)    eindex
000                    0
022                   22
024                   24
025                   25
027                   27
028                   28
(No column name)    eindex
000                    0
001                   22
002                  24
003                  25
004                   27
005                   28
我尝试此查询以插入数据,但显示错误

insert into table2 
values (001, Divid)

select DivId 
from Oper_Db.dbo.table1
但这显示了一个错误

列名“Divid”无效

那么如何解决这个错误呢

更新:

当我只运行select语句查询时

insert into DivisionMap (divBI_Id, DiviOp_id)
 select RIGHT('000'+CAST(eindex as VARCHAR(3)),3),eindex from mydatabase.dbo.employee
那么这个就是这样的

divdw_id    divid
001          1
002          2
003          4
004          5
005          6
006          8 
(No column name)    eindex
000                    0
022                   22
024                   24
025                   25
027                   27
028                   28
(No column name)    eindex
000                    0
001                   22
002                  24
003                  25
004                   27
005                   28
像这样我想去哪里

divdw_id    divid
001          1
002          2
003          4
004          5
005          6
006          8 
(No column name)    eindex
000                    0
022                   22
024                   24
025                   25
027                   27
028                   28
(No column name)    eindex
000                    0
001                   22
002                  24
003                  25
004                   27
005                   28


ps算命先生今天在度假,所以我们将猜测你自己的愿望

你做错了。如果希望第一列硬编码为001,第二列硬编码为Table1.Divid,则一次只能为值指定一行。请尝试以下操作:

insert into table2 (divdw_id,Divid)
 select '001',DivId from Oper_Db.dbo.table1
或者,如果001是序列号,则尝试此操作

insert into table2 (divdw_id,Divid)
 select ROW_NUMBER() OVER(ORDER BY DivId),DivId from Oper_Db.dbo.table1
如果需要在divdw_id前面加0,请尝试以下操作

insert into table2 (divdw_id,Divid)
 select RIGHT('000'+CAST(ROW_NUMBER() OVER(ORDER BY DivId) AS VARCHAR(10)),3),DivId from Oper_Db.dbo.table1
如果要从0开始序列,请使用以下命令

insert into table2 (divdw_id,Divid)
 select RIGHT('000'+CAST(ROW_NUMBER() OVER(ORDER BY DivId)-1 AS VARCHAR(10)),3),DivId from Oper_Db.dbo.table1

错误消息
列名'Divid'无效。
清楚地说明了问题:您的列名是
Divid
,但您的'e'在查询中选择了
Divid
,这是错误的,,当我执行此操作时,针对001插入5个值,因为我只需要1个值,即001 1,当我使用002执行时,然后是002 2,您能否根据您的确切要求更新问题@seesharp@seesharp我已经更新了为什么会有divid的选择语句,然后你用cast写了一个。。。因为如果divid系列是1,2,3,5,6,那么divdw_id应该是1,2,3,4,5,而不是1,2,3,5,6。。。然而,当我运行您的查询时,这是像这样插入divdw_id(001002003005006)divid(001002003005006),其中我想要的divdwId(001002003004005)divid(001002003005006)divdw_id将是按divid生成的序列号,它没有任何其他与divid相关的内容。这是错误的,,当我执行此操作时,针对001插入5个值,其中我只需要1个值,即001 1,当我使用002执行时,则插入002 2