Reputation: 59
I came across an if then else statement in oracle, but I am not sure what is the purpose of the two select count(*) statements. Can someone help me with that. I need to undestand this to re-write the same in t-sql. Thanks. Here's the code:
BEGIN
IF (nEL_ID > 0) THEN
select upperMatched||valueMatched INTO sResult
from
(
select count(*) as upperMatched
from HSIP.FY_ORT_VALUES ov
where ov.FY = nFY and OV.EL_ID = nEL_ID
and upper(OV.VAL_DESCRIPTION) = upper(sRESPONSE_STRING)
)um,
(
select count(*) as valueMatched
from HSIP.FY_ORT_VALUES ov
where ov.fy = nfy and OV.EL_ID = nEL_ID
and OV.VAL_DESCRIPTION = sRESPONSE_STRING
)lm;
ELSE
sResult := '00';
END IF;
RETURN sResult;
SQL Server:
CREATE FUNCTION [hsip].[getMatchedFlags]
(
@nFY NUMERIC(4,0) = 0,
@sResponse_String varchar(250),
@nEL_ID NUMERIC(5,0) = 0
)
RETURNS @sResult TABLE(FY NUMERIC(4,0),
Response_string varchar(250),
el_id numeric(5,0))
AS
BEGIN
insert into @sResult
--select * from (select case
SELECT CASE
when (@nEL_ID>0) then
SELECT(CONCAT(
(select count(*) as upperMatched
from HSIP.FY_ORT_VALUES ov
where ov.FY = @nFY and OV.EL_ID = @nEL_ID
and upper(OV.VAL_DESCRIPTION) = upper(@sRESPONSE_STRING)),
(SELECT COUNT(*) AS valueMatched
FROM hsip.FY_ORT_VALUES OV
WHERE OV.FY= @nFY AND OV.el_id= @nEL_ID
AND OV.VAL_DESCRIPTION= @sResponse_String))
ELSE
SET @SRESULT= 0;
return;
Upvotes: 1
Views: 49
Reputation:
um
and lm
are scalar subqueries (each returns a single row with exactly one column; in this case, the single value returned is a number, the count of something or another).
SQL (or at least Oracle SQL) allows you to use the result of scalar queries as expressions - in this case, as plain numbers. The "IF" branch sets the result sResult
as the concatenation of these two numbers (after converting them implicitly to strings).
That doesn't make much sense to me; if the first count is 399 and the second is 1, then the result will be the string '3991' (not a number, but a string). By the way, if the first count was 39 and the second 91, you would get exactly the same result.
Note - as discussed in comments below this answer, I missed the main point of the original question. xQbert explains the meaning of the counts themselves in a Comment to the original question.
Upvotes: 1