2011-07-04 49 views
1

需要將SQL查詢轉換爲某種標準XML格式的幫助。將SQL(SELECT查詢)轉換爲XML的API或工具FORM

例如,可以說我有一個查詢 「選擇A,B,C從XYZ其中B = C和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> 

請建議我的一些API或工具可以提供給我這個XML結果。

+1

我想你必須自己做。 –

+0

爲具有一些功能的複雜查詢編寫SQL2XML轉換器將會很複雜 – Jay

+0

@Jayachandran,爲什麼要這樣做?如果您想要將XML發送到服務器,將其轉換回SQL並執行該SQL,爲什麼不直接發送SQL? – svick

回答

0

,看一下

​​

+0

你介意在該鏈接的相關部分擴展該答案嗎? – RichardTheKiwi

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 
+0

對不起,延遲迴復...感謝Greg!非常有趣的解決方案來管理通過SQL轉換。我通過Java代碼轉換SQL2XML。感謝您回答問題的時間。 – Jay