屬性的實際值在哪裏?您可以與您現有的表使用的唯一的事情是NVARCHAR
因爲「日期時間」是不是有效datetime
值,「詮釋」不是一個有效的int
值等
假設同一表中包含了屬性的值,則替代是使用sql_variant
:
drop table #t
go
create table #t (
AttrID int primary key,
AttrName nvarchar(256) not null,
AttrValue sql_variant not null,
AttrDataType as sql_variant_property(AttrValue, 'BaseType')
)
go
insert into #t (AttrID, AttrName, AttrValue) select 1, N'Test integer', cast(1 as int)
insert into #t (AttrID, AttrName, AttrValue) select 2, N'Test datetime', getdate()
insert into #t (AttrID, AttrName, AttrValue) select 3, N'Test decimal', cast(1.0 as decimal(2,1))
go
select * from #t
但sql_variant
有一些大的缺點:它不能存儲所有的數據類型,它不是由所有的工具和庫的支持,它需要額外的編碼無處不在將其轉換爲「真正的'數據類型等。
很好的答案!謝謝:) 我想我會做手動,因爲我不知道這將如何與類型的數據集。我爲數據類型創建了一個表。 這很高興知道。 –