利用採購單來練習子報表(內嵌報表)
IF OBJECT_ID('Purch') IS NOT NULL
DROP TABLE Purch
IF OBJECT_ID('PurchDetail') IS NOT NULL
DROP TABLE PurchDetail
IF OBJECT_ID('Employ') IS NOT NULL
DROP TABLE Employ
IF OBJECT_ID('Supplier') IS NOT NULL
DROP TABLE Supplier
CREATE TABLE Purch(PurNO char(11),PurDate date,PurEmpNO char(5),SPLNO char(5))
CREATE TABLE PurchDetail(PurNO char(11),MtNum char(16),Spec nvarchar(100),PurQty int,Price money,Unit nchar(4),DealDate date)
CREATE TABLE Employ (EmpNO char(5),EmpName nchar(10))
CREATE TABLE Supplier (SPLNO char(5),SPLName nchar(10),Contact nchar(10),Tel nchar(10),Fax nchar(10))
INSERT INTO Employ VALUES
('001',N'王小明'),
('002',N'李大白')
INSERT INTO Supplier VALUES
('001',N'雜物供應商',N'王XX','00-1234567','00-1234444'),
('002',N'電熱供應商',N'張OO','00-3217654','00-3217777'),
('003',N'噴漆供應商',N'蔡YY','00-9999999','00-9999990'),
('004',N'馬達供應商',N'柯ZZ','00-6666666','00-6666666')
INSERT INTO Purch VALUES
('20131021001','20131021','001','002'),
('20131021002','20131021','002','004')
INSERT INTO PurchDetail VALUES
('20131021001','557D118L050',N'電熱_230V 1180x500W',60,231,N'支','20131028'),
('20131021001','557D146L065',N'電熱_230V 1460×650W',60,251,N'支','20131028'),
('20131021001','557D194L065',N'電熱_230V 1940x650W',36,278,N'支','20131028'),
('20131021002','554819',N'馬達_10"_200~240V 50/60Hz_雙轉向_UL認証',4000,310,N'顆','20140101'),
('20131021002','553201B',N'馬達_12"_4P_220V_雙轉向_無保護器',600,340,N'顆','20131128'),
('20131021002','552215B',N'馬達 12" 75/46W 4P_200-240V_左右轉_散熱',600,463,N'顆','20131128'),
('20131021002','5546257',N'馬達_1/4HP 3ψ 380~420V50/60Hz_三片式外殼',300,828,N'顆','20131101')
-- 子報表 DataSet(dsPurch)T-SQL 語法
SELECT
P.* ,
E.EmpName ,
S.SPLName ,
S.Contact ,
S.Tel ,
S.Fax
FROM Purch AS P
JOIN Employ AS E ON P.PurEmpNO = E.EmpNO
JOIN Supplier AS S ON P.SPLNO = S.SPLNO
-- 子報表明細 DataSet(dsPurchDetail)T-SQL 語法
SELECT * FROM PurchDetail WHERE PurNO = @PurNO