sql - Implementing 'exclusive or' to return one record -


i'm attempting create stored procedure implements exclusive or return specific record table (basisvalues).

here's table:

basis   value 1       4.25 2       1.25 3       99.00 0       0.00 

i've designed stored procedure return basisvalue follows:

create procedure getbasisvalue                     @basis varchar(2) begin     select value basisvalues     ( basis  = @basis) or          ((basis <> @basis) , (basis = '0')) end 

as can see above, i'm attempting select value field basisvalues table according following logic:
1. if basis equal @basis passed stored procedure, return value.
2. if basis not equal @basis passed stored procedure, return value of basis = 0.

however, given 'basis' exists in table, end getting 2 records. example, if called get_basis_values @basis of 2 get:

basis   value 2   1.25 0   0.00 

in other words, return '0' record no matter what. missing in terms of logic?

you want use order by select prefered value:

select top 1 value basisvalues ( basis  = @basis) or        ((basis <> @basis) , (basis = '0')) order (case when basis = @basis 1 else 0 end) desc; 

this query (without top) return 1 or 2 values, based on describe in question. order results, the non-default first. choosing first row after order by, match first, , default if there no match.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -