我有很多變量表和每個表都有這麼多列在裏面。我想知道每個變量表的所有列名和數據類型,而無需通過每個變量表來標識它們是手動的。我試過使用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