CREATE VIEW ViewName [(ColName1, ...)]
[WITH ENCRYPTION]
AS <Phát bieu select>
[WITH CHECK OPTION]
Ví dụ:
USE BanHang
CREATE VIEW vwTienHD (MaHD, NgayLap, TienHD)
AS SELECT a.MaHD, NgayHD, SUM(SL*DG)
FROM HoaDon a, CTHD b
GROUP BY a.MaHD, NgayHD
HAVING SUM(SL*DG) > 0
Go
EXEC sp_helptext vwTienHD
SELECT * FROM vwTienHD
Ví dụ:
CREATE VIEW vwTienHD (MaHD, NgayLap, TienHD)
AS
SELECT a.MaHD, NgayHD, SUM(SL*DG)
FROM HoaDon a, CTHD b WHERE Year(NgayHD) =2004
GROUP BY a.MaHD, NgayHD HAVING SUM(SL*DG) > 0
Union
SELECT a.MaHD, NgayHD, SUM(SL*DG)
FROM HoaDon a, CTHD b WHERE Year(NgayHD) =2005
GROUP BY a.MaHD, NgayHD HAVING SUM(SL*DG) > 0
Go
EXEC SP_HelpText vwTienHD