Can store procedure take output parameter
- User
Can store procedure take output parameter
- Assistant
Yes, stored procedures in databases can take output parameters. In many database systems, such as Microsoft SQL Server, MySQL, and Oracle, you can define parameters in a stored procedure that are designated as output parameters. These output parameters allow you to return values from the stored procedure back to the calling program or script.
Here’s a brief example for different database systems:
SQL Server Example
sqlCREATE PROCEDURE GetEmployeeCount @DepartmentId INT, @EmployeeCount INT OUTPUT AS BEGIN SELECT @EmployeeCount = COUNT(*) FROM Employees WHERE DepartmentId = @DepartmentId; ENDTo call this stored procedure and retrieve the output parameter, you would do:
sqlDECLARE @Count INT; EXEC GetEmployeeCount @DepartmentId = 1, @EmployeeCount = @Count OUTPUT; PRINT @Count; -- This will print the employee count for department 1MySQL Example
In MySQL, you can use
OUTparameters:sqlDELIMITER // CREATE PROCEDURE GetEmployeeCount(IN DepartmentId INT, OUT EmployeeCount INT) BEGIN SELECT COUNT(*) INTO EmployeeCount FROM Employees WHERE DepartmentId = DepartmentId; END // DELIMITER ;To call this procedure:
sqlCALL GetEmployeeCount(1, @Count); SELECT @Count; -- This will retrieve the employee count for department 1Oracle Example
In Oracle, you can define output parameters using the
OUTkeyword:sqlCREATE OR REPLACE PROCEDURE GetEmployeeCount( p_DepartmentId IN NUMBER, p_EmployeeCount OUT NUMBER ) AS BEGIN SELECT COUNT(*) INTO p_EmployeeCount FROM Employees WHERE DepartmentId = p_DepartmentId; END;