sql - Selecting default values when specific values don't exist in where clause -
i having difficulty finding way selecting column default value, when specific value doesn't exist.
select top 1 cold, cole table (cola = @cola or cola = 'default') , (colb = @colb or colb = 'default') , (colc = @colc or colc = 'default') the problem above query of course might return row 'default' value when specific value exists in table. there approved (hopefully clean) way this? think need have order in there, i'm not sure how best handle multiple columns.
i not have pass null value in parameter. want pass specific value in , check if specific value exists in column, if not return 'default' value.
if i'm understanding correctly, think need. included questions , explained assumptions within tsql comments.
--can null passed in of parameters? --adjust logic (or remove it) suit needs. if @cola null or @colb null or @colc null begin raiserror('<your stored proc name>: input params must non-null.', 16, 1); return; end --search row input search params. --(you're searching "exact match", right?) select top (1) cold, cole table cola = @cola , colb = @colb , colc = @colc --if not found, search row default cola/b/c values. if @@rowcount = 0 select top (1) cold, cole table cola = 'default cola value' , colb = 'default colb value' , colc = 'default colc value' --if still not found, raise error. if @@rowcount = 0 raiserror('<your stored proc name>: no matching row found , no default row found.', 16, 1);
Comments
Post a Comment