注释行出错

时间:2013-07-11 08:52:44

标签: sql-server ssms

您好我已经创建了一个存储过程到一个表链接到表的地址这样的地址有三个外键:

-MaterialAddressID -HomeAddressID -PostalAddressID

这是我创建的存储过程:

-- =============================================
-- Author:  *******************
-- Create date: 11.07.2013
-- Description: Gets User Personal Details
-- =============================================
    ALTER PROCEDURE [eConnect].[GetUserPersonalDetails]
        @personId int
    AS
    BEGIN
        SELECT Persons.PersonalID,
               Persons.FirstName ,
               Persons.MiddleName , 
               Persons.LastName ,
               Persons.DateOfBirth,
               Persons.CityOfBirth ,
               Persons.BSN ,
               Persons.SecondaryEmail ,
               Persons.HomePhone ,
               Persons.WorkPhone ,
               Persons.MobilePhone,
               MaterialAddress.StreetName ,
               MaterialAddress.PostCode ,
               MaterialAddress.City ,
               HomeAddress.StreetName ,
               HomeAddress.PostCode ,
               HomeAddress.City ,
               PostalAddress.StreetName ,
               PostalAddress.PostCode ,
               PostalAddress.City
        FROM Sync_Persons as Persons
        JOIN Sync_Addresses as MaterialAddress ON Persons.MaterialAddressID = MaterialAddress.AddressID
        JOIN Sync_Addresses as HomeAddress ON Persons.HomeAddressID = HomeAddress.AddressID
        JOIN Sync_Addresses as PostalAddress ON Persons.PostalAddressId = PostalAddress.AddressID
        WHERE Persons.PersonalID = @personId
    END

当我尝试执行此操作时,我收到此错误:

  
    

Msg 245,Level 16,State 1,Procedure GetUserPersonalDetails,Line 11转换varchar值'I-024800'时转换失败     数据类型int。

  

在这种情况下,第11行是: - 说明:获取用户个人详细信息

为什么我会在评论中发现错误,如何更正呢?

1 个答案:

答案 0 :(得分:1)

尝试更改此行 -

WHERE CAST(SUBSTRING(
     Persons.PersonalID, 
     CHARINDEX('-', Persons.PersonalID) + 1, 
     LEN(Persons.PersonalID)
) AS INT) = @personId

或试试这个 -

WHERE Persons.PersonalID = 'I-' + CAST(@personId AS VARCHAR(10))