用于将SQL(SELECT查询)转换为XML FORM的API或工具

时间:2011-07-04 09:47:14

标签: sql xml

需要帮助将SQL查询转换为某种标准XML格式。

例如,假设我有一个查询“SELECT A,B,C FROM XYZ WHERE B = C AND A> 2”

我应该得到结果

<QUERY>
<SELECT>
    <COLUMNS>
        <COLUMN>A</COLUMN>
        <COLUMN>B</COLUMN>
        <COLUMN>B</COLUMN>
    </COLUMNS>
</SELECT>
<FROM>
    <TABLES>
    </TABLES>
</FROM>
<WHERE>
    <CONDITIONS>
        <AND>
            <CONDITION>
                <EQUALS>
                <VARIABLE>B</VARIABLE>
                <VARIABLE>C</VARIABLE>
                </EQUALS>
            </CONDITION>
            <CONDITION>
                <GREATER>
                <VARIABLE>A</VARIABLE>
                <VARIABLE>2</VARIABLE>
                </GREATER>
            </CONDITION>
        </AND>
    </CONDITIONS>
</WHERE>
</QUERY>

请向我推荐一些可以为我提供此XML结果的API或工具。

3 个答案:

答案 0 :(得分:1)

在codeproject SQL Parser中查看此链接 它给你一个好主意,

答案 1 :(得分:1)

这看起来很有趣。虽然在我的回复中,我尝试了以下内容,看看我是否能够提出一种方法来做到这一点。它需要更多的工作,但我相信通过使用这种格式,你可以提出一个可行的解决方案。

    SET NOCOUNT ON

    ------------------------------------------------------------------------------
    ----------- DECLARE VARIABLES AND TABLES -------------------------------------
    ------------------------------------------------------------------------------
    DECLARE @QueryID                  INT
    DECLARE @SelectID                 INT
    DECLARE @ColumnsBeginID           INT
    DECLARE @ColumnsEndID             INT
    DECLARE @FromID                   INT
    DECLARE @TablesBeginID            INT
    DECLARE @TablesEndID              INT
    DECLARE @WhereID                  INT
    DECLARE @ConditionsBeginID        INT
    DECLARE @ConditionsEndID          INT

    DECLARE @XMLQuery                 XML = '<QUERY></QUERY>'


    DECLARE @Query TABLE
    (
          QueryID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [SELECT]                    INT             NOT NULL    
        , [FROM]                      INT             NOT NULL    
        , [WHERE]                     INT                 NULL      
    )

    DECLARE @Select TABLE
    (
          SelectID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [COLUMNS_BEGIN]             INT             NOT NULL    
        , [COLUMNS_END]               INT             NOT NULL    
    )

    DECLARE @Column TABLE
    (
          ColumnID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Column]                    SYSNAME         NOT NULL
        , [TableAlias]                SYSNAME         NOT NULL
        , [ColumnAlias]               SYSNAME         NOT NULL
    )

    DECLARE @From TABLE
    (
          FromID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [TABLES_BEGIN]              INT             NOT NULL    
        , [TABLES_END]                INT             NOT NULL    
    )

    DECLARE @Table TABLE
    (
          TableID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Join]                       SYSNAME         NOT NULL    
        , [Tablename]                  SYSNAME         NOT NULL    
        , [TableAlias]                 SYSNAME         NOT NULL    
        , [On]                         SYSNAME         NOT NULL    
    )

    DECLARE @Where TABLE
    (
          WhereID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [CONDITIONS_BEGIN]          INT             NOT NULL    
        , [CONDITIONS_END]            INT             NOT NULL    
    )

    DECLARE @Condition TABLE
    (
          ConditionID                 INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [AndOr]                     SYSNAME         NOT NULL    
        , [Equality]                  SYSNAME         NOT NULL
        , [Variable1]                 SYSNAME         NOT NULL    
        , [Variable2]                 SYSNAME         NOT NULL    
    )

    ------------------------------------------------------------------------------
    ----------- USERS QUERY HERE -------------------------------------------------
    ------------------------------------------------------------------------------

    --SELECT A, B, C FROM XYZ WHERE B = C AND A > 2

    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('A', 'x', 'a')
    SET @ColumnsBeginID = @@IDENTITY 
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('B', 'x', 'b')
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('C', 'x', 'c')
    SET @ColumnsEndID = @@IDENTITY 

    INSERT INTO @Select ([COLUMNS_BEGIN], [COLUMNS_END]) VALUES (@ColumnsBeginID, @ColumnsEndID)
    SET @SelectID = @@IDENTITY


    INSERT INTO @Table ([Join], [Tablename], [TableAlias], [On]) VALUES ('FROM', 'XYZ', 'x', '')
    SET @TablesBeginID = @@IDENTITY
    SET @TablesEndID = @TablesBeginID

    INSERT INTO @From ([TABLES_BEGIN], [TABLES_END]) VALUES (@TablesBeginID, @TablesEndID)
    SET @FromID = @@IDENTITY


    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','EQUALS','B','C')
    SET @ConditionsBeginID = @@IDENTITY
    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','GREATER','A','2')
    SET @ConditionsEndID = @@IDENTITY

    INSERT INTO @Where ([CONDITIONS_BEGIN], [CONDITIONS_END]) VALUES (@ConditionsBeginID, @ConditionsEndID)
    SET @WhereID = @@IDENTITY


    INSERT INTO @Query ([SELECT], [FROM], [WHERE]) VALUES (@SelectID, @FromID, @WhereID)


    ------------------------------------------------------------------------------ 
    ----------- BUILD THE @XMLQuery VARIABLE ------------------------------------- 
    ------------------------------------------------------------------------------ 

    SET @XMLQuery = (SELECT SelectColumn            = c.[Column]
                          , SelectColumnTableAlias  = c.[TableAlias]
                          , SelectColumnColumnAlias = c.[ColumnAlias]

                          , FromTableJoin           = t.[Join]
                          , FromTable               = t.[Tablename]
                          , FromTableAlias          = t.[TableAlias]
                          , FromOn                  = t.[On]

                          , WhereConditionAndOr     = n.[AndOr]     
                          , WhereConditionEquaility = n.[Equality]  
                          , WhereConditionVariable1 = n.[Variable1] 
                          , WhereConditionVariable2 = n.[Variable2] 

                       FROM  @Query     q
                       JOIN  @Select    s  ON s.SelectID    = q.[SELECT]
                       JOIN  @From      f  ON f.FromID      = q.[FROM]
                       JOIN  @Where     w  ON w.WhereID     = q.[WHERE]
                       JOIN  @Column    c  ON c.ColumnID    BETWEEN s.[COLUMNS_BEGIN]    AND s.[COLUMNS_END]
                       JOIN  @Table     t  ON t.TableID     BETWEEN f.[TABLES_BEGIN]     AND f.[TABLES_END]
                       JOIN  @Condition n  ON n.ConditionID BETWEEN w.[CONDITIONS_BEGIN] AND w.[CONDITIONS_END]

                   ORDER BY c.ColumnID
                          , t.TableID
                          , n.ConditionID
                        FOR XML AUTO, ROOT ('QUERY') 
                        --FOR XML RAW, ROOT ('QUERY') 
                        --FOR XML PATH, ROOT ('QUERY') 
                      )

    SELECT @XMLQuery

答案 2 :(得分:0)