2015-01-12 184 views
0

我想在另一個臨時表中插入子查詢的結果。子查詢工作正常時,我只執行,但不是當作爲子查詢。它引發語法錯誤。 錯誤是:')'附近語法不正確。這是對的子查詢子查詢不能正常工作

DECLARE @TempT TABLE 
    (
     RowID INT IDENTITY(1, 1) , 
     Date DATETIME , 
     Type NVARCHAR(MAX) , 
     V_No INT , 
     Chq_No INT , 
     Description NVARCHAR(MAX) , 
     Debit MONEY , 
     Credit MONEY , 
     voucher_type_no INT , 
     status NVARCHAR(10) , 
     Clr_Date DATETIME , 
     Voucher_Id INT , 
     Party_Name NVARCHAR(MAX) , 
     DateYYYYMMDD DATETIME 
    ) 

DECLARE @i INT= 1 
INSERT INTO @TempT 
     SELECT * 
     FROM (SELECT v.date 'Date' , 
          vt.voucher_type_shortname 'Type' , 
          v.voucher_no 'V_No' , 
          v.cheque_no 'Chq_No' , 
          a.account_name 'Description' , 
          (CASE WHEN SUM(v.amount) > 0 THEN SUM(v.amount) 
            ELSE 0 
           END) Debit , 
          (CASE WHEN SUM(v.amount) < 0 THEN SUM(-v.amount) 
            ELSE 0 
           END) Credit , 
          v.voucher_type_no , 
          r.status , 
          r.recon_date 'Clr_Date' , 
          MIN(v.voucher_id) Voucher_Id , 
          '' 'Party_Name' , 
          CONVERT(VARCHAR(10), r.recon_date, 126) 'Date YYYY-MM-DD' 
        FROM  voucher v 
          LEFT OUTER JOIN reconcilation r ON v.voucher_id = r.voucher_id 
          LEFT OUTER JOIN account a ON v.other_acno = a.account_no , 
          voucher_type vt 
        WHERE  v.voucher_type_no = vt.voucher_type_no 
          AND v.voucher_type_no > 0 
          AND v.other_acno = a.account_no 
          AND v.acc_year = 51 
          AND v.account_no = 10030 
          AND R.recon_date <= '2015-01-12' 
          AND R.recon_date >= '2009-04-01' 
          AND V.posted IN (1, 2) 
        GROUP BY v.voucher_no , 
          v.cheque_no , 
          v.date , 
          vt.voucher_type_shortname , 
          a.account_name , 
          v.voucher_type_no , 
          r.status , 
          r.recon_date 
       ) 
+0

一個評論後使用別名第一線,不要將舊連接語法(選擇...從T1,T2),以及更新JOIN語法,它很混亂。 – jarlh

回答

2

最後支架

DECLARE @TempT TABLE 
    (
     RowID INT IDENTITY(1, 1) , 
     Date DATETIME , 
     Type NVARCHAR(MAX) , 
     V_No INT , 
     Chq_No INT , 
     Description NVARCHAR(MAX) , 
     Debit MONEY , 
     Credit MONEY , 
     voucher_type_no INT , 
     status NVARCHAR(10) , 
     Clr_Date DATETIME , 
     Voucher_Id INT , 
     Party_Name NVARCHAR(MAX) , 
     DateYYYYMMDD DATETIME 
    ) 

DECLARE @i INT= 1 
INSERT INTO @TempT 
     SELECT * 
     FROM (SELECT v.date 'Date' , 
          vt.voucher_type_shortname 'Type' , 
          v.voucher_no 'V_No' , 
          v.cheque_no 'Chq_No' , 
          a.account_name 'Description' , 
          (CASE WHEN SUM(v.amount) > 0 THEN SUM(v.amount) 
            ELSE 0 
           END) Debit , 
          (CASE WHEN SUM(v.amount) < 0 THEN SUM(-v.amount) 
            ELSE 0 
           END) Credit , 
          v.voucher_type_no , 
          r.status , 
          r.recon_date 'Clr_Date' , 
          MIN(v.voucher_id) Voucher_Id , 
          '' 'Party_Name' , 
          CONVERT(VARCHAR(10), r.recon_date, 126) 'Date YYYY-MM-DD' 
        FROM  voucher v 
          LEFT OUTER JOIN reconcilation r ON v.voucher_id = r.voucher_id 
          LEFT OUTER JOIN account a ON v.other_acno = a.account_no , 
          voucher_type vt 
        WHERE  v.voucher_type_no = vt.voucher_type_no 
          AND v.voucher_type_no > 0 
          AND v.other_acno = a.account_no 
          AND v.acc_year = 51 
          AND v.account_no = 10030 
          AND R.recon_date <= '2015-01-12' 
          AND R.recon_date >= '2009-04-01' 
          AND V.posted IN (1, 2) 
        GROUP BY v.voucher_no , 
          v.cheque_no , 
          v.date , 
          vt.voucher_type_shortname , 
          a.account_name , 
          v.voucher_type_no , 
          r.status , 
          r.recon_date 
       ) TAB 
+0

謝謝,我的不好,我跳過那部分。有時需要另一個人指出簡單的事情。 –