Adding a different string to a table fails

31 views Asked by At

When creating a Table type with a single string column and trying to INSERT 2 differents strings make it fails.

First, I created a new Table Type with a single string column:

CREATE TYPE [dbo].[String_Table] AS TABLE(
    [Value] [nvarchar](255) NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [Value] ASC
)WITH (IGNORE_DUP_KEY = OFF)

Second, I try to add to a variable defined using this new table type 2 different string value:

DECLARE @p1 dbo.String_Table
INSERT INTO @p1 VALUES(N'')
INSERT INTO @p1 VALUES(N'')

On the second INSERT the following error is returned by SQL:

Msg 2627, Level 14, State 1, Line 3
Violation of PRIMARY KEY constraint 'PK__#A6AA779__07D9BBC3B1FF3702'. Cannot insert duplicate key in object 'dbo.@p1'. The duplicate key value is ().

Please advise why this is happening because the strings are differents. The collation for the database is SQL_Latin1_General_CP1_CI_AS

*** NOT A DUPLICATE *** In my opinion, my question is different from the other question because when you insert different strings you do not expect a primary key violation constraint exception. The underlying cause may be similar, but the way it occurs is different which would help someone else searching for this. My question provides additional insight to the collation issue and the reason why it occurs plus it provides a better understanding of why it occurs.

0

There are 0 answers