简体   繁体   English

如何创建存储过程来更新员工表?

[英]How to create a stored procedure to update Employee table?

I am trying to update the salary on an employee table that I have created.我正在尝试更新我创建的员工表上的工资。 My procedure has to accept two values one for the emp_id and the other for the new salary ( emp_salary ).我的程序必须接受两个值,一个用于emp_id ,另一个用于新工资 ( emp_salary )。 Below is my employee table code:以下是我的员工表代码:

create table emp 
(
     emp_id  int, 
     emp_name varchar(30), 
     salary numeric(10, 2), 
     dept_code char(4)
)

insert into emp 
values (1, 'Jones', 11111.00, 'SALE'), (2, 'Smith', 22222.00, 'SALE'),
       (3, 'Potter', 33333.00, 'TECH'), (4, 'Clinton', 44444.00, 'MNGT')

Below is my stored procedure code:下面是我的存储过程代码:

create procedure usp_update_Imani
   (@emp_id int, @salary int)
as
begin
    update emp 
    set salary = salary * 1.1
    where emp_id = 1, 2, 3, 4;
End;

Easy, just reference the parameters of the stored procedure in your update statement:很简单,只需在update语句中引用存储过程的参数:

create procedure usp_update_Imani(@emp_id int, @salary int)
as
begin
    update emp set salary = @salary where emp_id = @emp_id;
end;

Compile it, and test it as follows:编译它,并按如下方式测试它:

exec usp_update_Imani(1, 121212)

I would assume you want to pass in the new salary since you have a parameter for the value.我假设您想传入新的薪水,因为您有该值的参数。 Also, you are passing in the emp_id so not sure why you are trying to update all of them (the syntax you have there would fail anyway).此外,您正在传入 emp_id,因此不确定为什么要尝试更新所有这些(无论如何,您在那里的语法都会失败)。 Here is how you would create a procedure to update the salary for a single emp_id to a new value.下面是如何创建一个过程来将单个 emp_id 的工资更新为新值。

create procedure usp_update_Imani
(
    @emp_id int
    , @salary int
) AS
    set nocount on;

    UPDATE emp set salary = @salary
    where emp_id = @emp_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM