简体   繁体   中英

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 ). 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:

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). Here is how you would create a procedure to update the salary for a single emp_id to a new value.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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