sys.columns中的错误is_identity用于内联函数

时间:2016-10-20 08:45:57

标签: .net sql-server sql-server-2012

内联函数sys.columns存在问题 也许是SQL Server的错误或者我错过了什么?

让我们从头开始,我有一个简单的表和一个像这样的简单函数:

DROP TABLE [dbo].[SimpleTable]
GO

CREATE TABLE [dbo].[SimpleTable](
    [id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
    [descr] [varchar](50) NULL
    )
GO

DROP VIEW SimpleView
GO

CREATE VIEW SimpleView
AS
SELECT * FROM SimpleTable
GO

DROP FUNCTION SimpleFunction
GO

CREATE FUNCTION SimpleFunction(
    @dummy INT = NULL
)
RETURNS TABLE
RETURN
SELECT * FROM SimpleTable
GO

我可以使用直接的“表插入”或间接的“函数插入”在SimpleTable中插入一行,如下所示:

INSERT INTO SimpleTable (descr) VALUES ('DIRECT TABLE INSERT');
INSERT INTO SimpleView (descr) VALUES ('VIEW INSERT');
INSERT INTO SimpleFunction(DEFAULT) (descr) VALUES ('INLINE FUNCTION INSERT');

SELECT * FROM SimpleTable;

是的,它有效..

id  descr
1   DIRECT TABLE INSERT
2   VIEW INSERT
3   INLINE FUNCTION INSERT

当我尝试在列id中插入具有显式NULL或显式值的行时,SimpleTable和SimpleFunction都插入,行为方式相同:

INSERT INTO SimpleTable (id, descr) VALUES (3, 'DIRECT TABLE INSERT')
INSERT INTO SimpleView (id, descr) VALUES (5, 'VIEW INSERT')
INSERT INTO SimpleFunction(DEFAULT) (id, descr) VALUES (4, 'INLINE FUNCTION INSERT')

Server: Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'SimpleTable' when IDENTITY_INSERT is set to OFF.
Server: Msg 544, Level 16, State 1, Line 2
Cannot insert explicit value for identity column in table 'SimpleTable' when IDENTITY_INSERT is set to OFF.
Server: Msg 544, Level 16, State 1, Line 3
Cannot insert explicit value for identity column in table 'SimpleTable' when IDENTITY_INSERT is set to OFF.

INSERT INTO SimpleTable (id, descr) VALUES (null, 'DIRECT TABLE INSERT')
INSERT INTO SimpleView (id, descr) VALUES (NULL,'VIEW INSERT')
INSERT INTO SimpleFunction(DEFAULT) (id, descr) VALUES (null, 'INLINE FUNCTION INSERT')

Msg 339, Level 16, State 1, Line 1
DEFAULT or NULL are not allowed as explicit identity values.
Msg 339, Level 16, State 1, Line 2
DEFAULT or NULL are not allowed as explicit identity values.
Msg 339, Level 16, State 1, Line 3
DEFAULT or NULL are not allowed as explicit identity values.

因此没有区别,列id被识别并以两种方式被视为身份。

但现在,让我们来看看sys.columns

SELECT o.name o_name, o.type, o.type_desc, c.name c_name, c.is_identity
FROM sys.objects o
JOIN sys.columns c on o.object_id = c.object_id
where o.name in ( 'SimpleTable', 'SimpleFunction')
and c.name = 'id' 

o_name          type    type_desc                           c_name  is_identity
SimpleTable     U       USER_TABLE                          id      1
SimpleView      V       VIEW                                id      1
SimpleFunction  IF      SQL_INLINE_TABLE_VALUED_FUNCTION    id      0

还有sys.dm_exec_describe_first_result_set(感谢@Jeroen Mostert):

Type    name    source_table    source_column   is_identity_column  is_updateable   is_nullable is_part_of_unique_key
U       id      SimpleTable     id              1                   0               0           1
V       id      SimpleTable     id              1                   0               0           1
IF      id      SimpleTable     id              0                   1               0           1

如您所见,SimpleTable的标识列未在功能结构中标记为标识。

我的问题是为什么?

我在.NET中使用DbCommandBuilder,因此会产生错误的查询。

我曾尝试手动更改它,但自SQL Server 2008以来就不再可能了。

0 个答案:

没有答案