将varchar转换为int时出错?

时间:2011-04-08 15:38:56

标签: sql-server-2008

Msg 245,Level 16,State 1,Line 89 转换varchar值时转换失败'创建System.DirectoryServices程序集时出错。 '到数据类型int。

此错误是由以下声明引起的:

begin try print '  - Preparing to create System.DirectoryServices assembly with UNSAFE_ACCESS permission'
    create assembly [System.DirectoryServices] from 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll'
    with permission_set = unsafe
    print '  - System.DirectoryServices assembly created'
end try
begin catch
    print 'There was an error creating the System.DirectoryServices assembly. '+Error_number()+Error_Severity()
end catch

1 个答案:

答案 0 :(得分:2)

这来自catch块中数字与字符串的串联。没有强制转换是不允许的,因为它试图将字符串转换为数字而不是反之亦然,因为你的字符串文字不是数字,这个尝试注定要失败!

您可以使用严重性为0的RAISERROR来打印带有替换值的消息,或者替代方法是使用显式强制转换来自行连接字符串。

begin catch
declare @num int = Error_number()
declare @sev int = Error_Severity()
raiserror('There was an error creating the System.DirectoryServices assembly. %d %d ',0,1, @num,@sev)
end catch