• Is there any function that could be used for this (check if a table in a different context has any identity field)?

    I have to check this in 1 million places, I hate to copy-paste the code ... (and of course an UDF is not possible given the sp_executesql...)

    set @sql = N' SELECT @cnt = COUNT(*) FROM @DBName.sys.identity_columns WHERE object_id = OBJECT_ID(N'' @prmDB.dbo.@prmTable'', N''U'')'

    SELECT @params = N'@DBName varchar, @cnt int OUTPUT'

    EXEC sp_executesql @sql, @params, @prmDB, @cnt = @table_has_identity OUTPUT

    IF @table_has_identity = 0