0

I have this SQL query

SELECT table_name 
INTO #LukaTestTable
FROM information_schema.columns
WHERE column_name = 'GUID'
ORDER BY TABLE_NAME ;

How get the value of the column GUID in all tables with the name from TABLE_NAME?

Or can I get table like?

TABLE_NAME GUID_VALUE
5
  • Are you using MySQL or SQL Server? Those are two different databases. Commented Jan 29, 2018 at 9:40
  • Are you sure that all the columns GUID have the same data type? Commented Jan 29, 2018 at 9:40
  • @JustinasMarozas SQL Server Commented Jan 29, 2018 at 9:41
  • @sepupic yes, same data type Commented Jan 29, 2018 at 9:41
  • Then you can construct dynamic select with union all Commented Jan 29, 2018 at 9:42

3 Answers 3

1

In order to be able to query a table before you know what table you want you have to use dynamic queries. If you have an arbitrary number of tables, in SQL Server you could achieve this with cursors:

DECLARE
    @column VARCHAR(256) = 'GUID',
    @table VARCHAR(256),
    @query VARCHAR(4000);

DECLARE #Tables CURSOR LOCAL FAST_FORWARD
FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = @column
ORDER BY TABLE_NAME;

OPEN #Tables
FETCH NEXT FROM #Tables INTO @table

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @query = 'SELECT ' + @column + ' FROM ' + @table
    EXEC(@query)

    FETCH NEXT FROM #Tables INTO @table
END

CLOSE #Tables
DEALLOCATE #Tables;

If you're sure your 'GUID' column will have same data type - you can create a temp table and insert to it in WHILE loop and SELECT everything in one go at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Works, just EXEC (@query)
1

You can use dynamic sql like this:

   declare @sql varchar(max) = cast (' ' as varchar(max));
    select @sql = @sql +
    (select ' select GUID from ' + quotename(table_name) + ' union all ' as 'text()'
    from #LukaTestTable
    for xml path('')); 

    set @sql = reverse(stuff(reverse(@sql), 1, 10, ''));
    --print @sql
    exec(@sql);

And if you want table_name as e separate column you should first save it in your #LukaTestTable

Comments

1

Try this below script using While loop and dynamic Sql

IF OBJECT_ID('tempdb..#GetSqlQuery') IS NOT NULL
DROP TABLE #GetSqlQuery
IF OBJECT_ID('tempdb..#GetSpecficcolumnvalue') IS NOT NULL
DROP TABLE #GetSpecficcolumnvalue
GO
CREATE TABLE #GetSqlQuery
(
ID INT IDENTITY
,SELECtQuery nvarchar(max)
,TableName varchar(200)
)

CREATE TABLE #GetSpecficcolumnvalue
(
ID INT IDENTITY
,GetValue nvarchar(max)
,TableName varchar(200)
)

INSERT INTO #GetSqlQuery(SELECtQuery,TableName)
SELECT 'SELECT GUID,'''+TABLE_NAME+''' As TableName FROM '+QUOTENAME(TABLE_NAME) AS SELECtQuery,QUOTENAME(TABLE_NAME) AS TableName
FROM( 
SELECT 
    TABLE_SCHEMA,
    TABLE_NAME,
    COLUMN_NAME 

FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME ='GUID'   
)dt
ORDER BY TABLE_NAME


DEclare @MinId INt,@MaxId INT,@Sql nvarchar(maX),@TableName varchar(1000)
SELECT @MinId =MIN(Id),@MaxId=MAX(ID) FROM #GetSqlQuery
WHILE (@MinId<=@MaxId)
BEgin
SELECT @Sql=SELECtQuery ,@TableName=TableName   From  #GetSqlQuery WHERE ID=@MinId

PRINT @Sql
INSERT INTO #GetSpecficcolumnvalue(GetValue,TableName)
EXEC (@Sql)
SET @MinId=@MinId+1
END

  SELECT  ROW_NUMBER()OVER(ORDER BY (SELECT 1)) AS Seq,GetValue,TableName 
   FROM 
   (
 SELECT *, ROW_NUMBER ()OVER(PArtition by GetValue,TableName    ORDER BY 
 TableName) AS dup FROM #GetSpecficcolumnvalue
  )dt where dt.dup=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.