By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select @@version;
(No column name) |
---|
Microsoft SQL Server 2019 (RTM-CU16) (KB5011644) - 15.0.4223.1 (X64) Apr 11 2022 16:24:07 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor) |
CREATE TABLE dbo.CoolPeople(PersonName VARCHAR(20), PrimaryCar VARCHAR(20));
CREATE TABLE dbo.TruncateError(message nvarchar(4000));
INSERT INTO dbo.CoolPeople(PersonName, PrimaryCar)
VALUES ('Baby', '2006 Subaru Impreza WRX GD');
Msg 2628 Level 16 State 1 Line 1
String or binary data would be truncated in table 'fiddle_945a071ad1fd43c18ae64e3074459766.dbo.CoolPeople', column 'PrimaryCar'. Truncated value: '2006 Subaru Impreza '.
Msg 3621 Level 0 State 0 Line 1
The statement has been terminated.
-- Using TRY .. CATCH
BEGIN TRY
INSERT INTO dbo.CoolPeople(PersonName, PrimaryCar)
VALUES ('Baby', '2007 Subaru Impreza WRX GD');
END TRY
BEGIN CATCH
INSERT INTO dbo.TruncateError (message)
VALUES (ERROR_MESSAGE())
END CATCH;
1 rows affected
SELECT *
FROM dbo.TruncateError
message |
---|
String or binary data would be truncated in table 'fiddle_945a071ad1fd43c18ae64e3074459766.dbo.CoolPeople', column 'PrimaryCar'. Truncated value: '2007 Subaru Impreza '. |