sql server - SQL - select column if exists in OLEQuery -
at customers use older database have add column newer customers. program shall query new column if exists , use null if not.
using c# , olequery working far:
string sql = @"select i.[id] cid, pg.[name], pg.[id] pgid, i.[time], defect = case i.[defect] when 0 'ok' when 1 'nok' end, inspection (nolock) inner join programs pg on i.programid = pg.[id] i.[time] between ... ///some more code following here! ";
but want add (may/may not) existing column timeto.
string sql = @"select i.[id] cid, pg.[name], pg.[id] pgid, i.[time], defect = case i.[defect] when 0 'ok' when 1 'nok' end, timeto = case i.[timeto] when exists i.[timeto] else null end inspection (nolock) inner join programs pg on i.programid = pg.[id] i.[time] between ... //some more code following here! ";
i know difficult "exists" keyword there "one-line" solution? found several additional case select solutions. need solution in several places different columns case select solutions not fit. help
sql server won't allow reference field not exist. suspect because query optimizer cannot generate execution plan includes non-existent fields.
your app need to:
- identify version of db working with.
- choose appropriate queries.
- ensure handles missing data (either turning off functionality relies on new field or selecting appropriate default value).
Comments
Post a Comment