0

I want to create a table with following columns

AssessmentTypeSpecificAttributeId int
AssessmentTypeSpecificAttributeName nvarchar(256)
AssessmentTypeSpecificAttributeDataType DATATYPE

Is there any way to store datatypes as data...

If there is no way, I will have to use nvarchar(256) then.

But first, I would like to give it a try.

1 Answer 1

1

Where is the actual value of your attribute? The only thing you can use with your current table is NVARCHAR because "datetime" is not a valid datetime value, "int" is not a valid int value etc.

Assuming that the same table includes the attribute's value then an alternative would be to use 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

But sql_variant has some big disadvantages: it can't store all data types, it's not supported by all tools and libraries, it requires extra coding everywhere to convert it to the 'real' data type etc.

Sign up to request clarification or add additional context in comments.

1 Comment

great answer ! thanks :) I think I'll do it manually as I don know how this will work with typed datasets. I have created a table for datatypes. This was good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.