這看起來很有趣。雖然在我的回答中遲到了,但我嘗試了下面的看法,看看我能不能提出一種方法來做到這一點。它需要更多的工作,但我相信通過使用這種格式,你可以提出一個可行的解決方案。
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
我想你必須自己做。 –
爲具有一些功能的複雜查詢編寫SQL2XML轉換器將會很複雜 – Jay
@Jayachandran,爲什麼要這樣做?如果您想要將XML發送到服務器,將其轉換回SQL並執行該SQL,爲什麼不直接發送SQL? – svick