动态字段定义 - 可以在T-SQL中完成吗?

时间:2011-04-28 13:25:05

标签: tsql views

我要求我努力实施。如果可能的话,我想用原生T-SQL来实现这个目标。

我有以下表格:

CUSTOMER
========
ID,
Name

FIELDDEF
========
ID,
Name
FieldType   (Char  T, N, D   for Text, Number or Date)

CUSTOMERFIELD
=============
ID,
CustomerID,
FieldDefID,
CaptureDate,
ValueText,
ValueNumber,
ValueDate

基本上,这些表的目的是提供可扩展的自定义字段系统。这个想法是用户创建新的字段定义,可以是文本,数字或日期字段。然后,他们在ValueText,ValueNumber OR ValueDate字段中为这些字段创建值。

示例:

*Customer*
1,BOB
2,JIM

*FieldDef*
1,Mobile,T
1,DateOfBirth,D

*CustomerField*
ID,CustomerID,FieldDefID,CaptureDate,ValueText,ValueNumber,ValueDate
1,1,1,2011-01-1,07123456789,NULL,NULL
2,1,2,2011-01-1,NULL,NULL,09-DEC-1980
3,1,1,2011-01-2,07123498787,NULL,NULL

我需要创建一个如下所示的视图:

*CustomerView*
ID,Name,Mobile,DateOfBirth
1,BOB,07123498787,09-DEC-1980

请注意,Bob的移动设备是列表中的第二个,因为它使用最近的捕获日期。

理想情况下,我需要这是可扩展的,所以如果我将来创建一个新的字段def,它会自动在CustomerView中被选中。

这在T-SQL中是否可行?

谢谢,

西蒙。

3 个答案:

答案 0 :(得分:1)

您需要构建一个交叉表,您可以使用TSQL中的Pivot语句。这篇文章讨论了如何动态构建枢轴。

http://sqlserver-qa.net/blogs/t-sql/archive/2008/08/27/4809.aspx

答案 1 :(得分:1)

除非视图在每次FieldDef更改时动态重新创建,因为视图模式在创建时被锁定,否则视图无法实现。但是,使用存储过程是可能的,根据您的使用方式,存储过程可能有效,也可能不起作用。

修改1

以下示例查询仅适用于您当前的字段名称,并且必须由动态SQL修改才能正常工作:

修改2

修改为从客户字段表中获取最新值

with CustomerFieldNewest as (
    select
        cf1.*
    from
        customerfield cf1
    inner join
        (
            select
                customerid,
                fielddefid,
                max(capturedate) as maxcapturedate
            from
                customerfield cf2
            group by
                customerid,
                fielddefid
        ) cf2 on cf1.customerid = cf2.customerid
            and cf1.fielddefid = cf2.fielddefid
            and cf1.capturedate = cf2.maxcapturedate
)
,CustomerFieldPivot as (
    select
        C.ID as ID
        ,max(case when F.Name = 'Mobile' then CF.ValueText end) as Mobile
        ,max(case when F.Name = 'DateOfBirth' then CF.ValueDate end) as DateOfBirth
    from
        Customer C
    left join
        CustomerFieldNewest CF on C.ID = CF.CustomerID
    left join
        FieldDef F on F.ID = CF.FieldDefID
    group by
        C.ID
)
select
    C.*
    ,P.Mobile
    ,P.DateOfBirth
from
    Customer C
left join
    CustomerFieldPivot P on C.ID = P.ID

编辑3

以下是基于FieldDef中当前字段集动态生成视图的T-SQL代码(这假设视图CustomerView已经存在,因此您需要首先创建它作为空白定义或者你会得到一个错误)。我不确定所有这些的表现,但它应该可以正常工作。

declare @sql varchar(max)
declare @fielddef varchar(max)
declare @fieldlist varchar(max)

select
    @fielddef = coalesce(@fielddef + ', ' + CHAR(13) + CHAR(10), '') +
        '           max(case when F.Name = ''' + F.Name + ''' then CF.' +
            case F.FieldType
                when 'T' then 'ValueText'
                when 'N' then 'ValueNumber'
                when 'D' then 'ValueDate'
            end
        + ' end) as [' + F.Name + ']'

    ,@fieldlist = coalesce(@fieldlist + ', ' + CHAR(13) + CHAR(10), '') +
        '       [' + F.Name + ']'
from
    FieldDef F

set @sql = '
    alter view [CustomerView] as

    with CustomerFieldNewest as (
        select
            cf1.*
        from
            customerfield cf1
        inner join
            (
                select
                    customerid,
                    fielddefid,
                    max(capturedate) as maxcapturedate
                from
                    customerfield cf2
                group by
                    customerid,
                    fielddefid
            ) cf2 on cf1.customerid = cf2.customerid
                and cf1.fielddefid = cf2.fielddefid
                and cf1.capturedate = cf2.maxcapturedate
    )
    ,CustomerFieldPivot as (
        select
            C.ID as ID,
' + @fielddef + '
        from
            Customer C
        left join
            CustomerFieldNewest CF on C.ID = CF.CustomerID
        left join
            FieldDef F on F.ID = CF.FieldDefID
        group by
            C.ID
    )
    select
        C.*,
' + @fieldlist + '
    from
        Customer C
    left join
        CustomerFieldPivot P on C.ID = P.ID
'

print @sql
exec(@sql)

select * from CustomerView

答案 2 :(得分:1)

为了完整起见,有sql_variant

 declare @t table (typ varchar(1), yuk sql_variant)
   insert @t values ('d', getdate())
   insert @t values ('i', 1234)
   insert @t values ('s', 'bleep bloop')

 select
    yuk, 
    case typ 
        when 'd' then convert(datetime, yuk, 106)+50
        when 'i' then cast(yuk as int) * 2
        when 's' then reverse(cast(yuk as varchar))
        else yuk
    end
 from @t