2016-06-07 59 views
-1

返回多個值,我得到這樣的警告錯誤子查詢中SQLSERVER 2008

消息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 
+1

的消息說,子查詢中的一個返回更多的CTE比1排。您需要重新查看查詢和數據,以檢查是否有任何其他條件在子查詢中被遺漏。快速確定哪個子查詢將「TOP 1」添加到select語句,然後重新執行查詢 – Squirrel

回答

0

CaseWhen預計只有一個結果,使用SELECT TOP 1相匹配的條件。

When A.RegNo<>0 And A.NoFix=0 then (Select TOP 1 NIK from vwMstKaryawan Where RegNo=A.Regno) 

對其他所有情況都做同樣的事情。

0

有子查詢必須返回單個值,這樣只需使用頂部帶有關鍵字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 
0

這裏是3的方法來解決此問題。

  1. 要麼你在你添加TOP 1每個子查詢,但它不知道每次都

  2. 二是你的看法的最佳視圖給你想要的結果。加入你的冗餘表,但你不能給頂端1,所以要確保只有一個價值回報,因爲這給了越來越多的條件。 (在ISNULL條件你可以改變0,如果你的數據類型爲int或數字)

    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 
    
  3. 使用該

    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