How do you check if IDENTITY_INSERT is set to ON or OFF in SQL Server?

前端 未结 8 967
暗喜
暗喜 2020-12-09 00:52

I\'ve searched for this, but threads in which it appeared tended to have answers from people who didn\'t understand the question.

Take the following syntax:

<
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 01:23

    If you want to know about the session variable... Good question, but I cant see where this information would be usefull. In normal execution to check a normal table response to an insert, this should work!

    -- If you only want to know if there is identity insert on a given table:

    select is_identity
    from sys.columns
    where object_id = OBJECT_ID('MyTable', 'U') and name = 'column_Name'
    

    -- Or... Use this if you want to execute something depending on the result:

    if exists (select *
    from sys.columns
    where object_id = OBJECT_ID('MyTable', 'U') and is_identity = 1)
    ... your code considering identity insert
    else
    ... code that should not run with identity insert
    

    Have fun!

提交回复
热议问题