search

Home  >  Q&A  >  body text

SQL method to return data from multiple tables based on column names

I'm trying to do something a little weird but can't figure out the right way to accomplish it. Essentially, I'm trying to extract all tables/views and columns where the column name is like a certain string. Beyond that, I want to extract 1 row of data from that table/view and column combination. The second part is where I get lost. I know I can extract the necessary tables/views and columns using the select statement below.

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

This way I will get something like the following

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

But I want to get something like this:

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

Can anyone provide any insight?

P粉904450959P粉904450959277 days ago449

reply all(1)I'll reply

  • P粉392861047

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

    I can't think of an easy way to do this in a query, but here is an option...

    Put the list of columns and tables into a temporary table and run them through a loop, using dynamic SQL to select the largest row for each table.

    I've added a bunch of comments below to explain it.

    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.
    
    

    reply
    0
  • Cancelreply