By GokiSoft.com|
14:31 03/06/2023|
SQL Server/MySQL
[Share Code] Tìm hiểu View & Procedure trong SQL Server - G2212I
Trong quy trình phát triển phần mềm (CODE & CSDL)
Viết query trực tiếp trong code ->đọc dữ liệu ra
Ưu điểm: Thay CSDL -> ko phải cập nhật lại nhiều query
Nhược điểm: Thay ngôn ngữ lập trình -> Code lại toàn bộ dự án & query
Lưu truy vấn vào trong CSDL:
Ưu điểm: Thay ngôn ngữ lập trình -> ko phải viết lại query
Thay CSDL -> chuyển CSDL cũng dễ
join dự án ngân hàng, tài chính, ...
Nhược điểm:
??? view, store
Store -> Đặc điểm
Lưu trữ lệnh như view
Lưu nhiều lệnh trong store (insert, update, nhiều câu lệnh select, delete)
Giống như function trong ngôn ngữ lập trình
Hàm không đối số
Hàm có nhiều đối số
Hàm có kết quả trả về
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
go
-- Luu y: 1 view -> chua 1 select
-- view (vw_giaidau) -> coi nhu 1 table
create view vw_giaidau
as
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
go
alter view vw_giaidau
as
select TrongTai.fullname TenTrongTai, TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
go
select * from vw_giaidau
go
select TenTrongTai, level, exp from vw_giaidau
go
drop view vw_giaidau
go
------------------------ store ---------------------------------
create proc prc_test
as
begin
select * from CauThu
select TenTrongTai, level, exp from vw_giaidau
select * from vw_giaidau
end
exec prc_test
---------------- Tim hieu
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
and LichSu.id_club_1 = 1
go
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
and LichSu.id_club_1 = 2
go
create proc sp_view_giaidau
@clubId int
as
begin
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
and LichSu.id_club_1 = @clubId
end
exec sp_view_giaidau 1
exec sp_view_giaidau 2
-- prc nhieu tham so dau vao
create proc sp_view_giaidau2
@clubId1 int,
@clubId2 int
as
begin
select TrongTai.fullname 'Ten Trong Tai', TrongTai.level, TrongTai.exp, LichSu.giaidau,
LichSu.id_club_1, DB1.name 'Doi Bong 1',
LichSu.id_club_2, DB2.name 'Doi Bong 2'
from TrongTai, LichSu, DoiBong DB1, DoiBong DB2
where LichSu.id_trongtai = TrongTai.id
and DB2.id = LichSu.id_club_2
and DB1.id = LichSu.id_club_1
and LichSu.id_club_1 = @clubId1
and LichSu.id_club_2 = @clubId2
end
exec sp_view_giaidau2 1,2
exec sp_view_giaidau2 1,3
exec sp_view_giaidau2 1,4
---- prc co tham so dau vao (int, float, nvarchar, ...) & du lieu da ra (int, float, nvarchar, ...)
select * from CauThu
select count(*) from CauThu
--- Viet proc tim so CauThu tham gia tu ngay 2009-01-01 toi hien nay
create proc sp_count_cauthu
@joinedData date,
@count int output
as
begin
select * from CauThu where joined_date >= @joinedData
select @count = count(*) from CauThu where joined_date >= @joinedData
end
declare @count2 int
exec sp_count_cauthu '2009-01-01',@count = @count2 output
print N'So phan tu tim thay: ' + CONVERT(nvarchar, @count2)
go
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)