sql查询字符串的问题

时间:2011-04-11 12:27:03

标签: asp.net sql sql-server

我在显示总计时遇到问题,收到错误

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

奇怪的是,这个查询在简单地查询数据库时工作得很好,但是当尝试在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

用于构建和执行查询的代码如下。

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

这是相当简单的,当点击页面上的按钮时(在从用户那里获取变量之后(为了说明目的而替换),会被触发

3 个答案:

答案 0 :(得分:1)

最终order by似乎在一个有趣的地方。它可能不是必需的。

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

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