Miqe EqiM
Miqe EqiM

Reputation: 17

How to fetch data with php from a mssql procedure

Example:

CREATE PROCEDURE test (@age int) AS BEGIN
BEGIN TRAN

declare @flag int

if @age>20
begin
set @flag=1
end

else if @age<21
begin
set @flag=0
end

select @flag as flag

COMMIT TRAN
END

I need to fetch the @flag's value using php, how can I make this possible?

$row=mssql_fetch_array($r, MSSQL_ASSOC); $flag=$row['flag'];

EDIT: Never mind, it wasn't working because I forgot to delete some output queries in my procedure.

Thanks anyways ;)

Upvotes: 0

Views: 73

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

@flag is a local variable to the procedure. The best way to return a value is to use an output parameter:

CREATE PROCEDURE test (@age int, @flag int output) AS
BEGIN
    set @flag = (case when @age > 20 then 1 else 0 end);
END;

I don't see why a transaction would be necessary or desirable in this case.

Upvotes: 1

Related Questions