子查询无法正常工作

Sub Query not working properly

我正在尝试将子查询的结果插入另一个临时 table。 SubQuery 在我只执行它时工作正常,但在作为子查询时却没有。它抛出语法错误。 错误是:“)”附近的语法不正确。它在子查询的第一行

    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
                )

在最后一个括号后使用别名

    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