Database Localization - Lookup lists - smarter way

后端 未结 3 1290
感动是毒
感动是毒 2021-01-01 06:24

I\'m looking to add some lookup lists in the database, but I want them to be easy localizable (SQL 2005, ADO.NET)

This would include:

  • Easy Management
3条回答
  •  情歌与酒
    2021-01-01 06:51

    Since there are no user-defined global variables in SQL Server, you'll have to use one of two approaches:

    1. Tables - temporary or permanent. Example with permanent tables: http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx.
    2. SET CONTEXT_INFO: http://msdn.microsoft.com/en-us/library/ms187768.aspx. Context_info lets you associate 128 binary bytes to a session/connection. It works but be careful. If you get in the habit of using it, you run the risk of accidentally overwriting it in another context. There's only one per session/connection.

    Example context_info t-sql:

    declare @languagein varchar(30), @contextin varbinary(128),
        @languageout varchar(30), @contextout varbinary(128)
    
    select @languagein = 'ro-RO'
    select @contextin = cast(@languagein as varbinary(128))
    set context_info @contextin
    
    --do whatever you like here: queries, stored procs. 
    --context_info stays 'ro-RO' for the duration of the session/connection
    
    select @contextout = context_info()
    set @languageout = replace(cast(@contextout as varchar(30)),0x00, '')
    print @languageout
    

    Another technique I've used in localization is a three part coalesce to insure a result. Check for language-region first, then language, then a default. Based on your query:

    SELECT COALESCE(langregion.LookupValue, lang.LookupValue, fallback.LookupValue) LookupVal
    FROM LookupTable1 fallback
    LEFT OUTER JOIN LookupTable1 lang 
        ON lang.ID = fallback.ID AND lang.Lang = @language
    LEFT OUTER JOIN LookupTable1 langregion 
        ON langregion.ID = fallback.ID AND langregion.Lang = @languagewithregion
    WHERE fallback.ID = @Lookup_ID
    AND fallback.Lang = @defaultlanguage
    

提交回复
热议问题