我收到此警告
消息512,级别16,状态1,行2子查询返回的值超过1 值。子查询遵循=,!=,<,< =时,不允许这样做, >,> =或当子查询用作表达式时。
和我的查询
CREATE VIEW [dbo].[vwTrnLatihPerson2]
AS
SELECT
A.MKPersonID,
A.RegNo,
A.NoFix,
A.Nama,
NIK=
case
When A.RegNo<>0 And A.NoFix=0 then (Select NIK from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select NIK from vwMstBorongan Where FixNo=A.NoFix)
End,
Pendidikan=
case
When A.RegNo<>0 And A.NoFix=0 then (Select Pendidikan from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select Pendidikan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.JabatanID,J.Jabatan,J.SubDeptID,D.SubDeptAbbr,D.SubDeptName,D.DivisiID,
Divisi=(Select Divisi From tblMstDivisi Where DivisiID=D.DivisiID),
NamaDivisi=(Select NamaDivisi From tblMstDivisi Where DivisiID=D.DivisiID),
StatusTK=
case
When A.RegNo<>0 And A.NoFix=0 then 'Karyawan'
when A.NoFix<>0 And A.RegNo=0 then (Select Pekerjaan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.TglMasuk,
TglKeluar=
case
When A.RegNo<>0 And A.NoFix=0 then (Select TglKeluar from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select TglKeluar from vwMstBorongan Where FixNo=A.NoFix)
End,
A.NotActive
FROM
dbo.tblTrnLatihPerson A Left Outer Join
dbo.tblMstJabatan J On J.JabatanID=A.JabatanID LEFT Outer Join
dbo.tblMstSubDepartment D On D.SubDeptID=J.SubDeptID
--WHERE A.NotActive <> 1
GO
答案 0 :(得分:0)
Case
When
只需要一个结果,使用SELECT TOP 1
来匹配该条件。
When A.RegNo<>0 And A.NoFix=0 then (Select TOP 1 NIK from vwMstKaryawan Where RegNo=A.Regno)
对所有其他人做同样的事。
答案 1 :(得分:0)
子查询必须返回单个值,因此只需使用top关键字和1行 你的代码将是
CREATE VIEW [dbo].[vwTrnLatihPerson2]
AS
SELECT
A.MKPersonID,
A.RegNo,
A.NoFix,
A.Nama,
NIK=
case
When A.RegNo<>0 And A.NoFix=0 then (Select top 1 NIK from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select top 1 NIK from vwMstBorongan Where FixNo=A.NoFix)
End,
Pendidikan=
case
When A.RegNo<>0 And A.NoFix=0 then (Select top 1 Pendidikan from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select top 1 Pendidikan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.JabatanID,J.Jabatan,J.SubDeptID,D.SubDeptAbbr,D.SubDeptName,D.DivisiID,
Divisi=(Select top 1 Divisi From tblMstDivisi Where DivisiID=D.DivisiID),
NamaDivisi=(Select top 1 NamaDivisi From tblMstDivisi Where DivisiID=D.DivisiID),
StatusTK=
case
When A.RegNo<>0 And A.NoFix=0 then 'Karyawan'
when A.NoFix<>0 And A.RegNo=0 then (Select top 1 Pekerjaan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.TglMasuk,
TglKeluar=
case
When A.RegNo<>0 And A.NoFix=0 then (Select top 1 TglKeluar from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then (Select top 1 TglKeluar from vwMstBorongan Where FixNo=A.NoFix)
End,
A.NotActive
FROM
dbo.tblTrnLatihPerson A Left Outer Join
dbo.tblMstJabatan J On J.JabatanID=A.JabatanID LEFT Outer Join
dbo.tblMstSubDepartment D On D.SubDeptID=J.SubDeptID
--WHERE A.NotActive <> 1
GO
答案 2 :(得分:0)
以下是解决此错误的3种方法。
您可以在每个子查询中添加TOP 1,但不确定每次都能提供所需的结果
其次是您视图的最佳视图。加入你的冗余表,但你不能给前1名,所以要确保只有一个值返回给予越来越多的条件。 (在isnull条件下,如果数据类型为int或numeric,则可以更改为0)
CREATE VIEW [dbo].[vwTrnLatihPerson2]
AS
SELECT
A.MKPersonID,
A.RegNo,
A.NoFix,
A.Nama,
NIK=
case
When A.RegNo<>0 And A.NoFix=0 then isnull( VMK.NIK , '') --(Select NIK from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then isnull( VMB.NIK , '') --(Select NIK from vwMstBorongan Where FixNo=A.NoFix)
End,
Pendidikan=
case
When A.RegNo<>0 And A.NoFix=0 then isnull( VMK.Pendidikan , '') --(Select Pendidikan from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then isnull( VMB.Pendidikan , '') --(Select Pendidikan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.JabatanID,J.Jabatan,J.SubDeptID,D.SubDeptAbbr,D.SubDeptName,D.DivisiID,
Divisi=isnull( MD.Divisi , '')--(Select Divisi From tblMstDivisi Where DivisiID=D.DivisiID),
NamaDivisi=isnull( MD.NamaDivisi , '')--(Select NamaDivisi From tblMstDivisi Where DivisiID=D.DivisiID),
StatusTK=
case
When A.RegNo<>0 And A.NoFix=0 then 'Karyawan'
when A.NoFix<>0 And A.RegNo=0 then isnull( VMB.Pekerjaan , '') --(Select Pekerjaan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.TglMasuk,
TglKeluar=
case
When A.RegNo<>0 And A.NoFix=0 then isnull( VMK.TglKeluar , '') --(Select TglKeluar from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then isnull( VMB.TglKeluar , '') --(Select TglKeluar from vwMstBorongan Where FixNo=A.NoFix)
End,
A.NotActive
FROM
dbo.tblTrnLatihPerson A
Left Outer Join dbo.tblMstJabatan J On J.JabatanID=A.JabatanID
LEFT Outer Join dbo.tblMstSubDepartment D On D.SubDeptID=J.SubDeptID
--this 2 table added with left outer join AND NOTE IT SEEMS MULTIPLE VALUE YOU WILL GET THEN YOU HAVE TO ADD OTHER CONDITION TO JOIN
LEFT OUTER JOIN DBO.vwMstKaryawan VMK ON VMK.RegNo=A.Regno
LEFT OUTER JOIN DBO.vwMstBorongan VMB ON VMB.FixNo=A.NoFix
LEFT OUTER JOIN DBO.tblMstDivisi MD ON MD.DivisiID=D.DivisiID
--WHERE A.NotActive <> 1
GO
使用cte
CREATE VIEW [dbo].[vwTrnLatihPerson2]
AS
; with cte as
(
--GIVE ALIAS NAME TO DIFFERENTIATE THE SAME COLUMN NAME
SELECT TOP 1 A.RegNo, A.NoFix, VMK.NIK AS ANIK, VMB.NIK AS BNIK, VMK.Pendidikan APEND, VMB.Pendidikan BPEND, --VMK.Pekerjaan,
VMB.Pekerjaan BPEK, VMK.TglKeluar ATGL, VMB.TglKeluar BTGL
FROM
dbo.tblTrnLatihPerson A
Left Outer Join vwMstKaryawan VMK ON RegNo=A.Regno --give other condition if you want WITH ORDER BY
Left Outer Join vwMstBorongan VMB ON FixNo=A.NoFix --give other condition if you want WITH ORDER BY
),
cte1 as
(
select top 1 A.Regno, D.NamaDivisi, D.Divisi from
dbo.tblTrnLatihPerson A
LEFT Outer Join dbo.tblMstDivisi D On A.DivisiID=D.DivisiID --give other condition if you want WITH ORDER BY
)
SELECT
A.MKPersonID,
A.RegNo,
A.NoFix,
A.Nama,
NIK=
case
When A.RegNo<>0 And A.NoFix=0 then CTE.ANIK --(Select NIK from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then CTE.BNIK --(Select NIK from vwMstBorongan Where FixNo=A.NoFix)
End,
Pendidikan=
case
When A.RegNo<>0 And A.NoFix=0 then CTE.APEND --(Select Pendidikan from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then CTE.BPEND --(Select Pendidikan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.JabatanID,J.Jabatan,J.SubDeptID,D.SubDeptAbbr,D.SubDeptName,D.DivisiID,
Divisi=CTE1.Divisi-(Select Divisi From tblMstDivisi Where DivisiID=D.DivisiID),
NamaDivisi= CTE1.NamaDivisi--(Select NamaDivisi From tblMstDivisi Where DivisiID=D.DivisiID),
StatusTK=
case
When A.RegNo<>0 And A.NoFix=0 then 'Karyawan'
when A.NoFix<>0 And A.RegNo=0 then CTE.BPEK --(Select Pekerjaan from vwMstBorongan Where FixNo=A.NoFix)
End,
A.TglMasuk,
TglKeluar=
case
When A.RegNo<>0 And A.NoFix=0 then CTE.ATGL --(Select TglKeluar from vwMstKaryawan Where RegNo=A.Regno)
when A.NoFix<>0 And A.RegNo=0 then CTE.BTGL --(Select TglKeluar from vwMstBorongan Where FixNo=A.NoFix)
End,
A.NotActive
FROM
dbo.tblTrnLatihPerson A
Left Outer Join dbo.tblMstJabatan J On J.JabatanID=A.JabatanID
LEFT Outer Join dbo.tblMstSubDepartment D On D.SubDeptID=J.SubDeptID
LEFT Outer Join cte On cte.RegNo=A.Regno
LEFT Outer Join cte1 On cte1.Regno=A.Regno
--WHERE A.NotActive <> 1
GO