t-sql check identity_insert status – TSQL 檢查identity_insert的狀況

今天公司有一個程式有需要做一個Data Refresh的功能..
把資料從 一個Database Transfer 到另一個Database

如果你的Table/表單上有 IdentityField 的話

Table with Identity field
當你嘗試把 資料加進這個Identify Field上時 便會出現以下的錯誤信息

Cannot insert explicit value for identity column in table 'foods_tbl' when IDENTITY_INSERT is set to OFF.

所以如果你想把資料行一個Table轉到另一個 Mirror 出來的Table..
又想把舊的ID 加進MirrorTable 上時 便先要停用 Mirror 出來的Table上的Identity field 的屬性


select is_identity
from sys.columns
where object_id = OBJECT_ID(['表單名稱/Table Name']) and name = ['Field name']

Query to check if the field is the Identity field

select is_identity
from sys.columns
where object_id = OBJECT_ID('foods_tbl') and name = 'foodId'

Hope you find it useful

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.