SQLServer插入資料到有自增列的表

ywxj_001發表於2019-12-02

SQLServer插入資料到有自增列的表報錯:

僅當使用了列列表並且 IDENTITY_INSERT 為 ON 時,才能為表'Sales.dbo.Area'中的標識列指定顯式值。


正確方式:

set IDENTITY_INSERT Sales.dbo.Area ON
insert into Sales.dbo.Area(
[SysNo],
[DistrictSysNo],
[DistrictName]
)
select [SysNo],
[DistrictSysNo],

[DistrictName]

from opsdb.dbo.Area


跨伺服器,可以用linkserver:

set identity_insert  [Customer_Log]  on


insert into

  Customer..Customer_Log (

    [SysNo]

      ,[CustomerSysNo]

  )

    select 

       [SysNo]

      ,[CustomerSysNo]

    from

      [192.168.0.34].yy.dbo.Customer_Log  with(nolock)

    where

      sysno < 2013422222

  

  set identity_insert  [Customer_Log]  off


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2666500/,如需轉載,請註明出處,否則將追究法律責任。

相關文章