How To Call A Function In SQL Server Stored procedure

Step 1. Create a table or use an existing table. In my case, I use a table that has columns, username, and password. You also need to know how to create and execute a stored procedure. If you're new to SPs, read this before this article, Learn everything about stored procedures in SQL Server.

Step 2. The create function SQL query is used to create a new function. Create a function using the given query. 

CREATE FUNCTION function_to_be_called (@username VARCHAR(200))
RETURNS VARCHAR(100)
AS
BEGIN
    DECLARE @password VARCHAR(200)
    SET @password = (SELECT [password] FROM [User] WHERE username = @username)
    RETURN @password
END

Step 3. The create procedure query is used to create a stored procedure. Create a procedure using the given query. 

CREATE PROCEDURE chek_pass
    @user VARCHAR(200)
AS
BEGIN
    DECLARE @pass VARCHAR(200)
    SET @pass = dbo.function_to_be_called(@user)
    SELECT @pass
END

Step 4. Test it. Use exe to execute a stored procedure. 

exec chek_pass 'username'  

Step 5. Enjoy the code.

This is a simple query. You can try it with more complex queries and even put your business logic in database functions and reduce front-end UI work.

Here are some recommended articles: