Code SQL Server Procedure

3 1.4K 41
Code SQL Server Procedure

Đang tải... (xem toàn văn)

Thông tin tài liệu

1)Viết một thủ tục tính tổng tiền thu (TotalDue) của mỗi khách hàng trong một tháng bất kỳ của một năm bất kỳ (tham số tháng và năm) được nhập từ bàn phím, thông tin gồm: CustomerID, SumofTotalDue =Sum(TotalDue)create proc totalDue thang datetime,nam datetimeasbeginselect CustomerID,sumofTotal=SUM(SubTotal)from Sales.SalesOrderHeaderwhere MONTH(OrderDate)=thang and YEAR(OrderDate)=namgroup by CustomerIDendexec totalDue 7,20054)Tạo thủ tục đặt tên là TongThu có tham số vào là mã nhân viên, tham số đầu ra là tổng trị giá các hóa đơn nhân viên đó bán được. Sử dụng lệnh RETURN để trả về trạng thái thành công hay thất bại của thủ tục. create proc TongThu manv int,tonghd money outputasbeginselect tonghd=SUM(SubTotal)from Sales.SalesOrderHeaderwhere SalesPersonID=manvreturn 1if tonghd>0 return 1else return 0enddeclare tong moneyexec TongThu 282,tong outputprint convert(char(10),tong)10)Viết thủ tục Sp_Update_Product có tham số Productid dùng để tăng listprice lên 10% nếu sản phẩm này tồn tại, ngược lại hiện thông báo không có sản phẩm này. alter proc sp_up masp intasbeginif masp is not null beginselect ProductID,ListPrice=ListPrice+0.1ListPricefrom Production.Productwhere ProductID=maspendelse printKo co sp nayendexec sp_up null

1) Viết thủ tục tính tổng tiền thu (TotalDue) khách hàng tháng năm (tham số tháng năm) nhập từ bàn phím, thông tin gồm: CustomerID, SumofTotalDue =Sum(TotalDue) create proc totalDue @thang datetime,@nam datetime as begin select CustomerID,sumofTotal=SUM(SubTotal) from Sales.SalesOrderHeader where MONTH(OrderDate)=@thang and YEAR(OrderDate)=@nam group by CustomerID end exec totalDue 7,2005 4) Tạo thủ tục đặt tên TongThu có tham số vào mã nhân viên, tham số đầu tổng trị giá hóa đơn nhân viên bán Sử dụng lệnh RETURN để trả trạng thái thành công hay thất bại thủ tục create proc TongThu @manv int,@tonghd money output as begin select @tonghd=SUM(SubTotal) from Sales.SalesOrderHeader where SalesPersonID=@manv return if @tonghd>0 return else return end declare @tong money exec TongThu 282,@tong output print convert(char(10),@tong) 10) Viết thủ tục Sp_Update_Product có tham số Productid dùng để tăng listprice lên 10% sản phẩm tồn tại, ngược lại thông báo sản phẩm alter proc sp_up @masp int as begin if @masp is not null begin select ProductID,ListPrice=ListPrice+0.1*ListPrice from Production.Product where ProductID=@masp end else print'Ko co sp nay' end exec sp_up null 9) Viết thủ tục XoaHD, dùng để xóa hóa đơn bảng Sales.SalesOrderHeader biết SalesOrderID Lưu ý trước xóa mẫu tin Sales.SalesOrderHeader phải xóa mẫu tin hoá đơn Sales.SalesOrderDetail Nếu không xoá hoá đơn không phép xóa Sales.SalesOrderDetail hóa đơn create proc xoahd @mahd int as begin if exists(select * from Sales.SalesOrderDetail where SalesOrderID=@mahd) begin delete from Sales.SalesOrderDetail where SalesOrderID=@mahd delete from Sales.SalesOrderHeader where SalesOrderID=@mahd end else begin delete from Sales.SalesOrderHeader where SalesOrderID=@mahd end end 7) Tạo thủ tục hiển thị tên số tiền mua cửa hàng mua nhiều hàng theo năm cho create proc hienthi @nam datetime as begin select top name,SubTotal from Purchasing.PurchaseOrderHeader poh join Purchasing.Vendor v on poh.VendorID=v.BusinessEntityID where YEAR(OrderDate)=@nam order by SubTotal DESC end exec hienthi 2007 Cursor alter proc @inkhmuasp int as begin declare @makh int,@ten nvarchar(50) declare dskh cursor for select CustomerID,FirstName+' '+LastName as name from Person.Person p join Sales.SalesOrderHeader soh on p.BusinessEntityID=soh.CustomerID open dskh fetch next from dskh into @makh,@ten print 'Ma khach hang' +space(5)+'Ten' while @@FETCH_STATUS=0 begin print convert(char(5),@makh)+space(5)+convert(char(5),@ten) begin declare @masp int,@soluong int,@gia int declare dssp cursor for select ProductID,OrderQty,UnitPrice from Sales.SalesOrderDetail sod join Sales.SalesOrderHeader soh on sod.SalesOrderID=soh.SalesOrderID where CustomerID=@makh open dssp fetch next from dssp into @masp,@soluong,@gia print 'Ma SP'+space(5)+'So luong' +space(5)+'gia' while @@FETCH_STATUS=0 begin print convert(char(5),@masp) +space(5)+convert(char(5),@soluong)+space(5)+convert(char(5),@gia) fetch next from dssp into @masp,@soluong,@gia end end close dssp deallocate dssp fetch next from dskh into @makh,@ten end close dskh deallocate dskh end exec inkhmuasp

Ngày đăng: 06/12/2016, 12:03

Tài liệu cùng người dùng

Tài liệu liên quan