A表:(主单)
Id,JobNO,LoadPort,DiscPort,FinalDestination,Vessel,Voyage.
B表:(货单)
Id,JobNO,PrentJobNO(关联B.JobNo),cntNo,Weight,CBM,SealNo,CntSize,Marks,Description.
实现:
建一个视图,来实现填充如下报表效果:
Marks Description,和两表的所有字段
要求提示:A表的数据是唯一的,B表的数据具有多样性。(因为一个A.jobno下可能有多个B.jobno,
而B.jobno下又有多种cntNo)
请帮大伙帮忙看看,这个语句如何写?如描述不详尽,请指出!小弟谢过了。
这个需求似乎Inner join就可以啊
Create View V_A_B
as
select A.*,B.*,'待定' as [Marks Description] from A
inner join B on A.jobno=B.JobNO
修改完整:
IF OBJECT_ID('[tableA]') IS NOT NULL
DROP TABLE [TableA]
GO
CREATE TABLE [TableA] ([jobNo] int primary key identity ,
[AOther] [nvarchar](30))
INSERT INTO [TableA]
SELECT '测试A01' union all
SELECT '测试A02'
IF OBJECT_ID('[tableB]') IS NOT NULL
DROP TABLE [TableB]
GO
CREATE TABLE [TableB] (
[jobhno] nvarchar(10),
[parentjobno] int ,
[cntno] nvarchar(20),
[Marks] nvarchar(10),
[Description] [nvarchar](200)
)
INSERT INTO [TableB]
SELECT'01',1,'c1','a','treww'union all
select'01',1,'c2','b','erwe'union all
select'01',1,'c2','c','sdfsdf'union all
select'02',2,'c3','d','dsfsdf'union all
select'03',2,'c3','e','sdfsf'union all
select'04',2,'c4','f','fsd'union all
select'05',3,'c5','g','ttt'
select a.[jobNo],B.jobhno,B.cntno,B.Marks,B.[description],a.[AOther]
from [tablea] A
inner join [tableb] B on A.jobno=B.[parentjobno]
/*
jobNo jobhno cntno Marks description AOther
1 01 c1 a treww 测试A01
1 01 c2 b erwe 测试A01
1 01 c2 c sdfsdf 测试A01
2 02 c3 d dsfsdf 测试A02
2 03 c3 e sdfsf 测试A02
2 04 c4 f fsd 测试A02
(6 row(s) affected)
*/
其实还是一个inner join
Create proc CPP_GetOneJobDemo
(@jobno int)
as
select a.[jobNo],B.jobhno,B.cntno,B.Marks,B.[description],a.[AOther]
from [tablea] A
inner join [tableb] B on A.jobno=B.[parentjobno] and a.jobno=@jobno
go
--调用 CPP_GetOneJobDemo 1