简体   繁体   English

SQL Server中关键字“ FROM”附近的语法不正确

[英]Incorrect syntax near the keyword 'FROM' in SQL Server

Please help me in the code, I get an error 请帮助我的代码,我得到一个错误

Incorrect syntax near the keyword 'FROM' 关键字“ FROM”附近的语法不正确

SELECT 
    produkt.Twr_Kod as kod,
    (SELECT
         ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0) 
     FROM
         cdn.TraSElem 
     WHERE
         trs_typ = 3 AND TrS_TrEIdWydania = 0 
         AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId 
         AND zasoby.TwZ_MagId = 1
     FROM 
         CDN.TwrZasoby as zasoby
     WHERE 
         zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
    CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
    CDN.Towary AS product
LEFT JOIN 
    CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY 
    product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY 
    kod

SQLs kinda need to look like this: SQL有点像这样:

SELECT columns FROM tables_or_queries WHERE predicates GROUP BY grouping_keys ORDER BY columns

This pattern can be nested both inside the SELECT region and the FROM region: 此模式可以嵌套在SELECT区域和FROM区域内:

SELECT
  column1,
  (SELECT columnA FROM table WHERE where_predicates) as column2,
  column3
FROM
  table
  INNER JOIN
  (SELECT columnA, columnB FROM table WHERE where_predicates) as nested_query
  ON join_predicates
WHERE
  where_predicates

But you can't have multiple FROM regions etc.. To get more targeted advice youre going to have to tell us what you're hoping your query does.. Right now, we can only say to sort out the basic syntax errors the compilation process is complaining about 但是您不能有多个FROM区域等。要获得更有针对性的建议,您将不得不告诉我们您希望查询执行的操作。.现在,我们只能说要整理出基本语法错误过程在抱怨

You seem to have multiple FROM statements in a single SELECT statement. 您似乎在单个SELECT语句中有多个FROM语句。 Every SELECT can have only one FROM clause. 每个SELECT只能有一个FROM子句。 Glancing through your query, I think it's going to give you performance nightmares. 浏览一下您的查询,我认为这会给您带来性能方面的噩梦。 Use joins instead of using inline queries. 使用联接而不是使用内联查询。 Will improve performance. 将提高性能。

SELECT 
    produkt.Twr_Kod as kod,
    (SELECT
         ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0) 
     **FROM
         cdn.TraSElem 
     WHERE
         trs_typ = 3 AND TrS_TrEIdWydania = 0 
         AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId 
         AND zasoby.TwZ_MagId = 1
     FROM 
         CDN.TwrZasoby as zasoby**
     WHERE 
         zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
    CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
    CDN.Towary AS product
LEFT JOIN 
    CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY 
    product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY 
    kod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM