简体   繁体   English

如果表中存在列名,如何从sql server数据库中获取所有表并将表数据保存在临时表中

[英]how to get all the tables from sql server database if a column name exists in the table and save the table data in a temporary table

I am trying to write a windows service, which will send automatic emails. 我正在尝试编写Windows服务,该服务将发送自动电子邮件。 all the tables which require email sending have common columns 'templateid' and 'emailstatus'. 所有需要发送电子邮件的表都具有公用列'templateid'和'emailstatus'。 I want to iterate through all the tables and get the tables which has column name 'templateid'. 我想遍历所有表并获取具有列名“ templateid”的表。 Now that i have the list of tables with column name 'templateid' get the data from each table whose email status is 'false' and save it in a temporary table. 现在,我有了列名称为“ templateid”的表列表,从电子邮件状态为“ false”的每个表中获取数据,并将其保存在临时表中。 if 'table1' has 4 rows of data, the temporary table should have 4 rows. 如果“ table1”具有4行数据,则临时表应具有4行。 after iterating through the next table the row collection should be added to the same temporary table. 在遍历下一个表之后,应将行集合添加到同一临时表中。

IF (SELECT object_id('TempDB..#TEMPTABLE')) IS NOT NULL
BEGIN
    DROP TABLE #TEMPTABLE
END
CREATE TABLE #TEMPTABLE(
[ID] INTEGER IDENTITY(1,1) NOT NULL,
[TABLE_NAME] VARCHAR(1000)
)
INSERT INTO #TEMPTABLE(TABLE_NAME)
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'TEMPLATEID'
SELECT * FROM #TEMPTABLE
DECLARE @ROWCOUNT INT
SET @ROWCOUNT = (SELECT COUNT(ID) FROM #TEMPTABLE) 
DECLARE @I INT
SET @I=1
WHILE(@I<=@ROWCOUNT)
BEGIN
    DECLARE @TABLENAME VARCHAR(500)
        SELECT @TABLENAME=TABLE_NAME FROM #TEMPTABLE WHERE ID=@I
        EXEC('SELECT * FROM '+@TABLENAME)
    SET @I=@I+1
END

i found the above query which is giving me all the tables. 我发现上面的查询给了我所有的表。 after that i am clueless how to proceed further as i am not good with sql server. 之后,我不知道如何进一步进行,因为我对sql server不好。

Not sure you are still looking for an answer but the following code will give you a temporary table with just the tables that have a column named 'templateid'. 不确定您是否仍在寻找答案,但是以下代码将为您提供一个临时表,其中仅包含具有名为“ templateid”的列的表。 From here you would need a cursor as Tanner suggested to loop through each table and then insert records from each table (into your final target table) where email status = 'false'. 正如Tanner建议的那样,从这里您将需要一个游标,以遍历每个表,然后将每个表的记录插入到您的最终目标表中(电子邮件状态为“ false”)。

Here is the code that gets you the temp tables with columns named 'templateid' along with a sample for your cursor: 这是使您获得临时表的代码,这些临时表具有名为“ templateid”的列以及一个游标示例:

declare @max_tables int
declare @max_columns int
declare @sql nvarchar(400)
declare @x int
declare @y int
declare @table varchar(50)
declare @columns varchar(800)
declare @tablename varchar(100)

create table #c ([Table] varchar(50),[Columns] varchar(800))

select ROW_NUMBER() OVER(ORDER BY name) AS Row, name 
into #table_list
from sys.objects 
where type_desc = 'USER_TABLE' 
order by name

set @max_tables = (select count(*) from sys.objects where type_desc = 'USER_TABLE')
set @y = 0

while @y < @max_tables
    begin
        set @y = @y + 1
        set @table = (select name from #table_list where row = @y)

        create table #t (c int)

        set @sql = 'select count(*) as c from Information_schema.Columns where table_name = ''' + @table + ''''
        insert into #t exec sp_executesql @sql

        set @max_columns = (select top 1 c from #t)

        DROP TABLE #t

        set @x = 0
        set @columns = ''

        while @x < @max_columns 
            begin
                set @x = @x + 1
                set @columns = @columns + (select column_name from Information_schema.Columns where table_name = @table and ordinal_position = @x)
                if @x < @max_columns set @columns = @columns + ', '
            end

        insert into #c select @table,@columns

    end

select * into #tables from #c c
where c.Columns like '%templateid%'

declare my_cursor cursor for
select table from #t

open my_cursor
fetch next from my_cursor into @tablename

while @@fetch_status = 0
begin

     --do something here to retrieve your data from each table and 
     --insert into target table

end

close my_cursor
deallocate my_cursor


DROP TABLE #c,#tables,#table_List

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从两个临时表中获取数据并插入第三个表而不在 SQL 服务器中添加新行 - How to get data from two temporary tables and insert in third table without adding new row in SQL Server 如何使用 SQL Server 从数据库中获取具有列的表的特定列中的所有表名和不同值 - How to get all the table names and distinct values in a particular column from a database for the tables which have a column using SQL Server 如何从不同的表中获取更新的列名,并将其数据与列名一起插入SQL Server的单个历史表中? - How can I get updated column names from different tables and insert their data with column name in a single history table in SQL Server? SQL 服务器:从一个表中获取所有数据,但条件在第二个表列 - SQL Server : get all data from one table but condition on 2nd tables column 如何使用SQL Server联接4个表以获取所有4个表数据 - How to join 4 Table to get all 4 tables Data using sql server 从数据库中每个表的每个列获取数据| SQL服务器 - Get data from every column in every table in a database | SQL Server 检查临时表中是否存在列总是在 SQL 服务器中返回 false - Checking if column exists in temporary table always returns false in SQL Server SQL如果在临时表中存在 - SQL If Exists in temporary table 如何从SQL Server数据表的列中获取字符串数据 - how to get string data from the column of the data table in sql server 检查SQL Server数据库表中是否存在表或列 - Check if table or column exists in SQL Server database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM