I have a scenario in a very old and very large application, where I have a table representing a type of resource:
CREATE TABLE resource (resource_id INT, name NVARCHAR(4000)) This table is selected from in hundreds of different places, including stored procedures and dynamic SQL in the application code.
A team recently updated this resource's name to be localized, and their approach is pretty straight forward. There is a new table containing the localized names, and a 'default' language ID on the resource table, for when the name isn't localized for the requested language:
-- Foreign keys omitted ALTER TABLE resource ADD default_language_id INT CREATE TABLE resource_local (resource_id INT, language_id INT, name NVARCHAR(4000)) Most procs have an @user_language_id parameter, so the logic for choosing the name to return is simple: take resource_local.name matching language_id = @user_language_id if it exists, otherwise resource_local.name matching language_id = resource.default_language_id if it exists, otherwise take resource.name.
Unfortunately, this turns the logic to select the correct name into something like this:
SELECT ISNULL(ISNULL(exact.name, default.name), res.name) FROM resource res LEFT JOIN resource_local exact ON exact.resource_id = res.resource_id AND exact.language_id = @user_language_id LEFT JOIN resource_local default ON default.resource_id = res.resource_id AND default.language_id = res.default_language_id WHERE res.resource_id = @resource_id All of the hundreds of places that try to select resource.name are having to be updated with this logic, which has turned this project into a massive effort across the entire organization, as each team needs to update their SQL to use this logic. This also causes maintainability issues, as any new developers dealing with this table need to know that they can't just use the name column.
It's too late now, but I'm curious: is there any better way to approach this, so that selecting the name column from resource will just 'do the right thing' based on the @user_language_id variable (if it exists)?