用户定义的表类型插入有时会导致转换错误
我有一个名为 tvpInsertedColumns 的用户定义表类型:
CREATE TYPE [Audit].[tvpInsertedColumns] AS TABLE(
[ColumnName] [varchar](max) NOT NULL,
[NewValue] [varchar](max) NULL
)
在存储过程中,我尝试执行此操作(@Name 和 @Phone 都是 VARCHAR):
DECLARE @AuditColumns Audit.tvpInsertedColumns
INSERT INTO @AuditColumns (ColumnName,NewValue)
SELECT 'Name',@Name UNION ALL
SELECT 'Phone',@Phone
此操作失败,并显示错误:
转换时失败 varchar 值“某个名称” 数据类型 int。
但是,在另一个存储过程中,我正在执行此操作(@AddressLine1 和 @AddressLine1 是 VARCHAR):
DECLARE @AuditColumns AS Audit.tvpInsertedColumns
INSERT INTO @AuditColumns (ColumnName,NewValue)
SELECT 'AddressLine1',@AddressLine1 UNION ALL
SELECT 'AddressLine2',@AddressLine2
并且一切正常。
这两个存储过程都只是执行简单的插入,然后尝试将该类型与另一个以 UDT 作为参数的存储过程一起使用。
这是我第一次真正体验 UDT,所以我希望我只是错过了一些明显的东西,但这对我来说毫无意义。如果您需要更多信息,请告诉我。
I have a User-Defined Table Type called tvpInsertedColumns:
CREATE TYPE [Audit].[tvpInsertedColumns] AS TABLE(
[ColumnName] [varchar](max) NOT NULL,
[NewValue] [varchar](max) NULL
)
In a Stored Procedure I am attempting to do this (Both @Name and @Phone are VARCHARs):
DECLARE @AuditColumns Audit.tvpInsertedColumns
INSERT INTO @AuditColumns (ColumnName,NewValue)
SELECT 'Name',@Name UNION ALL
SELECT 'Phone',@Phone
This fails with the error:
Conversion failed when converting the
varchar value 'Some Name'
to data type int.
However, in another Stored Procedure I am doing this (@AddressLine1 and @AddressLine1 are VARCHARs):
DECLARE @AuditColumns AS Audit.tvpInsertedColumns
INSERT INTO @AuditColumns (ColumnName,NewValue)
SELECT 'AddressLine1',@AddressLine1 UNION ALL
SELECT 'AddressLine2',@AddressLine2
And everything works just fine.
Both Stored Procedures are just doing a simple insert and then trying to use the type along with another stored procedure that takes the UDT as a parameter.
This is my first real experience with UDTs, so I hope I'm just missing something obvious, but this makes no sense to me. Let me know if you need further information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 UDT 不太了解,但我认为正在发生的是,
在某一时刻,
@name
或@phone
值的类型为整数。尝试将 @Name 和 @Phone 转换为 varchar
I don't know much about UDTs but what I think is happening is that,
at one point, either
@name
or@phone
values are of type integer.Try to cast @Name and @Phone to varchar