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

前端 未结 4 1792
日久生厌
日久生厌 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:55

    The sql_variant type has its limitations as described well by Zarathos.

    What I find confusing is that you mention varchar(1000) and then 'ouch' about returning a converted nvarchar(4000).

    I would start by saying that it is a good thing that the entire world have finally stopped using local and limited charsets and decided to go all in on Unicode and UTF-8, so you should prefer nvarchar over varchar and ntext over text.

    And the return is nvarchar(4000) and not nchar(4000). Difference is that any varchar is - variable in size, while the plain type char is fixed in size. Returning tuples of char(4000) would be sending a lot of empty waste, but with varchar this is not an issue.

    Okay. But what would be a proper datatype be for you? I would recommend ntext. What was 1000 today could be 10'000 tomorrow. If you have 'a lot of text' that you are not indexing, then perhaps your database should not decide what the limit may be. It's just text.

    ntext also fits well with .NET, as its strings are always in Unicode. Conversion from string to int is also faster in .NET than done by sql server.

    Hope this helps

提交回复
热议问题