0% found this document useful (0 votes)
2 views

[CSDL]T SQL Script Procedure Function (HD)

The document contains SQL scripts for various database operations including calculating age, retrieving order details, inserting new employees, and creating stored procedures for managing product categories and customer orders. It includes examples of conditional statements, aggregate functions, and the use of temporary tables. Additionally, it demonstrates how to create and execute stored procedures to handle business logic in a database context.

Uploaded by

xuannhu29042005
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)
2 views

[CSDL]T SQL Script Procedure Function (HD)

The document contains SQL scripts for various database operations including calculating age, retrieving order details, inserting new employees, and creating stored procedures for managing product categories and customer orders. It includes examples of conditional statements, aggregate functions, and the use of temporary tables. Additionally, it demonstrates how to create and execute stored procedures to handle business logic in a database context.

Uploaded by

xuannhu29042005
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

Bài thực hành 1: Script

use Northwind
go

--1
declare @ns datetime, @tuoi int
set dateformat dmy
set @ns = '29/01/2002'
set @tuoi = DATEDIFF(day,@ns,getdate())/365
print 'Tuoi cua ban la ' + convert(nvarchar(3),@tuoi)

--2
Select EmployeeID, count(OrderID) As SoLuong,
case
when count(OrderID) < 3 then N'Sốlượng đơn hàng quá ít!'
else 'Okey'
end Trangthai
From Orders
Where Year (OrderDate) = 1997
Group by EmployeeID

--3
declare @maHD int
set @maHD = 10248
select o.ProductID, p.ProductName
from [Order Details] o, Products p
where OrderID = @maHD and p.ProductID = o.ProductID

--4
Select * from Employees
declare @tenNV nvarchar(10)
set @tenNV = N'Thanh An'
if not exists (select FirstName from Employees where FirstName = @tenNV)
insert into Employees (FirstName, LastName)
values ('a',@tenNV)
Select @@ROWCOUNT

--5
select c.CustomerID, COUNT(distinct o.OrderID) as SLDH, SUM(Quantity) as SLSP
from Customers c, Orders o, [Order Details] d
where c.CustomerID = o.CustomerID and d.OrderID = o.OrderID
group by c.CustomerID
having COUNT(distinct o.OrderID) =1 and SUM(Quantity) > 5

declare @TKKH table (MAKH nchar(5), SLDH int, SLSP int)


insert into @TKKH
select c.CustomerID, COUNT(distinct o.OrderID) as SLDH, SUM(Quantity) as
SLSP
from Customers c, Orders o, [Order Details] d
where c.CustomerID=o.CustomerID and d.OrderID=o.OrderID
group by c.CustomerID

select *from @TKKH


where SLDH = 1 and SLSP > 5

--Câu 6
Declare @year nvarchar(50)
Set @year = 2002
Select soh.SalesPersonID, con.FirstName,con.LastName, SUM(soh.TotalDue) As
'TongDoanhThu'
From Sales.SalesOrderHeader soh, Person.Contact con
Where soh.SalesPersonID = con.ContactID
AND YEAR(soh.OrderDate) = @year
Group By soh.SalesPersonID, con.FirstName , con.LastName
Having SUM(soh.TotalDue) > (Select AVG(TotalDue)
From Sales.SalesOrderHeader
Where YEAR(OrderDate) = @year)
Order By soh.SalesPersonID ASC
Go
--cau7
declare @NgayDH datetime, @slnv int, @tongDH int, @TBDH float
set dateformat dmy
set @NgayDH ='5/7/1996'

select @slnv = COUNT(*)


from Employees
select @tongDH = COUNT(*)
from Orders

set @TBDH = ROUND (@tongDH/@slnv,0)

select e.EmployeeID, e.LastName + ' ' + e.FirstName as Hoten, COUNT(OrderID) as


SLDHDL,
'ChenhLech' = @TBDH - COUNT(OrderID)
from Employees e, Orders o
where e.EmployeeID = o.EmployeeID and YEAR(OrderDate) = YEAR(@NgayDH)
group by e.EmployeeID, e.LastName + ' ' + e.FirstName

select *from Orders

Bài thực hành 2: Stored Procedure


--1) Viế t stored- procedure xuất danh sách các danh mục chưa có sản phẩ
m nào.
CREATE PROC spCau1
AS
BEGIN
SELECT *
FROM Categories
WHERE CategoryID NOT IN
(SELECT DISTINCT CategoryID
FROM Products)
END

INSERT INTO Categories(CategoryName)


VALUES (N'Nước giải khát')

EXEC spCau1

-- 2)Viết stored- procedure xuất danh sách khách hàng có đơn đặt hàng chưa giao
với sốlượng sản phẩ m mua > 1.
--Thủ tục có tham sốvào
go
CREATE PROC spCau2
AS
BEGIN
SELECT c.CompanyName
FROM Customers c, Orders o, [Order Details] d
WHERE o.ShippedDate is null and c.CustomerID = o.CustomerID and o.OrderID =
d.OrderID
group by c.CompanyName, o.OrderID
having sum(d.Quantity) > 10
end
exec spCau2

-- 3) Viế
t stored-procedure truyề
n vào mã sản phẩ
m, xuấ
t ra thông tin sản phẩm.

Go
CREATE PROC spCau3 @MaSP int
AS
BEGIN
SELECT *
FROM Products
WHERE ProductID = @MaSP
END
DECLARE @MaSanPham int
SET @MaSanPham=2

EXEC spCau3 @MaSanPham

--Cau4
go
Create proc spCau4 @NgayBD datetime, @NgayKT datetime
As
begin
if @NgayBD is null
Set @NgayBD = DATEADD (dd,+(Day(GEtDate())-1), getDAte())

select *
from Products
where ProductID in
(select ProductID
from [Order Details]
where OrderID in (
select OrderID
from Orders
where OrderDate between @NgayBD and @NgayKT)
)
end
set dateformat dmy
declare @NgayBatDau datetime, @NgayKetThuc datetime
set @NgayBatDau = '25/6/1996'
set @NgayKetThuc = '28/8/1996'

exec spCau4 @NgayBatDau, @NgayKetThuc

--Cau 6
go
create proc spCau6 @TenSP nvarchar(40), @DG money, @Sl int, @TT bit, @KetQua int
output
as
begin
if exists (select ProductName from Products where ProductName = @TenSP)
set @KetQua = -2
else
begin
insert into Products(ProductName, UnitPrice, QuantityPerUnit,Discontinued)
values (@TenSP, @DG, @Sl, @TT)
if @@ROWCOUNT > 0
set @KetQua = 1
else
set @KetQua = -1
end
end
declare @Kq int
exec spCau6 'Cocacola1',10000,2,-1, @Kq output
print @Kq

go
create function fnKTKH (@MaKH nchar(5))
returns bit
as
begin
declare @kq bit
if exists (select CompanyName from Customers where CustomerID = @MaKH)
set @kq = 1
else
set @kq = 0
return @kq
end

alter proc spThemDH @MaKH nchar(5)


as
begin
if [dbo].[fnKTKH] (@MaKH) = 1
begin
insert into Orders(CustomerID, OrderDate)
values (@MaKH, GETDATE())
print 'Dat Hang Thanh Cong'
end
else
print 'Loi'
end

exec spThemDH ANTON

You might also like