问题
I have a problem with sys.columns
for inline functions.
Maybe an error of SQL Server or I'm missing something?
Let's start from beginning, I have a simple table and a simple function like these:
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
I can insert a row into SimpleTable using both a direct "table insert" or indirect "function insert" like this:
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;
yes, it works..
id descr
1 DIRECT TABLE INSERT
2 VIEW INSERT
3 INLINE FUNCTION INSERT
When I try to insert rows with explicit NULLs or explicit values in column id
both SimpleTable and SimpleFunction insert, behave the same way:
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.
and
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.
So there is no difference and the column id is recognized and treated as identity in both ways.
But now, let's take a look into 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
and also sys.dm_exec_describe_first_result_set
(thanks to @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
As you can see, the identity column of SimpleTable is not marked as identity in function structure.
My question is WHY?
I'm using a DbCommandBuilder
in .NET and it produces wrong queries because of that.
I have tried to manually change it but it is no more possible since SQL server 2008.
来源:https://stackoverflow.com/questions/40149576/wrong-is-identity-in-sys-columns-for-inline-functions