When executing the following code in SQL Server SQLExpress 15.0.2110 I get an error telling me that the insert fails due to SQL Server’s inablity to cast my NChar string into an int, where the problem is actually I’m trying to insert an integer into a nvarchar column. Is this fixed in a later version of SQL Server?
GO
IF OBJECT_ID('odd_table') IS NOT NULL
DROP TABLE odd_table
GO
IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.DOMAINS isd WHERE isd.Domain_Name = N'odd_table_type')
drop TYPE odd_table_type
GO
CREATE TABLE odd_table (
[an_nvarchar_column] [nvarchar](128) NULL
)
GO
CREATE TYPE odd_table_type AS TABLE (
[an_nvarchar_column] [nvarchar](128) NULL
)
GO
INSERT INTO odd_table (an_nvarchar_column) VALUES
(N'Abracadabra'),
(3)
;
-- Gives the error
/*
Msg 245, Level 16, State 1, Line 20
Conversion failed when converting the nvarchar value 'Abracadabra' to data type int.
*/
DECLARE @odd_table_var odd_table_type
INSERT INTO @odd_table_var (an_nvarchar_column) VALUES
(N'Abracadabra'),
(3)
;
-- Gives the error
/*
Msg 245, Level 16, State 1, Line 34
Conversion failed when converting the nvarchar value 'Abracadabra' to data type int.
*/
I have also tried this inserting into a view and within a stored procedure, with the same result.
3