You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--create a temp table, and insert the output of the RESTORE HEADERONLY statement into it via EXEC and a "RESTORE" string
64
+
DECLARE @HO ASdbo.Header_Only;
65
+
66
+
SELECT*
67
+
INTO #HO
68
+
FROM @HO
69
+
WHERE1=2
70
+
71
+
INSERTINTO #HO
72
+
EXEC ('RESTORE HEADERONLY FROM DISK = N''D:\Backup\SomeApp9_Template.bak'' ')
73
+
74
+
75
+
-- build a dynamic T-SQL query by iterating through the column names of the temp table (well, actually of the table type).
76
+
--The end result is a bunch of queries that select a static string (field name) and a single column (field value) from the temp table, UNION'ed together.
77
+
--The last "UNION ALL" is trimmed from the string. And the string is executed
78
+
DECLARE @TSql VARCHAR(MAX) ='';
79
+
SELECT
80
+
@TSql = @TSql +'SELECT '''+c.name+''' AS [Field], CAST(['+c.name+'] AS VARCHAR(MAX)) AS [Value] FROM #HO UNION ALL'+CHAR(13) +CHAR(10)
81
+
FROMsys.table_types t
82
+
JOINsys.columns c
83
+
onc.object_id=t.type_table_object_id
84
+
WHEREt.name='Header_Only'
85
+
ORDER BYc.column_id
86
+
87
+
SET @TSql =LEFT(@TSql, LEN(@TSql) -11) +CHAR(13) +CHAR(10)
0 commit comments