Heim  >  Fragen und Antworten  >  Hauptteil

SQL-Methode zum Zurückgeben von Daten aus mehreren Tabellen basierend auf Spaltennamen

Ich versuche etwas etwas Seltsames zu tun, weiß aber nicht, wie ich es richtig hinbekomme. Im Wesentlichen versuche ich, alle Tabellen/Ansichten und Spalten zu extrahieren, bei denen der Spaltenname einer bestimmten Zeichenfolge ähnelt. Darüber hinaus möchte ich eine Datenzeile aus dieser Kombination aus Tabelle/Ansicht und Spalte extrahieren. Im zweiten Teil verliere ich mich. Ich weiß, dass ich die erforderlichen Tabellen/Ansichten und Spalten mithilfe der folgenden Select-Anweisung extrahieren kann.

SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%email%'
ORDER BY    TableName,ColumnName;

Auf diese Weise bekomme ich etwas wie unten

|ColumnName   |TableName   |
|emailAddress |all_emails  |
           ....

Aber ich möchte so etwas bekommen:

|ColumnName   |TableName   |Example             |
|emailAddress |all_emails  |[email protected]|
                    ....

Kann jemand einen Einblick geben?

P粉904450959P粉904450959240 Tage vor402

Antworte allen(1)Ich werde antworten

  • P粉392861047

    P粉3928610472024-02-22 12:09:52

    我想不出一种简单的方法来在查询中执行此操作,但这里有一个选项......

    将列和表的列表放入临时表中,并通过循环运行它们,使用动态 SQL 为每个表选择最大行。

    我在下面添加了大量评论来解释它。

    DECLARE @SQL NVARCHAR(1000)
    DECLARE @TABLE NVARCHAR(1000)
    DECLARE @COLUMN NVARCHAR(1000)
    DECLARE @SAMPLE NVARCHAR(1000)
    
    DROP TABLE IF EXISTS ##TABLELIST
    
    SELECT      COLUMN_NAME AS 'ColumnName'
               ,TABLE_NAME AS  'TableName'
               ,ROW_NUMBER() OVER (ORDER BY COLUMN_NAME,TABLE_NAME)[RN]
    
    INTO ##TABLELIST
    
    FROM        INFORMATION_SCHEMA.COLUMNS
    WHERE       COLUMN_NAME LIKE '%email%';
    
    
    
    ALTER TABLE ##TABLELIST
    
    ADD [Sample] NVARCHAR(1000) -- Add a column for your sample row.
    
    DECLARE @ROWCOUNTER INT = 1 -- Add a counter for the loop to use.
    
    WHILE @ROWCOUNTER <= (SELECT MAX([RN]) FROM ##TABLELIST) -- Keep the loop running until the end of the list.
    
    BEGIN
    
    UPDATE ##TABLELIST
    SET @TABLE  = TableName WHERE [RN] = @ROWCOUNTER -- Get the table name into a variable.
    
    UPDATE ##TABLELIST
    SET @COLUMN = ColumnName WHERE [RN] = @ROWCOUNTER -- Get the column name into a variable.
    
    
    SET @SQL = 'SELECT @SAMPLE = MAX([' + @COLUMN + ']) FROM [' + @TABLE + ']' -- Create SQL statement to pull max column from table specified in variables.
     
    EXEC SP_EXECUTESQL @SQL, N'@SAMPLE NVARCHAR(1000) OUTPUT', @SAMPLE OUTPUT -- Execute SQL and put the output into the @SAMPLE variable.
    
    
    UPDATE ##TABLELIST
    SET [Sample] = CAST(@SAMPLE AS NVARCHAR(1000)) WHERE [RN] = @ROWCOUNTER -- Insert the SQL output into the sample column.
    
    
    
    SET @ROWCOUNTER = @ROWCOUNTER+1 -- Add one to the row counter to move to the next column and table.
    
    END
    
    
    SELECT * FROM ##TABLELIST -- Select final output.
    
    

    Antwort
    0
  • StornierenAntwort