如何将ID从表A复制到表B.

时间:2016-07-31 07:03:24

标签: sql sql-server tsql

这是SQL代码:

Declare @A_ID  AS A_ID

insert into TBL_FO VALUES(NEWID(), @A_ID, '30000.00','1','1')
(select * from TBL_DETAIL where A_ID = '59366409-2EB6-49BC-A88F-801692B735D6')
  1. 我想跟踪或将TBL_DETAIL中的A_ID复制为'59366409-2EB6-49BC-A88F-801692B735D6'@A_ID的TBL_FO。我如何声明A_ID以复制相同的ID

2 个答案:

答案 0 :(得分:1)

试试这个:

Declare @A_ID  as varchar(100) -- adjust the length as needed
select @A_ID = A_ID from TBL_DETAIL where A_ID = '59366409-2EB6-49BC-A88F-801692B735D6'

Declare @FO_ID as int -- To insert this into One or more tables
SET @FO_ID = NEWID() -- Initialize it one time.

insert into TBL_FO VALUES(@FO_ID, @A_ID, '30000.00','1','1') -- First use of @FO_ID

insert into TBL_SOMEOTHERTBL VALUES( @FO_ID, .... )  -- Second use of @FO_ID

etc ...

答案 1 :(得分:1)

指定要设置的列,然后使用除了A_ID之外的具有所需值的子查询(按列名称的顺序):

insert 
into 
   TBL_FO (FirstColumnName, 
           SecondColumnName, 
           ThirdColumnName, 
           FourthColumnName, 
           FifthColumnName) 
(select 
   NEWID(), 
   A_ID, 
   '30000.00', 
   '1', 
   '1' 
 from 
   TBL_DETAIL 
 where 
   A_ID = '59366409-2EB6-49BC-A88F-801692B735D6')

注意:如果表格列与插入表格的顺序相同,也可以选择不指定列名称