我有很多变量表和每个表都有这么多列在里面。我想知道每个变量表的所有列名和数据类型,而无需通过每个变量表来标识它们是手动的。我试过使用sys.columns,但当然它不起作用,因为变量表信息没有存储在系统数据库中。加入多个变量表并输出所有列名和数据类型到一个变量
所有列名的结果应该像@AllColumnsAndDataTypes的结果。请参阅代码中的推荐内容。谢谢
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
不错的建议,但问题是我正在寻找一个解决方案,变量表不是我的临时表 – NanoHead 2014-10-29 19:32:02
我不知道我理解。我链接的源代码和我提供的示例绝对使用表变量而不是临时表。 – 2014-10-29 19:54:49
嗯有趣....这实际上是不错的,除了我必须命名一个具有唯一ID的列,否则如果表具有相同的列名,它将从另一个表中拉出错误的结果。 – NanoHead 2014-10-29 20:45:18