DECLARE @TableName nvarchar(50) DECLARE column_to_row CURSOR FOR --List of tables that we want to unpivot columns as row -- kolonlari alinacak tablo listesi SELECT DISTINCT t.name FROM sys.tables t JOIN sys.schemas s ON t.schema_id=t.schema_id WHERE t.name like '%_CT%' AND s.name='cdc' OPEN column_to_row FETCH NEXT FROM column_to_row INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @script nvarchar(max) = null DECLARE @columns nvarchar(2000) = null -- keep the table's column list -- listelecek tablonun kolon listesi tutuluyor select @columns = COALESCE(@columns + ',','') + c.name from sys.tables t join sys.columns c on t.object_id = c.object_id where t.name = @TableName set @script = 'SELECT '+@columns+' FROM [cdc].['+@TableName+'] (nolock)' print (@script) FETCH NEXT FROM column_to_row INTO @TableName END CLOSE column_to_row DEALLOCATE column_to_row