unqiueidenfitier与int SQL Server Procedure类型不兼容

时间:2014-06-16 17:00:16

标签: sql sql-server stored-procedures sql-server-2012-express

我有以下插入用户表的程序:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Andy Armstrong
-- Create date: 
-- Description: 
-- =============================================
CREATE PROCEDURE db_SignupAddLogin 
    -- Add the parameters for the stored procedure here
    @LoginName VARCHAR(15), 
    @LoginPassword VARCHAR(15)


AS
BEGIN
DECLARE @GUID UNIQUEIDENTIFIER
SET @GUID = NEWID();
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
     INSERT INTO tblMemberLogin
          ( 
            UserID,
            LoginName,
            LoginPassword
          ) 
     VALUES 
          ( 
            @GUID,
            @LoginName,                 
            @LoginPassword              
          ) 

    RETURN @GUID
END
GO

但是当我执行它时,我收到以下错误:

Msg 206, Level 16, State 2, Procedure db_SignupAddLogin, Line 34
Operand type clash: uniqueidentifier is incompatible with int

我无法解释为什么因为我没有在任何地方引用int。

我的tblMemberLogin架构如下所示:

用户ID(PK,唯一标识符,NOTNULL) LoginName(nchar(15),not null) LoginPassword(nchar(15),not null)

请帮忙!

3 个答案:

答案 0 :(得分:1)

RETURN只能与int一起使用。您只需使用SELECT查询即可检索变量@GUID的值。

参考:http://technet.microsoft.com/en-us/library/ms174998(v=sql.110).aspx

答案 1 :(得分:1)

摆脱RETURN @GUID,你应该好好去。

答案 2 :(得分:1)

在SQL Server中,存储过程可能只返回整数值。 SQL Server RETURN

如果要从整数以外的存储过程返回数据,可以使用输出参数:Returning Data from Stored Procedures

您声明输出参数以及输入参数:

CREATE PROCEDURE CREATE PROCEDURE db_SignupAddLogin 
    -- Add the parameters for the stored procedure here
    @LoginName VARCHAR(15), 
    @LoginPassword VARCHAR(15),
    @NewGuid UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
    SET @NewGuid = NEWID();

    -- rest of procedure
END

然后使用输出参数:

DECLARE @NewLoginGuidFromSP UNIQUEIDENTIFIER
EXECUTE db_SignupAddLogin 'Username', 'password', @NewGuid = @NewLoginGuidFromSP OUTPUT;