1

我是 SQL Server 的初学者

我在 SAP B1 中的查询是

  DECLARE @FROMDATE AS DATETIME
  SET @FROMDATE= [%0]

  DECLARE @TODATE AS DATETIME
  SET @TODATE= [%1]

  DECLARE @ACCNUM AS NVARCHAR(100)
  SET @ACCNUM= [%2]

获取账户每笔交易的账户名称、编号

   SELECT 
   T4.[ACCOUNT] AS 'A/C NUM',
   T4.[ACCTNAME] AS 'A/C NAME',
   T4.[TransId] AS 'KEY',

   SUM(VEL_DB) AS 'VEL DB',
   SUM(VEL_CR) AS 'VEL CR',

   SUM(DEF_DB) AS 'DEF_DB',
   SUM(DEF_CR) AS 'DEF CR',
   T4.[LINEMEMO],
   T1.[U_Naaration]


   FROM OJDT T1 

根据 ocrcode2 计算账户中的每笔交易的贷方和借方

   INNER JOIN 
      (
     SELECT T2.[TRANSID],T2.[ACCOUNT],T3.[ACCTNAME],T2.[LINEMEMO],

     case
     when T2.[OcrCode2]='VEL'
     THEN
       (T2.[Debit]) 
     ELSE
       NULL
     END as VEL_DB,

    case
    when T2.[OcrCode2]='VEL'
    THEN
     (T2.[CREDIT]) 
    ELSE
     NULL
    END as VEL_CR,

   case
   when T2.[OcrCode2]='' OR T2.[OCRCODE2] IS NULL
   THEN
     (T2.[Debit]) 
   ELSE
     NULL
   END as DEF_DB,

   case
   when T2.[OcrCode2]='' OR T2.[OCRCODE2]IS NULL
   THEN
       (T2.[CREDIT]) 
   ELSE
     NULL
   END as DEF_CR


   FROM JDT1 T2 
      INNER JOIN OACT T3 ON T3.[ACCTCODE]=T2.[ACCOUNT]
    where T2.[DUEDATE]>=@FROMDATE AND T2.[DUEDATE]<=@TODATE ) 
    T4 ON T4.TRANSID = T1.TRANSID AND T4.[ACCOUNT]=@ACCNUM


  GROUP BY T4.[TransId] ,T1.[U_Naaration],T4.[ACCTNAME],T4.[ACCOUNT],T4.[LINEMEMO]

   FOR BROWSE

我收到错误

必须选择要从中选择的表

4

1 回答 1

0

根据此处Petr Verner 的正确答案,您可能需要重写查询以避免使用子查询。

答案还继续建议选项:

  1. 将查询嵌入到存储过程中并改为调用该 SP。

  2. 使用视图。(您可以将它用于子查询部分或整个查询)。

  3. 将子查询的结果存储到表变量(或临时表)中,然后在主查询中使用保存的结果集。

于 2012-07-05T12:28:36.690 回答