Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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_Sql Server_Stored Procedures - Fatal编程技术网

如何在SQL Server中使用存储过程更新表

如何在SQL Server中使用存储过程更新表,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我在SQLServer中创建了一个名为Employee的表,现在我想使用存储过程更新该表 该表包含emp_名称、emp_代码和状态列。假设该表有三条记录:首先,在存储过程中,我希望使用select语句获取最后两条记录,并且必须将已获取记录的状态设置为“Y” 我已经编写了一个存储过程,但它不会影响原始表。请建议查询存储过程 以下是我提出的问题: create procedure updtdemployee As select e.Emp_name ,e.Circle from empl

我在SQLServer中创建了一个名为Employee的表,现在我想使用存储过程更新该表

该表包含emp_名称、emp_代码和状态列。假设该表有三条记录:首先,在存储过程中,我希望使用select语句获取最后两条记录,并且必须将已获取记录的状态设置为“Y”

我已经编写了一个存储过程,但它不会影响原始表。请建议查询存储过程

以下是我提出的问题:

create  procedure updtdemployee As
  select e.Emp_name ,e.Circle 
  from employee e 
  where e.Emp_Code ='2501' or e.Emp_Code='2502'

  begin
    update employee set status='Y' where Emp_name = employee.Emp_name
  end

您不需要选择部件,只需执行更新即可

CREATE PROCEDURE updtdemployee
       @employeeID INT
  AS
    BEGIN
     UPDATE employee 
     SET status='Y' 
     WHERE Emp_Code = @employeeID
    END
如果你想做静态的,你可以用这个

CREATE PROCEDURE updtdemployee     
      AS
        BEGIN
         UPDATE employee 
         SET status='Y' 
         WHERE Emp_Code = 2501 or Emp_Code = 2502
        END

根据你的问题,如果这只会影响最后两条记录,我想获取最后两条记录

CREATE PROCEDURE updtdemployee     
      AS
        BEGIN
         UPDATE employee 
         SET status='Y' 
         WHERE Emp_Code in (select top 2 Emp_Code from employee order by Emp_Code desc)
        END
我只是在猜测您想要订购的方式,但这是您想要的吗?

*请尝试下面的代码

Create Procedure UpdateRecord (@emp_code int)
as
begin 
update employee set status= 'Y'
 where emp_code=@emp_code
end
*执行如下

exec UpdateRecode 3

3是您的emp_代码。请根据您的要求进行更改

您可以在更新中执行WHERE操作,但我需要使用emp代码从表中获取两条记录,然后我必须更改该resp记录。有可能吗?我给你放了模板,现在我为你的案例更新了我的答案,但是如果你总是有两个值,你可以添加另一个变量并使用它。例如,我说,但我想从表中获取任意两行,我必须将状态更改为“是”,确切地说,这一点就可以了,在这种情况下,始终返回最后2条记录。现在我看了一下,您可能想将其更改为类似代码创建过程UPDTDEEMPLOYEE AS BEGIN UPDATE employee SET status='Y'中的Emp_代码,其中Emp_代码按Emp_代码desc END代码从employee WHERE status'Y'中选择前2位Emp_代码,这样它就不会一直尝试更新记录它已修改?否则,如果您只想接受emp_代码来修改代码创建过程UPDTDEEMPLOYEE@empcode1 int,@empcode2 int作为BEGIN UPDATE employee SET status='Y',其中emp_代码=@empcode1或emp_代码=@empcode2 END code
-- =============================================
-- Author:      XYZ
-- Create date: xx-xx-xxxx
-- Description: Procedure for Updating  Emp Detail
-- =============================================


CREATE PROCEDURE [dbo].[SP_EmpLoyee_Update]
(
@EmpCode bigint=null,
@EmpName nvarchar(250)=null,
@MNumber nvarchar(250)=null,
@Status int=null,
@LoginUserId nvarchar(50)=null,
@Msg nvarchar(MAX)=null OUTPUT
)
AS
BEGIN TRY

UPDATE tbl_Employees
SET
EmpName=@EmpName,
MNumber=@MNumber,
Status=@Status,
ModificationDate=GETDATE()

WHERE EmpCode=@EmpCode

    SET @Msg='Employee   Updated Successfully.'

END TRY
BEGIN CATCH

    SET @Msg=ERROR_MESSAGE()

END CATCH

GO
drop procedure if exists updateBillerContactInfo1;
DELIMITER $$
CREATE PROCEDURE updateBillerContactInfo1 (INOUT account_id_list varchar(4000))
BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE first_name varchar(255) DEFAULT "";
    DECLARE last_name varchar(255) DEFAULT "";
    DECLARE db_id int(10) DEFAULT 0;

-- declare cursor
DEClARE curContact 
    CURSOR FOR 
    select contact.first_name, contact.last_name, acc.db_id from 
    ...(table and conditions)limit 100;

DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET finished = 1;

OPEN curContact;

updateContact: LOOP
    FETCH curContact INTO first_name, last_name, db_id;
    IF finished = 1 THEN 
        LEAVE updateContact;
    END IF;
    -- to update in stored procedure, use stored statement, do not use UPDATE WHERE directly, it cause issue
    SET @updateText = CONCAT('UPDATE iam_capability.t_account 
        SET first_name = ?, last_name = ? 
        WHERE db_id = ?;');
    PREPARE stmt1 FROM @updateText;
    SET @fn = first_name;
    SET @ln = last_name;
    SET @id = db_id;
    EXECUTE stmt1 USING @fn, @ln, @id;
    DEALLOCATE PREPARE stmt1;
    -- build update account id list
    SET account_id_list = CONCAT(db_id, ",",account_id_list);
END LOOP updateContact;
CLOSE curContact;

END$$
DELIMITER ;

-- use transaction to run the script 
start transaction;
SET @account_id_list = ""; 
CALL updateBillerContactInfo1(@account_id_list); 
SELECT @account_id_list;

-- check if data get updated as expected, yes run commit, no, run Rolback

commit;

ROLLBACK;