动态SQL中的动态转换

时间:2017-09-06 19:05:29

标签: sql-server casting dynamic-sql

我正在尝试创建一个存储过程,该存储过程将返回一个结果集,该结果集具有可变类型的可变数量的列(在tblHeaderDefinition中定义)(也在tblHeaderDefinition中)。这些列的值作为VARCHAR存储在一个单独的表中,该表是INNER JOINED到定义表。

一些不同的应用程序(在C#,VBA等中)引入这些数据并以不同的方式使用它,但是当我带入这个表时,我的列都是类型VARCHAR,与值列相同。我想在SQL中将它们转换为适当的类型,并让应用程序查看列的type属性以确定如何处理列,而不是将定义表引入每个单独的应用程序并让每个应用程序转换为正确的类型。

下面的查询返回我正在寻找的平面表,但我想不出在这种情况下使用动态转换的方法。

DECLARE @collist NVARCHAR(MAX)
SET @collist = stuff((SELECT DISTINCT ',' + QUOTENAME(ColumnName) 
            FROM tblHeaderDefinition
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

DECLARE @q NVARCHAR(MAX)
SET @q = '
    SELECT * 
    FROM (
        SELECT ColumnName, Value
            FROM (
                SELECT  tblHeaderDefinition.pkHeaderDefinitionID
                    , tblHeaderDefinition.ColumnName
                    , tblHeaderDefinition.ColumnType
                    , tblHeaderValue.Value
                FROM    tblHeaderValue 
                INNER JOIN  tblHeaderDefinition ON tblHeaderValue.fkHeaderDefinitionID = tblHeaderDefinition.pkHeaderDefinitionID
        ) AS x
    ) AS source
    pivot (
        max(Value)
        FOR ColumnName IN (' + @collist + ')
    ) AS pvt
'

EXEC (@q)

测试

我正在使用的表:

CREATE TABLE [dbo].[tblHeaderDefinition](
    [pkHeaderDefinitionID] [int] IDENTITY(1,1) NOT NULL,
    [ColumnName] [varchar](256) NULL,
    [ColumnType] [varchar](256) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tblHeaderValue](
    [pkHeaderValueID] [int] IDENTITY(1,1) NOT NULL,
    [fkHeaderDefinitionID] [int] NULL,
    [fkHeaderID] [int] NULL,
    [Value] [varchar](256) NULL
) ON [PRIMARY]

GO

SET IDENTITY_INSERT [dbo].[tblHeaderDefinition] ON 
GO
INSERT [dbo].[tblHeaderDefinition] ([pkHeaderDefinitionID], [ColumnName], [ColumnType]) VALUES (1, N'ColIntTest', N'INT')
GO
INSERT [dbo].[tblHeaderDefinition] ([pkHeaderDefinitionID], [ColumnName], [ColumnType]) VALUES (2, N'ColVarCharTest', N'VARCHAR(50)')
GO
INSERT [dbo].[tblHeaderDefinition] ([pkHeaderDefinitionID], [ColumnName], [ColumnType]) VALUES (3, N'ColRealTest', N'REAL')
GO
INSERT [dbo].[tblHeaderDefinition] ([pkHeaderDefinitionID], [ColumnName], [ColumnType]) VALUES (4, N'ColBitTest', N'BIT')
GO
SET IDENTITY_INSERT [dbo].[tblHeaderDefinition] OFF
GO

SET IDENTITY_INSERT [dbo].[tblHeaderValue] ON 
GO
INSERT [dbo].[tblHeaderValue] ([pkHeaderValueID], [fkHeaderDefinitionID], [fkHeaderID], [Value]) VALUES (1, 1, 5, N'54')
GO
INSERT [dbo].[tblHeaderValue] ([pkHeaderValueID], [fkHeaderDefinitionID], [fkHeaderID], [Value]) VALUES (2, 2, 5, N'NA-0490')
GO
INSERT [dbo].[tblHeaderValue] ([pkHeaderValueID], [fkHeaderDefinitionID], [fkHeaderID], [Value]) VALUES (3, 3, 5, N'1000.094')
GO
INSERT [dbo].[tblHeaderValue] ([pkHeaderValueID], [fkHeaderDefinitionID], [fkHeaderID], [Value]) VALUES (4, 4, 5, N'1')
GO
SET IDENTITY_INSERT [dbo].[tblHeaderValue] OFF
GO

1 个答案:

答案 0 :(得分:1)

您可以使用另一个包含列的转换的变量:

DECLARE @collist NVARCHAR(MAX), @collist2 NVARCHAR(MAX);

SET @collist = stuff((SELECT DISTINCT ',' + QUOTENAME(ColumnName) 
            FROM tblHeaderDefinition
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @collist2 = stuff((SELECT DISTINCT ',CAST(' + QUOTENAME(ColumnName) + ' AS ' + 
                                        ColumnType + ') AS ' + QUOTENAME(ColumnName)
            FROM tblHeaderDefinition
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

DECLARE @q NVARCHAR(MAX)
SET @q = '
    SELECT ' + @collist2 + ' 
    FROM (
        SELECT ColumnName, Value
            FROM (
                SELECT  tblHeaderDefinition.pkHeaderDefinitionID
                    , tblHeaderDefinition.ColumnName
                    , tblHeaderDefinition.ColumnType
                    , tblHeaderValue.Value
                FROM    tblHeaderValue 
                INNER JOIN  tblHeaderDefinition ON tblHeaderValue.fkHeaderDefinitionID = tblHeaderDefinition.pkHeaderDefinitionID
        ) AS x
    ) AS source
    pivot (
        max(Value)
        FOR ColumnName IN (' + @collist + ')
    ) AS pvt
'

EXEC (@q)