我有一個問題,顯示的總和,得到錯誤問題的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
這是非常簡單,點擊頁面上的按鈕時會觸發(從用戶(我已經取代了用於說明目的)
你嘗試運行這種方式,使用常數而不是動態的用戶變量和你會得到同樣的錯誤? – 2011-04-11 15:56:51
我做過了,這是我試圖排除的第一件事情之一,它是導致問題的變量,與約束或變量相同的錯誤 – Allan 2011-04-11 16:03:27
抱歉。我不知道爲什麼查詢會自行運行,並且在asp.net中調用相同的查詢時無法正常工作。我從來沒有遇到過這個問題。 – 2011-04-11 17:16:01