关键字 UNION 附近的语法不正确

Posted

技术标签:

【中文标题】关键字 UNION 附近的语法不正确【英文标题】:Incorrect Syntax near the keyword UNION 【发布时间】:2010-09-30 10:07:48 【问题描述】:

我收到一条错误消息,显示“。

目标站点是-

Void ProcessResults(System.Data.OleDb.OleDbHResult)

堆栈跟踪如下:-

在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,String 方法) 在 System.Data.OleDb.OleDbDataReader.NextResult() 的 System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr) .OleDb.OleDbCommand.ExecuteReader(CommandBehavior 行为)在 System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.FillInternal(DataSet 数据集,DataTable[] 数据表,Int32 System.Data.Common.DbDataAdapter.Fill(DataSet 数据集,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand 命令,CommandBehavior 行为)在 System.Data.Common 的 startRecord、Int32 maxRecords、String srcTable、IDbCommand 命令、CommandBehavior 行为)。 DbDataAdapter.Fill(DataSet dataSet) at MFActivityPlanning.NewStuffS.PopulateNewStuffsDataGrid()

查询如下:

 strSelectQuery = "SELECT LED_ID AS PK_ID, FIRST_NAME + ' ' + LAST_NAME AS NAME"
                        + " FROM M_LEADERLED INNER JOIN M_USER_DETAILS"
                        + " ON M_LEADERLED.LED_ID = M_USER_DETAILS.PK_ID"
                        + " WHERE (M_LEADERLED.LEADER_ID = " + Session["UserID"].ToString() + ""
                        + " AND M_USER_DETAILS.ACTIVE = 1 AND M_LEADERLED.START_DATE <= Getdate()"
                        + " AND M_LEADERLED.END_DATE > Getdate())"
                        + " UNION SELECT PK_ID, FIRST_NAME + ' ' + LAST_NAME AS NAME"
                        + " FROM M_USER_DETAILS WHERE PK_ID = " + Session["UserID"].ToString() + ";";



  // Query for Idea
            string selectIdea = "SELECT MWL.PK_ID AS PK_ID, 'Idea'+ ': ' + MI.TITLE AS TITLE,"
                        + " 'Idea.aspx?id=' + CONVERT(VARCHAR, MI.PK_ID) AS OBJECTURL"
                        + " FROM M_WATCHLIST MWL INNER JOIN M_IDEAS MI ON MWL.FK_OBJECT_ID = MI.PK_ID"
                        + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                        + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Idea) + ""
                        + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";
        // Query for Articles
        string selectArticle = "SELECT MWL.PK_ID AS PK_ID, 'Articles' + ': ' + MA.TITLE AS TITLE,"
                    + " 'Articles.aspx?id=' + CONVERT(VARCHAR, MA.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_ARTICLES MA ON MWL.FK_OBJECT_ID = MA.PK_ID"
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Article) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MA.IS_ACTIVE = 1";

        // Query for Tips
        string selectTips = "SELECT MWL.PK_ID AS PK_ID, 'Tips' + ': ' + MT.TITLE AS TITLE,"
                    + " 'Tips.aspx?id=' + CONVERT(VARCHAR, MT.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_TIPS MT ON MWL.FK_OBJECT_ID = MT.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Tip) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MT.IS_ACTIVE = 1";

        // Query for IWantINeed
        string selectIWantINeed = "SELECT MWL.PK_ID AS PK_ID, 'IWantINeed' + ': ' + MWN.TITLE AS TITLE,"
                    + " 'IWantINeed.aspx?id=' + CONVERT(VARCHAR, MWN.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_I_WANT_I_NEED MWN ON MWL.FK_OBJECT_ID = MWN.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.IWantINeed) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for PointCAT
        string selectPointCAT = "SELECT MWL.PK_ID AS PK_ID, 'PointCAT' + ': ' + MPS.TITLE AS TITLE,"
                    + " 'PointCAT.aspx?id=' + CONVERT(VARCHAR, MPS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_POINTCAT_SESSIONS MPS ON MWL.FK_OBJECT_ID = MPS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.PointCAT) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MPS.IS_ACTIVE = 1";

        // Query for Seminar
        string selectSeminar = "SELECT MWL.PK_ID AS PK_ID, 'Seminar' + ': ' + MS.TITLE AS TITLE,"
                    + " 'Seminar.aspx?id=' + CONVERT(VARCHAR, MS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_SEMINAR MS ON MWL.FK_OBJECT_ID = MS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Seminar) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MS.IS_ACTIVE = 1";

        // Query for CodeCAT
        string selectCodeCAT = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Code Review Feedback:"
                    + " ' + MUD1.FIRST_NAME + ' ' + MUD1.LAST_NAME AS TITLE,"
                    + " 'CodeCatFeedback.aspx?id=' + CONVERT(VARCHAR, MCS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_CODECAT_SESSIONS MCS ON MWL.FK_OBJECT_ID = MCS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " INNER JOIN M_USER_DETAILS MUD1 ON MUD1.PK_ID = MCS.REVIEW_MEMBER"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.CodeCatFeedback) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MCS.ACTIVE = 1";

        // Query for NowShowing
        string selectNowShowing = "SELECT MWL.PK_ID AS PK_ID, 'NowShowing' + ': ' + MNS.TITLE AS TITLE,"
                    + " 'NowShowing.aspx?id=' + CONVERT(VARCHAR, MNS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_NOW_SHOWING MNS ON MWL.FK_OBJECT_ID = MNS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.NowShowing) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MNS.IS_ACTIVE = 1";

        // Query for QA
        string selectQA = "SELECT MWL.PK_ID AS PK_ID, 'Q&A' + ': ' + MQ.TITLE AS TITLE,"
                   + " 'QA.aspx?id=' + CONVERT(VARCHAR, MQ.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_QA MQ ON MWL.FK_OBJECT_ID = MQ.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.QA) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MQ.IS_ACTIVE = 1";

        // Query for Message
        string selectMessage = "SELECT MWL.PK_ID AS PK_ID, 'Message' + ': ' + MS.SUBJECT AS TITLE,"
                   + " 'Message.aspx?id=' + CONVERT(VARCHAR, MS.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_MESSAGE MS ON MWL.FK_OBJECT_ID = MS.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Message) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MS.IS_ACTIVE = 1";

        // Query for Documents
        string selectDocuments = "SELECT MWL.PK_ID AS PK_ID, 'Documents' + ': ' + MSV.TITLE AS TITLE,"
                   + " 'Document.aspx?id=' + CONVERT(VARCHAR, MSD.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_SHARE_DOCUMENT MSD ON MWL.FK_OBJECT_ID = MSD.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " INNER JOIN M_SHARE_DOC_VERSION MSV ON MSV.FK_DOC_ID = MSD.PK_ID AND MSV.IS_ACTIVE =1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Document) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MSD.IS_ACTIVE = 1";

        // Query for Candidate
        string selectCandidate = "SELECT MWL.PK_ID AS PK_ID, 'Candidate' + ': ' + MC.CAND_NAME AS TITLE,"
                   + " 'Candidates.aspx?id=' + CONVERT(VARCHAR, MC.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_CANDIDATE MC ON MWL.FK_OBJECT_ID = MC.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Candidate) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for Prospect
        string selectProspect = "SELECT MWL.PK_ID AS PK_ID, 'Prospect' + ': ' + MP.PROSPECT_NAME AS TITLE,"
                   + " 'Prospect2.aspx?id=' + CONVERT(VARCHAR, MP.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_PROSPECT MP ON MWL.FK_OBJECT_ID = MP.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Prospect) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for ToolCAT
        string selectToolCAT = "SELECT MWL.PK_ID AS PK_ID, 'ToolCAT' + ': ' + MT.TOOL_NAME AS TITLE,"
                    + " 'Catalyst/ToolCAT.aspx?id=' + CONVERT(VARCHAR, MT.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_TOOLCAT MT ON MWL.FK_OBJECT_ID = MT.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.ToolCAT) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MT.IS_ACTIVE = 1";

string selectLearningMatter = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Learning Matter: ' + KMLM.TITLE AS TITLE,"
                        + " 'KLMS/LearningMatterView.aspx?id=' + CONVERT(VARCHAR, KMLM.PK_ID) AS OBJECTURL"
                        + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_LEARNING_MATTER KMLM"
                        + " ON MWL.FK_OBJECT_ID = KMLM.PK_ID INNER JOIN M_USER_DETAILS MUD"
                        + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                        + " WHERE MWL.OBJECT_TYPE = 505 AND MWL.FK_USER_ID = " + userId + " AND"
                        + " MWL.IS_ACTIVE = 1 AND KMLM.IS_ACTIVE = 1";
        // Query for Questions
        string selectQuestions = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Questions: ' + CONVERT(VARCHAR(400),"
                   + " [QUESTION]) AS [TITLE],"
                   + " 'KLMS/Question.aspx?id=' + CONVERT(VARCHAR, KMQ.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_QUESTIONS KMQ"
                   + " ON MWL.FK_OBJECT_ID = KMQ.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 510 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMQ.IS_ACTIVE = 1";

        // Query for Test
        string selectTest = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Tests: ' + KMT.TITLE AS TITLE,"
                   + " 'KLMS/Test.aspx?id=' + CONVERT(VARCHAR, KMT.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_TESTS KMT"
                   + " ON MWL.FK_OBJECT_ID = KMT.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 515 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMT.IS_ACTIVE = 1";

        // Query for Course
        string selectCourse = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Courses: ' + KMC.TITLE AS TITLE,"
                   + " 'KLMS/Course.aspx?id=' + CONVERT(VARCHAR, KMC.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_COURSES KMC"
                   + " ON MWL.FK_OBJECT_ID = KMC.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 500 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMC.IS_ACTIVE = 1";

        //************************************************************************************************

        string orderQuery = " ORDER BY " + GridViewSortExpression + " " + GridViewSortDirection;

        //For All
        if (ddlTypes.SelectedValue == "10")
        
            selectStatement = selectArticle + " UNION " + selectTips + " UNION " + selectIdea
                    + " UNION " + selectIWantINeed + " UNION " + selectPointCAT + " UNION " + selectSeminar
                    + " UNION " + selectCodeCAT + " UNION " + selectNowShowing + " UNION " + selectQA
                    + " UNION " + selectMessage + " UNION " + selectDocuments + " UNION " + selectCandidate
                    + " UNION " + selectProspect + " UNION " + selectToolCAT;

谁能帮帮我....

【问题讨论】:

错误在你的sql语句中,你能显示你用来获取数据的sql语句吗? ps 所有这些错误都是由 oledbDatareader 产生的 向我们展示您的查询。问题似乎出在查询上。 向我们展示你的 sql 语句:它看起来像:select id, name from tablename 你能把“selectStatement”变量的值复制到这里,就在添加了所有UNION的命令之后吗? 【参考方案1】:

相当大的查询。我会在ifwatch 中添加一个breakpoint(F9) selectStatement;将查询从那里复制到我的 sql 编辑器中,然后从那里运行/修复它。

您似乎在UNION 语句中缺少一些空格或列。

【讨论】:

以上是关于关键字 UNION 附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章

错误“关键字 'CONVERT' 附近的语法不正确”

关键字“read”附近的语法不正确

关键字“或”附近的语法不正确

在关键字“pivot”附近出现语法错误,提示语法不正确。我在这里想念啥

SQL错误关键字'user'附近的语法不正确[重复]

关键字“JOIN”附近的语法不正确。使用 asp.net [重复]