试试这个——
询问:
IF OBJECT_ID (N'tempdb.dbo.#temp') IS NOT NULL
DROP TABLE #temp
DECLARE @temp TABLE (FieldA NVARCHAR(50))
INSERT INTO @temp (FieldA)
VALUES
('SkillA'),
('SkillB'),
('SkillC')
SELECT *
INTO #temp
FROM (
SELECT FieldA, a = CAST(NULL AS INT)
FROM @temp
) src
PIVOT
(
MAX(a)
FOR FieldA IN (SkillA, SkillB, SkillC)
) unpvt
SELECT *
FROM #temp t
输出:
SkillA SkillB SkillC
----------- ----------- -----------
NULL NULL NULL
更新 2:我不知道金额行数据
询问:
IF OBJECT_ID (N'tempdb.dbo.##temp') IS NOT NULL
DROP TABLE ##temp
DECLARE @temp TABLE (FieldA VARCHAR(20), FieldB VARCHAR(20))
INSERT INTO @temp (FieldA, FieldB)
VALUES
('SKillA', 'SubjectA'),
('SkillB', 'SubjectB'),
('SkillC', 'SubjectC')
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = '
SELECT *
INTO ##temp
FROM (
SELECT ' + STUFF((
SELECT DISTINCT ', [' + FieldA + '] = NULL, [' + FieldB + '] = NULL'
FROM @temp
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') + ') t
WHERE 1 != 1'
EXEC sys.sp_executesql @SQL
SELECT *
FROM ##temp t
输出:
SKillA SubjectA SkillB SubjectB SkillC SubjectC
----------- ----------- ----------- ----------- ----------- -----------