根据其他表行

时间:2016-07-12 07:47:05

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

我有以下表格和值,

Tb_name         column_name1    column_name2
Citator_KTLO_CC Date_Created    Date_Modified
Citator_KTLO_QA Date_Created    Date_Modified

我想从表中选择动态列,因此结果如下:

Select Date_Created,Date_Modified from Citator_KTLO_CC

并在下一个循环中选择第二行,如

Select Date_Created,Date_Modified from Citator_KTLO_QA

如何使用动态sql执行此操作?

感谢任何一个例子。

5 个答案:

答案 0 :(得分:2)

这是一个如何做到这一点的例子 由于你没有发布很多信息我只是假设包含所有表名的表被称为' tables'

此功能仅在所有表具有相同列类型时才有效。

-- create a test table you dont need this
create table tables (tb_name varchar(100) primary key, field1 varchar(100), field2 varchar(100))

-- fill my test table you dont need this
insert into tables values ('table1', 'field1', 'field2')
insert into tables values ('table2', 'foo1', 'foo2')
insert into tables values ('table3', 'test1', 'test2')

-- this is the actual code you need, replace the names with your real names
declare @sql varchar(max) = ''
declare @tb_name varchar(100) = ''
declare @field1 varchar(100) = ''
declare @field2 varchar(100) = ''

declare myCursor cursor for
  select tb_name, field1, field2 from tables -- dont know how your table is called

open myCursor
fetch next from myCursor into @tb_name, @field1, @field2
while @@FETCH_STATUS = 0
begin
  set @sql = @sql + ' select ' + @field1 + ', ' + @field2 + ' from ' +  @tb_name + ' union all '
  fetch next from myCursor into @tb_name, @field1, @field2
end
close myCursor
deallocate myCursor 

select @sql = left(@sql, len(@sql) - 10) 

exec (@sql)

修改 使用where子句是可能的,但事情会变得更复杂

declare @something date = getdate()

set @sql = @sql + ' select ' + @field1 + ', ' + @field2 + ' from ' +  @tb_name + ' where ' + @field1 + ' = ' + @something + ' union all '

您可以使用上面的示例构建您需要的内容。

修改 使用带有日期格式的where子句

declare @something date = getdate()

set @sql = @sql + ' select ' + @field1 + ', ' + @field2 + ' from ' +  @tb_name + ' where ' + @field1 + ' = ''' + CONVERT(varchar(8), @something, 112) + ''' union all '

答案 1 :(得分:0)

DECLARE @SQL VARCHAR(1000); 

SET @SQL = '

SELECT  *
FROM    Citator_KTLO_CC
UNION ALL
SELECT  *
FROM    Citator_KTLO_QA;'

EXEC (@SQL);

答案 2 :(得分:0)

试试这个.. 如果您在aloop中运行,则选择一行

DECLARE @sql NVARCHAR(4000)
SELECT @sql = ' select ' + column_name_1 + ',' + column_name2 + ' from ' + Tb_name
FROM < yourtable >
EXEC (@sql)

OR

DECLARE @sql NVARCHAR(4000)
SELECT @sql = 'union all select ' + column_name_1 + ',' + column_name2 + ' from ' + Tb_name
FROM < yourtable >


SET @sql =stuff(@sql,1,10,'')
EXEC (@sql)

答案 3 :(得分:0)

这样的事情怎么样?如果您有两个以上的cols,则可以使用动态sql生成cols列表,然后生成更多动态sql而不是硬编码。

DROP TABLE #Test

CREATE TABLE #Test 
(Tb_name  NVARCHAR(15),
 column_name1    NVARCHAR(12),
 column_name2 NVARCHAR(13));

 INSERT INTO #Test VALUES
('Citator_KTLO_CC','Date_Created','Date_Modified'),
('Citator_KTLO_QA','Date_Created','Date_Modified');

DECLARE @SQL  NVARCHAR(MAX)

SET @SQL = (SELECT STUFF((SELECT ' UNION ALL SELECT ' + Cols + ' FROM '+TbL 
 FROM (SELECT QUOTENAME(Tb_name) TBL,
      QUOTENAME(column_name1) + ', '+
      QUOTENAME(column_name2) Cols
      FROM #Test) Blah
 FOR XML PATH('')),1,10,''))

 PRINT @SQL

EXEC sys.sp_executesql @SQL

答案 4 :(得分:0)

DECLARE @ColumnList1 VARCHAR(MAX) = ''''''; 
DECLARE @ColumnList2 VARCHAR(MAX) = ''''''; 

DECLARE @ColumnNameFromTable1 VARCHAR(50);
DECLARE @ColumnNameFromTable2 VARCHAR(50);

DECLARE MyCursor1 CURSOR
FOR
    SELECT  COLUMN_NAME 
    FROM    INFORMATION_SCHEMA.COLUMNS
    WHERE   TABLE_NAME = 'Citator_KTLO_CC'
    ORDER BY ORDINAL_POSITION

DECLARE MyCursor2 CURSOR
FOR
    SELECT  COLUMN_NAME 
    FROM    INFORMATION_SCHEMA.COLUMNS
    WHERE   TABLE_NAME = 'Citator_KTLO_QA'
    ORDER BY ORDINAL_POSITION

OPEN MyCursor1
OPEN MyCursor2

FETCH NEXT FROM MyCursor1 INTO @ColumnNameFromTable1; 

WHILE @@FETCH_STATUS = 0
    BEGIN 

        FETCH NEXT FROM MyCursor2 INTO @ColumnNameFromTable2; 

        SET @ColumnList1 = @ColumnList1 + ',' + @ColumnNameFromTable1 
        SET @ColumnList2 = @ColumnList2 + ',' + @ColumnNameFromTable2 

        FETCH NEXT FROM MyCursor1 INTO @ColumnNameFromTable1; 
    END

CLOSE MyCursor1;
DEALLOCATE MyCursor1;

CLOSE MyCursor2;
DEALLOCATE MyCursor2;

EXEC ('SELECT ' + @ColumnList1 + ' FROM Citator_KTLO_CC UNION ALL SELECT ' + 

 @ColumnList2 + ' FROM Citator_KTLO_QA ')