0% found this document useful (0 votes)
5 views4 pages

De4 Full Lowercase

The document outlines the creation of a database 'qlxe' with tables for suppliers, fees, and registration details. It includes SQL procedures for various operations such as filtering by price, viewing supplier addresses, and deleting suppliers by name. Additionally, it provides a function to retrieve vehicle registration information based on a specified quantity.

Uploaded by

hayhayvuivui2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

De4 Full Lowercase

The document outlines the creation of a database 'qlxe' with tables for suppliers, fees, and registration details. It includes SQL procedures for various operations such as filtering by price, viewing supplier addresses, and deleting suppliers by name. Additionally, it provides a function to retrieve vehicle registration information based on a specified quantity.

Uploaded by

hayhayvuivui2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

bai tap co so du lieu qlxe

1. tao co so du lieu va cac bang

create database qlxe;


go

use qlxe;
go

create table tblnhacungcap (


manhacc char(10) primary key,
tennhacc nvarchar(100),
diachi nvarchar(100)
);

create table tblmucphi (


mamp char(10) primary key,
dongia int,
mota nvarchar(250)
);

create table tbldangkydungcap (


madkcc char(10) primary key,
manhacc char(10) foreign key references tblnhacungcap(manhacc),
mamp char(10) foreign key references tblmucphi(mamp),
ngaybatdaucungcap date,
ngayketthuccungcap date,
soluongxedangky int
);

2. cursor - in danh sach theo dia chi a

declare @tennhacc nvarchar(100), @mamp char(10), @dongia int, @soluong int;

declare cur cursor for


select ncc.tennhacc, mp.mamp, mp.dongia, dkcc.soluongxedangky
from tblnhacungcap ncc
join tbldangkydungcap dkcc on ncc.manhacc = dkcc.manhacc
join tblmucphi mp on dkcc.mamp = mp.mamp
where ncc.diachi = 'a';

open cur;
fetch next from cur into @tennhacc, @mamp, @dongia, @soluong;

while @@fetch_status = 0
begin
print 'tennhacc: ' + @tennhacc + ', mamp: ' + @mamp +
', dongia: ' + cast(@dongia as nvarchar) +
', soluongxedangky: ' + cast(@soluong as nvarchar);
fetch next from cur into @tennhacc, @mamp, @dongia, @soluong;
end

close cur;
deallocate cur;

3. thu tuc: loc dongia >= d

create procedure sp_loctheodongia


@d int
as
begin
select ncc.tennhacc, mp.mamp, mp.dongia, dkcc.soluongxedangky
from tblnhacungcap ncc
join tbldangkydungcap dkcc on ncc.manhacc = dkcc.manhacc
join tblmucphi mp on dkcc.mamp = mp.mamp
where mp.dongia >= @d;
end

4. ham: thong tin xe dang ky >= a

create function fn_thongtinxedangky (@a int)


returns table
as
return
select ncc.manhacc, ncc.tennhacc, mp.mamp, mp.dongia,
dkcc.ngaybatdaucungcap, dkcc.ngayketthuccungcap, dkcc.soluongxedangky
from tblnhacungcap ncc
join tbldangkydungcap dkcc on ncc.manhacc = dkcc.manhacc
join tblmucphi mp on dkcc.mamp = mp.mamp
where dkcc.soluongxedangky >= @a;

5. thu tuc: xem dia chi nha cung cap

create procedure sp_xemdiachincc


@ten nvarchar(100)
as
begin
if exists (select 1 from tblnhacungcap where tennhacc = @ten)
select diachi from tblnhacungcap where tennhacc = @ten;
else
print 'khong ton tai nha cung cap co ten nay';
end

6. thu tuc: tao mamp tu dong

create procedure sp_taomamptudong


as
begin
declare @newma char(10);
select @newma = 'mp' + right('0000' + cast(isnull(max(cast(substring(mamp, 3, 10) as
int)) + 1, 1) as varchar), 4)
from tblmucphi;

print 'ma moi la: ' + @newma;


end

7. thu tuc: xoa theo tennhacc

create procedure sp_xoatheotennhacc


@ten nvarchar(100)
as
begin
declare @ma char(10);

select @ma = manhacc from tblnhacungcap where tennhacc = @ten;

if @ma is not null


begin
delete from tbldangkydungcap where manhacc = @ma;
delete from tblnhacungcap where manhacc = @ma;
print 'da xoa thanh cong nha cung cap va lien quan.';
end
else
print 'khong ton tai nha cung cap co ten nay';
end

8. thu tuc: cap nhat tbldangkydungcap

create procedure sp_capnhatdkcc


@madkcc char(10),
@manhacc char(10),
@mamp char(10),
@ngaybd date,
@ngaykt date,
@soluong int
as
begin
if exists (select 1 from tbldangkydungcap where madkcc = @madkcc)
begin
print 'ma dang ky cung cap da ton tai';
return;
end

if not exists (select 1 from tblnhacungcap where manhacc = @manhacc)


or not exists (select 1 from tblmucphi where mamp = @mamp)
begin
print 'du lieu khoa ngoai khong hop le';
return;
end

insert into tbldangkydungcap


values (@madkcc, @manhacc, @mamp, @ngaybd, @ngaykt, @soluong);
end

You might also like