如何在sql中获取表名和一列?

时间:2022-09-23 21:36:38

I have few tables in my database. I want to get all table names along with one field i.e last_timestamp which is common in every table. I know the table names

我的数据库中有几张表。我想得到所有的表名和一个字段,即last_timestamp,这在每个表中都很常见。我知道表名

I tried following query but its giving me only one table and I am unable to write query for my requirement

我尝试了以下查询,但它只给了我一个表,我无法为我的要求编写查询

SELECT t.table_name, MAX(c.last_timestamp) FROM information_schema.tables t, city c WHERE t.table_name='city' or t.table_name  ='city_area' and TABLE_SCHEMA='my_db';

this gives me

这给了我

'city', '2016-05-13 15:08:07',

I think I need dynamic alias for tables. But I dont understand how to to it. Is it possible?

我想我需要表的动态别名。但我不明白该怎么做。可能吗?

3 个解决方案

#1


0  

Please Try the below code to get the desired output.

请尝试以下代码以获得所需的输出。

--Creating Two Temp tables
CREATE TABLE #Temp1
( 
ID int IDENTITY(1,1),
TableName Varchar(50)
)

CREATE TABLE #Temp2
( 
TableName Varchar(50),
last_timestamp Date
)
--Inserting the required tables into Temp table 1, From information_schema
INSERT INTO #Temp1
SELECT t.table_name AS TableName FROM information_schema.tables t WHERE    t.table_name IN ('Table1','Table2') AND TABLE_SCHEMA='my_db'
--Below code gives the desired output.
DECLARE @Count INT
DECLARE @Count1 INT
DECLARE @Query nvarchar(max)
DECLARE @TableVariable Varchar(100)
SET @Count1 =0
SET @Count =(SELECT count(ID) FROM #Temp1)
WHILE (@Count1<@Count) 
BEGIN
   SET @Count1 =(@Count1 +1)
   SET @TableVariable = (SELECT TableName FROM #Temp1 WHERE ID =@Count1)

   SET @Query = 'SELECT t.table_name AS TableName,max(c.last_timestamp)AS  last_timestamp FROM information_schema.tables t,' +@TableVariable+ ' c
              WHERE t.table_name IN ('''+@TableVariable+''') GROUP BY   t.table_name'
    INSERT  INTO #Temp2 EXECUTE SP_EXECUTESQL @Query
END
SELECT * FROM #Temp2

#2


0  

use AdventureWorks2012
go
declare @sql varchar(max) = ''
declare @table varchar(max)
declare sqlcursor cursor for
SELECT  t.TABLE_SCHEMA + '.' + c.table_name
        --,c.column_name 
from    [INFORMATION_SCHEMA].[TABLES] t
join    [INFORMATION_SCHEMA].[COLUMNS] c on c.table_name = t.table_name
WHERE   COLUMN_NAME = 'ModifiedDate'
OPEN    sqlcursor   
FETCH NEXT FROM sqlcursor INTO @table   
set @sql = 'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
WHILE @@FETCH_STATUS = 0   
BEGIN   
       FETCH NEXT FROM sqlcursor INTO @table 
       IF @@FETCH_STATUS = 0   
       set @sql = @sql + ' union ' +  'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
END   

CLOSE sqlcursor   
DEALLOCATE sqlcursor

EXEC (@sql)

#3


0  

Assume the tables to be Q37290859Table1 and Q37290859Table2, each with Id and DateColumn common to both. Then you can use simple while loop (thus avoiding cursor) to build a final query.

假设表格为Q37290859Table1和Q37290859Table2,每个表格都包含Id和DateColumn。然后你可以使用简单的while循环(从而避免游标)来构建最终查询。

declare @sql varchar(max)
declare @table varchar(20)
declare @column varchar(20)
declare @tableList table 
(
    table_id int identity(1,1), 
    table_name varchar(30), 
    column_name varchar(30)
)

insert into @tableList 
    select t.table_name, c.column_name 
    from information_schema.tables t
    inner join information_schema.columns c on t.table_name = c.table_name
    where c.column_name = 'DateColumn'

declare @ctr int = 1
declare @ctr_max int = (select max(table_id) from @tableList)
set @sql = ''

while @ctr <= @ctr_max
begin
    select top 1 @table = table_name, @column = column_name from @tableList where table_id = @ctr
    if @ctr > 1 -- add union
        set @sql = @sql + ' union '
    set @sql = @sql + 'select '''+@table+''', max('+@column+') from ' + @table
    set @ctr = @ctr + 1
end

exec (@sql)

#1


0  

Please Try the below code to get the desired output.

请尝试以下代码以获得所需的输出。

--Creating Two Temp tables
CREATE TABLE #Temp1
( 
ID int IDENTITY(1,1),
TableName Varchar(50)
)

CREATE TABLE #Temp2
( 
TableName Varchar(50),
last_timestamp Date
)
--Inserting the required tables into Temp table 1, From information_schema
INSERT INTO #Temp1
SELECT t.table_name AS TableName FROM information_schema.tables t WHERE    t.table_name IN ('Table1','Table2') AND TABLE_SCHEMA='my_db'
--Below code gives the desired output.
DECLARE @Count INT
DECLARE @Count1 INT
DECLARE @Query nvarchar(max)
DECLARE @TableVariable Varchar(100)
SET @Count1 =0
SET @Count =(SELECT count(ID) FROM #Temp1)
WHILE (@Count1<@Count) 
BEGIN
   SET @Count1 =(@Count1 +1)
   SET @TableVariable = (SELECT TableName FROM #Temp1 WHERE ID =@Count1)

   SET @Query = 'SELECT t.table_name AS TableName,max(c.last_timestamp)AS  last_timestamp FROM information_schema.tables t,' +@TableVariable+ ' c
              WHERE t.table_name IN ('''+@TableVariable+''') GROUP BY   t.table_name'
    INSERT  INTO #Temp2 EXECUTE SP_EXECUTESQL @Query
END
SELECT * FROM #Temp2

#2


0  

use AdventureWorks2012
go
declare @sql varchar(max) = ''
declare @table varchar(max)
declare sqlcursor cursor for
SELECT  t.TABLE_SCHEMA + '.' + c.table_name
        --,c.column_name 
from    [INFORMATION_SCHEMA].[TABLES] t
join    [INFORMATION_SCHEMA].[COLUMNS] c on c.table_name = t.table_name
WHERE   COLUMN_NAME = 'ModifiedDate'
OPEN    sqlcursor   
FETCH NEXT FROM sqlcursor INTO @table   
set @sql = 'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
WHILE @@FETCH_STATUS = 0   
BEGIN   
       FETCH NEXT FROM sqlcursor INTO @table 
       IF @@FETCH_STATUS = 0   
       set @sql = @sql + ' union ' +  'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
END   

CLOSE sqlcursor   
DEALLOCATE sqlcursor

EXEC (@sql)

#3


0  

Assume the tables to be Q37290859Table1 and Q37290859Table2, each with Id and DateColumn common to both. Then you can use simple while loop (thus avoiding cursor) to build a final query.

假设表格为Q37290859Table1和Q37290859Table2,每个表格都包含Id和DateColumn。然后你可以使用简单的while循环(从而避免游标)来构建最终查询。

declare @sql varchar(max)
declare @table varchar(20)
declare @column varchar(20)
declare @tableList table 
(
    table_id int identity(1,1), 
    table_name varchar(30), 
    column_name varchar(30)
)

insert into @tableList 
    select t.table_name, c.column_name 
    from information_schema.tables t
    inner join information_schema.columns c on t.table_name = c.table_name
    where c.column_name = 'DateColumn'

declare @ctr int = 1
declare @ctr_max int = (select max(table_id) from @tableList)
set @sql = ''

while @ctr <= @ctr_max
begin
    select top 1 @table = table_name, @column = column_name from @tableList where table_id = @ctr
    if @ctr > 1 -- add union
        set @sql = @sql + ' union '
    set @sql = @sql + 'select '''+@table+''', max('+@column+') from ' + @table
    set @ctr = @ctr + 1
end

exec (@sql)