我有两张桌子:
我需要一个带列名的select语句和数据作为DataSet。(输出也可能是XML)
TABLES:
FormColumDefinition ------------------------------ formColumnID formColumnDataType formColumnName formColumnLabel formColumnSeqNumber (just represents how it should appear on the form). FormData --------------- formDataID formRowNumber formColumnID formDataDate (date) - only one of the three date/int/char will have value based on the column data type. formDataInt (int) formDataChar (varchar)
答案 0 :(得分:0)
如果我理解正确,请给出以下内容:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FormData](
[formDataID] [int] IDENTITY(1,1) NOT NULL,
[formRowNumber] [int] NOT NULL,
[formColumnID] [int] NOT NULL,
[formDataDate] [datetime] NULL,
[formDataInt] [int] NULL,
[formDataChar] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[FormData] ON
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (1, 1, 1, NULL, NULL, N'John')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (2, 1, 2, NULL, NULL, N'Private')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (3, 1, 3, NULL, NULL, N'123456')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (4, 2, 1, NULL, NULL, N'Bill')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (5, 2, 2, NULL, NULL, N'Captain')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (6, 2, 3, NULL, NULL, N'789352')
SET IDENTITY_INSERT [dbo].[FormData] OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FormColumnDefinition](
[formColumnID] [int] IDENTITY(1,1) NOT NULL,
[formColumnDataType] [varchar](50) NOT NULL,
[formColumnName] [varchar](50) NOT NULL,
[formColumnLabel] [varchar](50) NOT NULL,
[formColumnSeqNumber] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[FormColumnDefinition] ON
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (1, N'System.String', N'Name', N'Name', 0)
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (2, N'String.String', N'Rank', N'Rank', 1)
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (3, N'System.Int32', N'SerialNumber', N'Serial Number', 2)
SET IDENTITY_INSERT [dbo].[FormColumnDefinition] OFF
您可以使用如下的枢轴命令:
SELECT formRowNumber, [Name],[Rank],[SerialNumber]
FROM
(SELECT fcd.formColumnName, fd.formRowNumber, fd.formDataChar
FROM FormColumnDefinition fcd INNER JOIN FormData fd
ON fcd.formColumnID = fd.formColumnID) AS src
PIVOT
(
MAX(formDataChar)
FOR formColumnName IN ([Name],[Rank],[SerialNumber])
) AS pvt
ORDER BY pvt.formRowNumber
得到:
formRowNumber Name Rank SerialNumber 1 John Private 123456 2 Bill Captain 789352
PIVOT的问题是你必须提前知道列名。如果你不介意一点动态SQL,你也可以解决这个问题。这是我在http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx时无耻地从安德拉斯偷走的一个例子。
DECLARE @cols NVARCHAR(2000);
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + formColumnName
FROM FormColumnDefinition
ORDER BY '],[' + formColumnName
FOR XML PATH('')
), 1, 2, '') + ']';
DECLARE @query NVARCHAR(4000);
SET @query = N'SELECT formRowNumber, ' + @cols +
'FROM (SELECT fcd.formColumnName, fd.formRowNumber, fd.formDataChar ' +
'FROM FormColumnDefinition fcd INNER JOIN FormData fd ' +
'ON fcd.formColumnID = fd.formColumnID) AS src PIVOT ' +
'(MAX(formDataChar) FOR formColumnName IN ('+ @cols + ')) AS pvt ORDER BY pvt.formRowNumber;';
EXECUTE(@query);