Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 根据返回值执行更新/插入语句_Sql Server - Fatal编程技术网

Sql server 根据返回值执行更新/插入语句

Sql server 根据返回值执行更新/插入语句,sql-server,Sql Server,我想知道我是否可以得到一些帮助,在SQL中,我有两个表,它们之间的关系基于人员ID,我想写一个sp,检查人员是否已经退出: 执行insert into语句,否则执行update语句 我正在检查的表的值是否为exit name:GSACPeopleBadgeRequest SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[spUpdateIDBadgeInfo] @PersonID Int,

我想知道我是否可以得到一些帮助,在SQL中,我有两个表,它们之间的关系基于人员ID,我想写一个sp,检查人员是否已经退出: 执行insert into语句,否则执行update语句

我正在检查的表的值是否为exit name:GSACPeopleBadgeRequest

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spUpdateIDBadgeInfo] 
     @PersonID Int,
     @RequestedBy Int,
     @RequestedOn SmallDatetime,
     @BadgeStatusType Int,
     @ShippedLocationID Int,
     @Notes Varchar(500)= NULL,
     @Active bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from

    SET NOCOUNT ON;

DECLARE @retVal INT
SET @retVal = 0

DECLARE @CheckVal Bit
SET @CheckVal = 0


 IF @PersonID > 0
      BEGIN
        '#####################################How do this right.###########################
        @CheckVal  = select * from GSACPeopleBadgeRequest   where PersonID = @PersonID


    -- Check if there is any personID in GSACPeopleRequest, if not make a new insert
else
            Update  dbo.GSACPeopleBadgeRequest  
                 SET
                 RequestedBy = @RequestedBy,
                 RequestedOn = @RequestedOn,
                 BadgeStatusType = @BadgeStatusType,
                 ShippedLocationID = @ShippedLocationID ,
                 Notes = ISNULL(@Notes,@Notes),
                 Active = @Active
           WHERE PersonID = @PersonID

            -- Set the return value 
           SET @retVal = CAST(@@ROWCOUNT As int)

Select @retVal

END

END
GO

我认为当@personid未找到时,您需要进行插入。如果存在,则更新,但在您的问题中,会以相反的方式进行解释。试试这个

IF NOT EXISTS (SELECT 1
               FROM   GSACPeopleBadgeRequest
               WHERE  PersonID = @PersonID)
  INSERT INTO GSACPeopleBadgeRequest
              (RequestedBy,RequestedOn,BadgeStatusType,ShippedLocationID,Notes,Active)
  SELECT @RequestedBy,
         @RequestedOn,
         @BadgeStatusType,
         @ShippedLocationID,
         Isnull(@Notes, ''),
         @Active
ELSE
  UPDATE dbo.GSACPeopleBadgeRequest
  SET    RequestedBy = @RequestedBy,
         RequestedOn = @RequestedOn,
         BadgeStatusType = @BadgeStatusType,
         ShippedLocationID = @ShippedLocationID,
         Notes = Isnull(@Notes, ''),
         Active = @Active
  WHERE  PersonID = @PersonID

-- Set the return value 
SET @retVal = Cast(@@ROWCOUNT AS INT)