简体   繁体   English

SQL:如何使用信息架构中的Table_Name和Pivoted Column_Name构建选择查询

[英]SQL: How to build a select query with Table_Name and Pivoted Column_Name from Information Schema

I've written some SQL queries of queries that I later use in Cursors to run each line. 我编写了一些SQL查询查询,以后在游标中使用它们来运行每一行。 They take advantage of Information_Schema,Table_Name,Column_name, which I recently just picked up how to do from forums. 他们利用了Information_Schema,Table_Name,Column_name的优势,我最近刚刚从论坛上了解了如何做。 Below is an example of one Query I use to delete rows from multiple tables with a criteria. 下面是一个查询示例,我使用该查询从多个表中删除带有条件的行。 I've left out the cursor code as I'm only focused on trying to build the select query. 由于我只专注于尝试构建选择查询,因此我省略了光标代码。

DECLARE @devCodeDELETE varchar(20);
SET @devCodeDELETE = '001e';

SELECT 'DELETE FROM ' + TABLE_NAME + ' WHERE devCode = '''+ @devCodeDELETE +''';'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like 'DEV_%';

The above Outputs which is then run through cursor code: 上面的输出然后通过游标代码运行:

DELETE FROM DEV_assembly WHERE devCode = '001e';
DELETE FROM DEV_comments WHERE devCode = '001e';
DELETE FROM DEV_costhistory WHERE devCode = '001e';
DELETE FROM DEV_dates WHERE devCode = '001e';
DELETE FROM DEV_master WHERE devCode = '001e';
etc...

This has been effective for building batch update queries against multiple tables, since all tables join with column "devCode". 由于所有表都以“ devCode”列联接,因此这对于针对多个表构建批处理更新查询非常有效。

I can't seem to figure out how to write a select query that will pivot (is it a pivot?) column names with it's source table, using Table_Name, Column_Name and Information_Schema. 我似乎无法弄清楚如何编写一个选择查询,该查询将使用Table_Name,Column_Name和Information_Schema对其源表进行透视(是否是透视?)列名。 Not even sure if it is possible. 甚至不确定是否有可能。

The output I'm trying to achieve for now would be : 我现在想要实现的输出是:

table1 table1.c1 table1.c2 table1.c3, etc
table2 table2.c1 table2.c2 table2.c3, table2.c4, table2.c5,etc
table3 table3.c1 table3.c3 table2.c3, table2.c4,etc
etc....

I displayed each line with different numbers of columns to illustrate each tables having different number of columns. 我以不同的列数显示每行,以说明每个表具有不同的列数。 If I can get this far, I make the necessary changes to make each line into a query. 如果可以解决的话,请进行必要的更改以使每一行都成为查询。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks in advance. 提前致谢。

-UPDATE---- -UPDATE ----

Thanks to the Brian from below I made edits pertaining to my schema: 感谢下面的Brian,我对自己的架构进行了编辑:

declare @TableName as varchar(256)
declare @ColumnName as varchar(256)
declare @str as varchar(max)
declare @outstring as varchar(max)

select TABLE_NAME, COLUMN_NAME 
into #temp
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE 'DEV_%' AND COLUMN_NAME <> 'devCode' AND COLUMN_NAME NOT LIKE 'ID%'

select distinct TABLE_NAME, CONVERT(varchar(max),'') outstring 
into #tempout
from #temp

Declare TableNames cursor FORWARD_ONLY FOR
select * from #tempout
for update of outstring

open TableNames

fetch Next From TableNames into @TableName, @outstring
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @outstring = ''
    SELECT @outstring = @outstring + ', ' + COLUMN_NAME
    FROM #temp
    WHERE TABLE_NAME = @TableName

    UPDATE #tempout 
    SET outstring = @outstring
    WHERE CURRENT OF TableNames 

    fetch Next From TableNames into @TableName, @ColumnName
END

close TableNames
deallocate TableNames

select 'INSERT INTO ' + TABLE_NAME + '(devCode' + outstring + ') SELECT ''newCode'',' + SUBSTRING(outstring,2,99999) +' FROM ' + TABLE_NAME + ' WHERE devCode = ''oldCode'';' from #tempout

And the output was as desired! 和输出是所需的! That'll go into a another cursor to run each line. 这将进入另一个光标以运行每一行。

INSERT INTO DEV_assembly(devCode, asmNote, asmUser) SELECT 'newCode', asmNote, asmUser FROM DEV_assembly WHERE devCode = 'oldCode';
INSERT INTO DEV_comments(devCode, comComment, comDate, comStatus, comExternal) SELECT 'newCode', comComment, comDate, comStatus, comExternal FROM DEV_comments WHERE devCode = 'oldCode';
INSERT INTO DEV_costhistory(devCode, costDate, costQuote, costFactory, costNote, costTimeStamp, costSelect) SELECT 'newCode', costDate, costQuote, costFactory, costNote, costTimeStamp, costSelect FROM DEV_costhistory WHERE devCode = 'oldCode';
INSERT INTO DEV_dates(devCode, datesRecord, datesNote, datesDue, datesStatus, datesComplete) SELECT 'newCode', datesRecord, datesNote, datesDue, datesStatus, datesComplete FROM DEV_dates WHERE devCode = 'oldCode';
INSERT INTO DEV_dimensions(devCode, dimNote, dimDimension) SELECT 'newCode', dimNote, dimDimension FROM DEV_dimensions WHERE devCode = 'oldCode';
etc....

You can't do a SQL pivot without knowing the columns in advance... unless you do some dynamic SQL. 如果不事先了解列,则无法进行SQL透视...除非您进行一些动态SQL。 Even then the number of columns is fixed so you'd have an enormous list of columns, one for every column title in the entire database. 即使这样,列数也是固定的,因此您将拥有庞大的列列表,整个数据库中的每个列标题都有一个。

What you can do is run a cursor though all the tables to build up a string with all the columns you need... such as: 您可以做的是通过所有表运行一个游标,以使用所需的所有列来构建一个字符串,例如:

declare @TableName as varchar(256)
declare @ColumnName as varchar(256)
declare @str as varchar(max)
declare @outstring as varchar(max)

select TABLE_NAME, COLUMN_NAME 
into #temp
from INFORMATION_SCHEMA.COLUMNS

select distinct TABLE_NAME, CONVERT(varchar(max),'') outstring 
into #tempout
from #temp

Declare TableNames cursor FORWARD_ONLY FOR
select * from #tempout
for update of outstring

open TableNames

fetch Next From TableNames into @TableName, @outstring
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @outstring = ''
    SELECT @outstring = @outstring + ' ' + COLUMN_NAME
    FROM #temp
    WHERE TABLE_NAME = @TableName

    UPDATE #tempout 
    SET outstring = @outstring
    WHERE CURRENT OF TableNames 

    fetch Next From TableNames into @TableName, @ColumnName
END

close TableNames
deallocate TableNames

select * from #tempout

暂无
暂无

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

相关问题 “SELECT column_name FROM table_name”意外结果 - "SELECT column_name FROM table_name" unexpected result SELECT * FROM table_name ORDER BY column_name? - SELECT * FROM table_name ORDER BY column_name? 当您运行查询“ SELECT * FROM table_name WHERE column_name;”时,会发生什么情况? - What is happening when you run the query “SELECT * FROM table_name WHERE column_name;”? 从表名中选择SELECT Colunm_Name Column_Name(无逗号) - SELECT Colunm_Name Column_Name from Table_Name (no comma) mySQL,从 information_schema 中选择 BOTH table_rows AND column_name - mySQL, select BOTH table_rows AND column_name from information_schema 如何从DML语句获取column_name和table_name? - How to get column_name and table_name from DML statement? SQL:从@variable 中选择表的表值,例如'Select * from @Table_name' where @table_name=[DB_Name].[Schema_name].[table_name] - SQL : selecting the table values of table from @variable like 'Select * from @Table_name' where @table_name=[DB_Name].[Schema_name].[table_name] 是否可以使用SELECT * FROM table_name WHERE column_name =&#39;value&#39;,其中&#39;value&#39;可能出现在多行中? - Is it possible to use SELECT * FROM table_name WHERE column_name = 'value' where 'value' might appear in more than one row? ORA-00907:查询ALTER TABLE Table_Name时缺少右括号修改了Column_Name - ORA-00907: missing right parenthesis at query ALTER TABLE Table_Name MODIFY Column_Name 如何避免使用“SELECT * FROM {table_name}”进行 SQL 注入? - How to avoid SQL injection with “SELECT * FROM {table_name}”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM