简体   繁体   中英

Join multiple variable tables and output the all column names and datatypes into a variable

I have many variable tables and each table has so many columns in it. I like to know all of my column names and datatypes for each variable table without going through each variable table to identify what they are manually. I've tried to use the sys.columns but of course it doesn't work since the variable table information doesn't stored in the system database.

The result of all columns names should be like the result of @AllColumnsAndDataTypes. Please see the commend within the codes. Thank you

            ALTER proc [dbo].[usp_BigCodes]
            as
            begin

             --Multiple table variables
             declare @Table_01 table( TB01_MasterID int, Column_01 nvarchar(100), Column_02 nvarchar(100))
             declare @Table_02 table( TB02_MasterID int, Column_03 nvarchar(100), Column_04 nvarchar(100))
             declare @Table_03 table( TB03_MasterID int, Column_04 nvarchar(100), Column_05 nvarchar(100))

              ---I need to convert all of the code above programatically into 1 table
             --The result should be out put into the @AllColumnsAndDataTypes 
             declare @AllColumnsAndDataTypes nvarchar(max)
             --Result should be similar to below.
             set @AllColumnsAndDataTypes='
                                         TB01_MasterID int, Column_01 nvarchar(100), Column_02 nvarchar(100)
                                         TB02_MasterID int, Column_03 nvarchar(100), Column_04 nvarchar(100)
                                         TB03_MasterID int, Column_04 nvarchar(100), Column_05 nvarchar(100)
                                        '
            ---Need code to do output the result into the variable like on line 15 to 17  
                --Join all table variables and output all columns names and datatype into @AllColumnsAndDataTypes 
                select  @AllColumnsAndDataTypes = ???????? from 
                @Table_01 as T1 
                INNER JOIN @Table_02 as T2 ON T1.TB01_MasterID=T2.TB02_MasterID
                INNER JOIN @Table_03 as T3  ON T1.TB01_MasterID=T3.TB03_MasterID 

             --Display the result
             select @AllColumnsAndDataTypes as AllColumnsInfo
            end

You can use sys.columns but you have to do it in the context of TempDB

Source

Example:

declare  @foo table ( bar int, blah varchar(30) )
declare  @object_name sysname, @object_id int

select   @object_name = OBJECT_NAME([object_id], DB_ID('TempDB'))
        ,@object_id = [object_id]
from     tempdb.sys.columns 
where    name = 'bar'

select   @object_name [object_name], @object_id [object_id]

select   [object_id], name
from     tempdb.sys.columns 
where    [object_id] = @object_id

Try the following...

declare @Table_01 table( TB01_MasterID int, Column_01 nvarchar(100), Column_02 nvarchar(100))

select TN.N.value('local-name(.)', 'sysname') as ColumnName
from 
  (
  select TV.*
  from (select 1) as D(N)
    outer apply (
                select top(0) *
                from @Table_01
                ) as TV
  for xml path(''), elements xsinil, type
  ) as TX(X)
cross apply TX.X.nodes('*') as TN(N)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM