declare @FJobID int declare @FJobIDs int if datediff(day,@FJDate,'2015-03-27')<=0 begin select @FJobID=FJobID from Hr_Job where FDepaID=@FNDepaID and FJobsID=@FNJobsID if (isnull(@FJobID,0)=0) begin insert into Hr_Job(FJobsID,FDepaID)values(@FNJobsID,@FNDepaID) set @FJobIDs=@@identity update Hr_Employee set FDepaID = @FNDepaID,FJobsID=@FNJobsID,FJobID=@FJobIDs where FEmplID = @FEmplID update Ba_Manager set FDepaID = @FNDepaID,FJobID=@FNJobsID where FEmplID = @FEmplID end else begin update Hr_Employee set FDepaID = @FNDepaID,FJobsID=@FNJobsID,FJobID=@FJobID where FEmplID = @FEmplID update Ba_Manager set FDepaID = @FNDepaID,FJobID=@FNJobsID where FEmplID = @FEmplID end end 给这段代码加注释
时间: 2023-02-06 09:50:15 浏览: 88
这段代码看起来像是在执行更新操作。
它使用了一些变量,包括 `@FJobID` 和 `@FJobIDs`,以及 `@FJDate`、`@FNDepaID` 和 `@FNJobsID`。
它首先检查 `@FJDate` 是否早于 2015 年 3 月 27 日。如果是,则执行一些操作,否则什么都不做。
在执行操作的情况下,它会尝试查询 `Hr_Job` 表中是否存在 `FDepaID` 为 `@FNDepaID`,`FJobsID` 为 `@FNJobsID` 的记录。如果存在,则设置 `@FJobID` 为该记录的 `FJobID` 值。
如果找不到这样的记录(即 `@FJobID` 为空),则会向 `Hr_Job` 表插入一条新记录,将 `FJobsID` 设置为 `@FNJobsID`,将 `FDepaID` 设置为 `@FNDepaID`。然后,它会设置 `@FJobIDs` 为插入的新记录的自增 ID 值,并使用这个值来更新 `Hr_Employee` 和 `Ba_Manager` 两个表中的记录。
如果找到了这样的记录(即 `@FJobID` 不为空),则会使用 `@FJobID` 的值来更新 `Hr_Employee` 和 `Ba_Manager` 两个表中的记录。
相关问题
create trigger tri_jieshu on reader_borrow for insert as declare @id varchar(50) select @id=borrow_id from inserted update book_borrowing set book_surplus=book_surplus-1 where book_id=@id create trigger tri_huanshu on reader_back for insert as declare @id varchar(50) select @id=back_id from inserted update book_borrowing set book_surplus=book_surplus+1 where book_id=@id create proc payment @id varchar(20),@name varchar(20) as begin select borrow_date,back_date from reader_back,reader_borrow where back_id=borrow_id and back_id=@id and borrow_bookname=back_bookname and (select datediff(day, borrow_date,back_date) FROM reader_back,reader_borrow)>7 end create view view_pay as select * from reader_back,reader_borrow where (borrow_id=back_id and borrow_bookname=back_bookname) create proc proc_pay (@id varchar(20)) as select back_id,back_bookname,back_date,borrow_date from view_pay where back_id=@id exec proc_pay @id='1'该代码有什么语法错误
在触发器 tri_jieshu 和 tri_huanshu 中,update 语句中的 where 子句需要指定 book_id 而不是 borrow_id 和 back_id。应该修改为:
```
update book_borrowing set book_surplus=book_surplus-1 where book_id=(select book_id from inserted)
```
和
```
update book_borrowing set book_surplus=book_surplus+1 where book_id=(select book_id from inserted)
```
在 payment 存储过程中,应该将 datediff 函数的返回值赋值给一个变量,然后使用该变量进行比较。应该修改为:
```
create proc payment @id varchar(20),@name varchar(20) as
begin
declare @days int
select @days = datediff(day, borrow_date,back_date) FROM reader_back,reader_borrow
where back_id=borrow_id and back_id=@id and borrow_bookname=back_bookname
if (@days > 7)
begin
select borrow_date,back_date from reader_back,reader_borrow
where back_id=borrow_id and back_id=@id and borrow_bookname=back_bookname
end
end
```
在 proc_pay 存储过程中,应该将 @id 的数据类型修改为 varchar(50),并且在查询结果中使用别名来避免列名冲突。应该修改为:
```
create proc proc_pay (@id varchar(50)) as
begin
select b.back_id, b.back_bookname, b.back_date, r.borrow_date
from reader_back b, reader_borrow r
where b.back_id = r.borrow_id and b.back_id = @id and b.borrow_bookname = r.back_bookname
end
exec proc_pay @id='1'
```
创建存储过程,给定书号,到库存表中统计其数量,并用此数量修改图书表中该书的数量。调用该存储过程,修正书号为“A0120”的图书的数量。 (2)创建存储过程,对于读者编号,逐条检查借阅表中该读者的借阅情况:还书日期为空记录,借阅天数=系统日期-借阅日期,如借阅天数<15 天,则输出“正常”;15 天≤借阅天数≤30 天,则输出“通知还书”;借阅天数>30 天,则输出“逾期”。调用该存储过程,测试读者编号为“0001”的借阅情况。
好的,我明白了你的问题。以下是对应的两个存储过程:
1. 修改图书数量的存储过程:
```sql
CREATE PROCEDURE update_book_quantity
@book_id varchar(10)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @quantity int
-- 统计库存表中该书的数量
SELECT @quantity = SUM(quantity)
FROM inventory
WHERE book_id = @book_id
-- 修改图书表中该书的数量
UPDATE books
SET quantity = @quantity
WHERE book_id = @book_id
END
```
你可以通过以下命令来调用该存储过程,修正书号为“A0120”的图书的数量:
```sql
EXEC update_book_quantity 'A0120';
```
2. 检查借阅情况的存储过程:
```sql
CREATE PROCEDURE check_borrow_status
@reader_id varchar(10)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @borrow_date date
DECLARE @return_date date
DECLARE @borrow_days int
-- 逐条检查借阅表中该读者的借阅情况
DECLARE cur_borrow CURSOR FOR
SELECT borrow_date, return_date
FROM borrow
WHERE reader_id = @reader_id
OPEN cur_borrow
FETCH NEXT FROM cur_borrow INTO @borrow_date, @return_date
WHILE @@FETCH_STATUS = 0
BEGIN
-- 计算借阅天数
IF @return_date IS NULL
SET @borrow_days = DATEDIFF(day, @borrow_date, GETDATE())
ELSE
SET @borrow_days = DATEDIFF(day, @borrow_date, @return_date)
-- 输出借阅情况
IF @borrow_days < 15
PRINT '正常'
ELSE IF @borrow_days >= 15 AND @borrow_days <= 30
PRINT '通知还书'
ELSE
PRINT '逾期'
FETCH NEXT FROM cur_borrow INTO @borrow_date, @return_date
END
CLOSE cur_borrow
DEALLOCATE cur_borrow
END
```
你可以通过以下命令来调用该存储过程,测试读者编号为“0001”的借阅情况:
```sql
EXEC check_borrow_status '0001';
```
阅读全文