您应该在存储过程中添加代码以检查变量的长度,然后再将它们的值插入表中,必要时引发错误。- 见下面的例子。
代码片段
--This batch will fail with the SQL Server error message
DECLARE @MyTable TABLE (MyID INT IDENTITY(1, 1), MyValue VARCHAR(10))
DECLARE @MyParameter VARCHAR(100)
--Create a string of 52 chars in length
SET @MyParameter = REPLICATE('Z', 52)
INSERT INTO @MyTable(MyValue)
VALUES (@MyParameter)
去
--This batch will fail with a custom error message
DECLARE @MyTable TABLE (MyID INT IDENTITY(1, 1), MyValue VARCHAR(10))
DECLARE @MyParameter VARCHAR(100)
--Create a string of 52 chars in length
SET @MyParameter = REPLICATE('Z', 52)
IF LEN(@MyParameter) > 10
BEGIN
RAISERROR('You attempted to insert too many characters into MyTable.MyValue.', 16, 1)
RETURN
END
ELSE
BEGIN
INSERT INTO @MyTable(MyValue)
VALUES (@MyParameter)
END
GO