SQL查询中不存在使用?

SQL查询中不存在使用?,sql,sql-server-2008-r2,Sql,Sql Server 2008 R2,我的存储过程有问题。我试图通过使用userID&GroupName查看来查看该组是否不存在。我希望能够拥有相同的组用户,并且不同的用户可以拥有相同的组名称 ALTER PROCEDURE [dbo].[Insert_Group] @UserID uniqueidentifier ,@GroupName varchar(100) ,@OutGroupID int out AS BEGIN -- SET NOCOUNT ON added to prevent extra

我的存储过程有问题。我试图通过使用
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

我不太确定问题是什么,但看起来您需要在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)

…问题是?您的选择应该在您用来与之进行McCompare的用户ID前面有一个@。如“and Groups.userID=@userID”所示。在存储过程中有两个地方需要这样做。假设我有userA和userB,它们都可以有相同的组名,但userA不能有两个同名的组