2011-04-11 173 views
0

我有一個問題,顯示的總和,得到錯誤問題的SQL查詢字符串

Line 1: Incorrect syntax near '('. 

我使用的查詢如下;

SELECT  
SUM(a_count) AS total 

FROM 
    (
    SELECT 
     TOP (100) PERCENT CONVERT(varchar, dt_created, 106) AS dt_created, 
     COUNT(*) AS a_count   
    FROM 
     fulf AS a 
    WHERE 
     (source = '10wnt47') 
     AND (dt_created >= '01 jan 2011') 
     AND (dt_created <= '11 apr 2011') 
    GROUP BY CONVERT(varchar, dt_created, 106) 
    ) 
AS b 

奇怪的是,這個查詢工作完美時只需quering數據庫,而是試圖似乎翻倒一個aspx頁面中使用它的時候。我已經使用查詢(沒有生成的總和),它工作正常。這可能是一些很明顯,我很想念,但我不能看到它... ...

的堆棧跟蹤如下...

[SqlException (0x80131904): Line 1: Incorrect syntax near '('. 
Incorrect syntax near the keyword 'AS'.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194 
    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392 
    System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33 
    System.Data.SqlClient.SqlDataReader.get_MetaData() +83 
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297 
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 
    System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141 
    System.Data.SqlClient.SqlCommand.ExecuteReader() +89 
    _Default.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\amayberry\My Documents\Visual Studio 2008\WebSites\chart_test\iframe2.aspx.vb:95 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 

使用來構建和執行查詢是作爲代碼IM如下。

Using myConnection As New SqlConnection 
      myConnection.ConnectionString = ConfigurationManager.ConnectionStrings("VisitScotlandConnectionString").ConnectionString 

      Dim mySelect As New SqlCommand 
      mySelect.Connection = myConnection 
      mySelect.CommandText = "SELECT SUM(a_count) AS total FROM (SELECT TOP (100) PERCENT CONVERT(varchar, dt_created, 106) AS dt_created, COUNT(*) AS a_count FROM fulf AS a WHERE (source = '10wnt47') AND (dt_created >= '01 jan 2011') AND (dt_created <= '11 apr 2011') GROUP BY CONVERT(varchar, dt_created, 106)) AS b" 
      myConnection.Open() 
      Dim myReader As SqlDataReader = mySelect.ExecuteReader() 

      If myReader.Read() Then 
       total = myReader("total").ToString() 
       Response.Write(total) 
      End If 

      myReader.Close() 
      myConnection.Close() 
     End Using 

這是非常簡單,點擊頁面上的按鈕時會觸發(從用戶(我已經取代了用於說明目的)

+0

你嘗試運行這種方式,使用常數而不是動態的用戶變量和你會得到同樣的錯誤? – 2011-04-11 15:56:51

+0

我做過了,這是我試圖排除的第一件事情之一,它是導致問題的變量,與約束或變量相同的錯誤 – Allan 2011-04-11 16:03:27

+0

抱歉。我不知道爲什麼查詢會自行運行,並且在asp.net中調用相同的查詢時無法正常工作。我從來沒有遇到過這個問題。 – 2011-04-11 17:16:01

回答

0

嗯,我結束了再殺這一點,重新編寫代碼,它似乎做工精細與此版本的代碼:

SELECT 
    COUNT(*) AS Total FROM dbo.fulf 
WHERE 
     (source = '" & vSource & "') 
    AND 
     (dt_created >= '" & Format(vStartDate, "dd MMM yyyy") & "') 
    AND 
     (dt_created <= '" & Format(vEndDate, "dd MMM yyyy") & "') 
ORDER BY 
    Total DESC 
1

最終order by似乎是一個有趣的獲取變量後地方它可能不需要

+0

我已經嘗試刪除這個(它是從以前的查詢是多餘的,所以不需要),但得到錯誤附近語法錯誤'('。第1行:'a_1'附近的語法不正確。 – Allan 2011-04-11 14:06:33

1

我相信你需要刪除「AS」你的表格名稱之後的條款所以他們應該是(注意,我換成你的一些變量名):。

SELECT SUM(TotalCount) AS total 
FROM 
    (
     SELECT 
      TOP (100) PERCENT CONVERT(varchar, dt_created, 106) AS dt_created, 
      cnt AS TotalCount 
     FROM 
      (
       SELECT 
        CONVERT(varchar, dt_created, 106) AS dt_created, 
        COUNT(*) AS cnt 
       FROM 
        dbo.fulf a 
       WHERE 
        (source = 'thing') 
        AND (dt_created >= 'date') 
        AND (dt_created <= 'date') 
       GROUP BY 
        CONVERT(varchar, dt_created, 106) 
      ) a_1 
     ORDER BY 
      CONVERT(datetime, dt_created, 106) 
    ) b 
+1

也可能是一個問題你正在使用「count」作爲標識符,因爲「count」是一個保留字,你應該改變它。 – 2011-04-11 12:45:09

+0

感謝您的幫助,現在我得到以下錯誤第1行:'('附近的語法錯誤。 第1行:'a_1'附近的語法錯誤 – Allan 2011-04-11 12:54:37

+0

在查詢存儲在變量中但在執行之前,您是否曾嘗試在代碼中放置斷點以驗證變量是否具有您期望的內容?的代碼,但不是在代碼中,可能的問題是您正在加載的動態變量。 – 2011-04-11 12:57:23