T-sql:将varchar值'61B08729-BB4F-426C-B20B-697F40F458C5'转换为数据类型int时转换失败

时间:2012-11-19 15:14:01

标签: tsql uniqueidentifier

我在另一个内部调用一个存储过程,由于某种原因SQL Server不喜欢我提供它的参数。它可能很简单,但我不确定它为什么不喜欢它。

存储过程1

USE [UserManagerTest]
GO
/****** Object:  StoredProcedure [dbo].[sp_UserManager_Log_User_Off]    Script Date: 11/19/2012 11:33:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================      
-- Author:    Nick Gowdy      
-- Create date: 13-11-2012      
-- Description:  Used by model is UserManager to filter data returned to code          
-- =============================================      
ALTER PROCEDURE [dbo].[sp_UserManager_Log_User_Off] 
                                           @userid           UNIQUEIDENTIFIER

AS 
  BEGIN 

 -- Get SessionID from UserID
 DECLARE @sessionid VARCHAR(60)
 DECLARE @userid uniqueidentifier
 select @userid = '61B08729-BB4F-426C-B20B-697F40F458C5'

 EXEC @sessionid = aspnet_Membership_Custom_GetUserSessionsByUserId @userid
 select @sessionid as sessionid

 -- Delete user session based on sessionid and userid
 EXEC aspnet_Membership_Custom_DeleteUserSession @sessionid, @userid
 END    

存储过程2

USE [UserManagerTest]
GO
/****** Object:  StoredProcedure [dbo].[aspnet_Membership_Custom_GetUserSessionsByUserId]    Script Date: 11/19/2012 12:16:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER PROCEDURE [dbo].[aspnet_Membership_Custom_GetUserSessionsByUserId]
    @UserId      uniqueidentifier
AS
BEGIN

declare @SessionId varchar(60)
select @SessionId = ( SELECT    
        [UserId]
    FROM    
        [dbo].[aspnet_Custom_UserSessionActivity]
    WHERE   
        [UserId] = @UserId)

        --select @SessionId

RETURN @SessionId
end

执行sp_usermanager_log_user_off

时出错
Msg 245, Level 16, State 1, Procedure aspnet_Membership_Custom_GetUserSessionsByUserId, Line 17
Conversion failed when converting the varchar value '61B08729-BB4F-426C-B20B-697F40F458C5' to data type int.

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:2)

我最好的猜测是,当你想要一个功能时,你定义了一个程序。

存储过程返回一个int,这是错误代码。你试图返回别的东西(一个独特的标识符,就是这样)并且它不起作用。

您也可以修改程序以提供OUTPUT参数。

MSDN文档(http://msdn.microsoft.com/en-us/library/ms187926.aspx):

  

将状态值返回给调用过程或批处理以指示   成功或失败(以及失败的原因)。

答案 1 :(得分:0)

使用long或float - 该字符串可能太大而无法转换为整数值