Are there any benefits to using sql_variant over varchar in SQL Server?

前端 未结 4 1817
日久生厌
日久生厌 2021-02-19 03:29

I currently have a database table setup as follows (EAV - business reasons are valid):

  • Id - int (PK)
  • Key - unique, varchar(15)
  • Value - varchar(10
4条回答
  •  不思量自难忘°
    2021-02-19 03:47

    The good thing about sql variant is that you can store several types in a column and you keep the type information.

    Insert into MySettings values ('Name','MyName'); Insert into MySettings values ('ShouesNumber',45); Insert into MySettings values ('MyDouble',31.32);

    If you want to retrieve the type:

    select SQL_VARIANT_PROPERTY ( value , 'BaseType' ) as DataType,* from mysettings
    

    and you have:

    Datatype Name          Value
    -----------------------------
    varchar  Name          MyName
    int      ShoesNumber   45
    numeric  MyDouble      31.32
    

    Unfortunately this has several drawbacks:

    1. not very fast
    2. not well supported by ORM frameworks

提交回复
热议问题