merge将两个变量合并为一个变量sql

时间:2014-07-31 08:36:00

标签: sql sql-server sql-server-2008

我有两个变量说

DECLARE @ONE NVARCHAR(MAX)
DECLARE @TWO NVARCHAR(MAX)

我已将值设置为这两个变量

SET @ONE= 'select a from Table1 AS Result1'
SET @TWO = 'select b from Table2  AS Result2'

Result1     | Result2
  1         |  2
  3         |  4

现在我想将结果合并到一个变量

DECLARE @RESULTS NVARCHAR(MAX)
SET @RESULTS = result from @one + result from @two

我想要的输出是

---------------------
| Result1    Result2 |
|  1           2     |
|                    |
|   3          4     |
 ---------------------

这就是我试过的

SET @RESULTS = '
    select * 
    FROM (
        select * 
        from '+ @ONE+'
    ) RESULT1 JOIN (
        select * 
        from '+@TWO+'
    ) RESULT2 ON 1=1'

2 个答案:

答案 0 :(得分:1)

这应该有用。

测试数据:

IF OBJECT_ID(N'Table1')>0
BEGIN
    DROP TABLE Table1
END

IF OBJECT_ID(N'Table2')>0
BEGIN
    DROP TABLE Table2
END

CREATE TABLE Table1 (Col1 VARCHAR(100))
CREATE TABLE Table2 (Col2 VARCHAR(100))

INSERT INTO Table1
VALUES('1'),('3')

INSERT INTO Table2
VALUES('2'),('4')

<强>查询:

DECLARE @ONE NVARCHAR(MAX)
DECLARE @OneColumns NVARCHAR(MAX)
DECLARE @TWO NVARCHAR(MAX)
DECLARE @TWOColumns NVARCHAR(MAX)
DECLARE @SQL NVARCHAR(MAX)

DECLARE @Table1Result TABLE (ID INT IDENTITY(1,1),Column1 VARCHAR(100))
DECLARE @Table2Result TABLE (ID INT IDENTITY(1,1),Column2 VARCHAR(100))

SET @ONE = 'SELECT Col1 FROM Table1'
SET @TWO = 'SELECT Col2 FROM Table2'

SET @OneColumns = REPLACE(REPLACE(@ONE,'FROM','!'),'SELECT','')
SET @OneColumns = SUBSTRING(@OneColumns,1,CHARINDEX('!',@OneColumns)-1)
SET @OneColumns = LTRIM(RTRIM(@OneColumns))
SET @OneColumns = Replace(@OneColumns,',',', a.')
SET @OneColumns = 'a.'+@OneColumns

SET @TwoColumns = REPLACE(REPLACE(@TWO,'FROM','!'),'SELECT','')
SET @TwoColumns = SUBSTRING(@TwoColumns,1,CHARINDEX('!',@TwoColumns)-1)
SET @TwoColumns = LTRIM(RTRIM(@TwoColumns))
SET @TwoColumns = Replace(@TwoColumns,',',', b.')
SET @TwoColumns = 'b.'+@TwoColumns

SET @ONE = REPLACE(@ONE,'SELECT ', 'SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS TId, ')
SET @TWO = REPLACE(@TWO,'SELECT ', 'SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS TId, ')

Print @ONE
Print @Two
Print @OneColumns
Print @TwoColumns

SET @SQL = 'SELECT '+@OneColumns+','+@TwoColumns+CHAR(13)+CHAR(10)
+'FROM('+@ONE++')AS a'+CHAR(13)+CHAR(10)
+'INNER JOIN('+@Two+')AS b'+CHAR(13)+CHAR(10)
+'ON a.TId = b.TId'+CHAR(13)+CHAR(10)

EXEC (@SQL)

<强>清理:

IF OBJECT_ID(N'Table1')>0
BEGIN
    DROP TABLE Table1
END

IF OBJECT_ID(N'Table2')>0
BEGIN
    DROP TABLE Table2
END

答案 1 :(得分:0)

您可以将RowID添加到SELECT,然后添加INNER JOIN

CREATE TABLE #Table1  (a INT)
CREATE TABLE #Table2  (b INT)

INSERT INTO #Table1 SELECT 1
INSERT INTO #Table1 SELECT 3

INSERT INTO #Table2 SELECT 2
INSERT INTO #Table2 SELECT 4

DECLARE @ONE NVARCHAR(MAX)
DECLARE @TWO NVARCHAR(MAX)

SET @ONE= 'select a, ROW_NUMBER() OVER (ORDER BY a) AS ROWID from #Table1 '
SET @TWO = 'select b, ROW_NUMBER() OVER (ORDER BY b) AS ROWID from #Table2  '


DECLARE @RESULTS NVARCHAR(MAX) = '
    select a, b 
    FROM (
        '+ @ONE+' 
    ) RESULT1 
    INNER JOIN (
        '+@TWO+'
    ) RESULT2 ON 
    RESULT1.ROWID=RESULT2.ROWID'


EXEC sp_executesql @RESULTS