子查询无法正常工作

时间:2015-01-12 08:35:13

标签: sql sql-server tsql subquery

我试图在另一个临时表中插入子查询的结果。 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
                )

1 个答案:

答案 0 :(得分: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