0

我的存储过程有问题。我试图通过使用userID & GroupName我希望能够不能拥有相同的组用户并且可以为不同的用户拥有相同的组名

ALTER PROCEDURE [dbo].[Insert_Group]
   @UserID uniqueidentifier 
   ,@GroupName varchar(100)
   ,@OutGroupID int out 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    if not EXISTS(select Groups.id from Groups where Groups.name = @GroupName and Groups.userID = userID)
    begin 
       -- Insert statements for procedure here
       INSERT INTO [ideaPark_DB].[dbo].[Groups]([userID], [name])
       VALUES (@UserID, @GroupName)

       SELECT @OutGroupID = SCOPE_IDENTITY();
    end 
 else 
    SELECT @OutGroupID = (select Groups.id 
                          from Groups 
                          where Groups.name = @GroupName and Groups.userID = userID)
END
4

1 回答 1

0

我不太确定问题是什么,但看起来你需要在 if not exists 行上添加一个 @:

if not EXISTS(select Groups.id from Groups where Groups.name =@GroupName  and Groups.userID = @userID)

编辑:实际上看起来你在底部也缺少一个。

SELECT @OutGroupID = (select Groups.id 
                      from Groups 
                      where Groups.name = @GroupName and Groups.userID = @userID)
于 2013-06-05T21:02:37.853 回答