I have query which returns single value (i.e) count. I’m exceuting it using the stored procedure in the following way and using execute reader with dataset to get single value
CREATE PROCEDURE GetCnt
@EmpNo char(4)
AS
BEGIN
SET NOCOUNT ON;
Declare @Cnt int
SELECT @Cnt = count(*)
FROM employees
WHERE EMPLNO = @EmpNo
AND test = 'p'
BEGIN
SELECT @Cnt
END
END
is this effcient way
or Do I need to use the execute.scalar() and return value directly from the query instead of assigning to @cnt
can any one advise me
You do not need to create the variable. You just need the following:
Since this is only one value being returned from the stored procedure, you will likely want to use
ExecuteScalar()