If else in stored procedure sql server

sql server stored procedure if statement multiple conditions
if else in sql stored procedure select statement
sql if else begin end
how to check if condition in stored procedure
how to use if else condition in stored procedure in sql server 2008
if else in trigger sql server
sql nested if statement in select
if else in stored procedure mysql

I have created a stored procedure as follow:

Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
(
@ParLngId int output
)
as
Begin
    SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
    if(@ParLngId = 0)
        begin
            Insert Into T_Param values ('PHY', 'Extranet Client', Null, Null, 'T', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL)
            SET @ParLngId = @@IDENTITY
        End
    Return @ParLngId
End

So I set a variable @ParLngId, I check if there is a such data in a table, if yes, I return the value, if not I insert one and return the variable which contains the Id of the inserted line... But now it shows me an SqlException:

Subquery returned more values. This is not permitted when the subquery follows =,! =, <, <=,>,> = Or when used as an expression.

Somebody has a solution ?

Thank you all for your answers but I figured out how to do it and the final procedure looks like that :

Create Procedure sp_ADD_RESPONSABLE_EXTRANET_CLIENT
(
@ParLngId int output
)
as
Begin
if not exists (Select ParLngId from T_Param where ParStrIndex = 'RES' and ParStrP2 = 'Web')
    Begin
            INSERT INTO T_Param values('RES','¤ExtranetClient', 'ECli', 'Web', 1, 1, Null, Null, 'non', 'ExtranetClient', 'ExtranetClient', 25032, Null, 'informatique.interne@company.fr', 'Extranet-Client', Null, 27, Null, Null, Null, Null, Null, Null, Null, Null, 1, Null, Null, 0 )
            SET @ParLngId = @@IDENTITY
    End
Else
    Begin
            SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
            Return @ParLngId
    End   
End

So the thing that I found out and which made it works is:

if not exists

It allows us to use a boolean instead of Null or 0 or a number resulted of count()

SQL Server IF ELSE Statement By Examples, Home / SQL Server Stored Procedures / SQL Server IF ELSE The IFELSE statement is a control-flow statement that allows you to execute or skip a statement  Programming a Stored Procedure. If you’re like most casual user of T-SQL then you’re you’ve mostly likely copied some queries into a query windows and ran it to see results. The queries were running from top to bottom, one statement after another, in sequence.

If there are no matching row/s then @ParLngId will be NULL not zero, so you need IF @ParLngId IS NULL.

You should also use SCOPE_IDENTITY() rather than @@IDENTITY.

IFELSE (Transact-SQL), Thank you all for your answers but I figured out how to do it and the final procedure looks like that : Create Procedure  Yes but When you add a procedure in sql server, it verifies first if there is not a procedure with the same name before adding it – Hubert Solecki Sep 3 '13 at 7:33 |

Try this one -

CREATE PROCEDURE sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
AS
BEGIN

    DECLARE @ParLngId INT
    SELECT TOP 1 @ParLngId = ParLngId
    FROM dbo.T_Param
    WHERE ParStrNom = 'Extranet Client'

    IF (@ParLngId = 0)
    BEGIN
        INSERT INTO dbo.T_Param
        VALUES ('PHY', 'Extranet Client', NULL, NULL, 'T', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL)

        RETURN SCOPE_IDENTITY()
    END
    ELSE BEGIN

        RETURN @ParLngId 

    END

END

If else in stored procedure sql server, Here we have to use condition with where clause instance of ELSE-IF Statement in stored procedure. Consider we will fetch Unit Price and  IFELSE Statement using in execution of a Transact-SQL statement (Store Procedure or T-SQL) and Trigger. IF tests can be nested after another IF or following an ELSE. There is no limit to the number of nested levels. IF condition is satisfied and the Boolean expression returns TRUE, it will executed IF Block SQL statement.

if not exists (select dist_id from tbl_stock where dist_id= @cust_id and item_id=@item_id)
    insert into tbl_stock(dist_id,item_id,qty)values(@cust_id, @item_id, @qty); 
else
    update tbl_stock set qty=(qty + @qty) where dist_id= @cust_id and item_id= @item_id;

How To Avoid ELSE-IF Statement In Stored Procedure In SQL Server, statement structure is used to select one of the specified statements to be executed based on pecified Boolean conditions. IF condition_1 statement_1; ELSE IF condition_2 statement_2; . Transact-SQL Understanding Stored Procedures. Check If Stored Procedure Exists, Else Drop It and Recreate – SQL Server. We tend to forget the most basic syntax of all and one of them is to check if a stored procedure exists and then drop it. Here’s the syntax for your reference:

You do not have to have the RETURN stament.

Have anther look at Using a Stored Procedure with Output Parameters

Also have another look at the OUT section in CREATE PROCEDURE

SQL Server FAQ - "IF ELSE IF ELSE , IF condition is not satisfied and the Boolean expression returns FALSE, it will executed ELSE Block SQL Statement query. --CREATE PROCEDURE CREATE​  A menos que um bloco de instruções seja usado, a condição IF ou ELSE poderá afetar o desempenho de somente uma instrução Transact-SQL Transact-SQL. Unless a statement block is used, the IF or ELSE condition can affect the performance of only one Transact-SQL Transact-SQL statement.

Checking IF…ELSE condition in Stored Procedure SQL Server, Stored Procedure - IF ELSE IF ELSE by Professor Saad Yousuf DB2 UDB, Oracle​, SQL Server Duration: 3:13 Posted: Mar 28, 2017 Is any Transact-SQL statement or statement grouping as defined by using a statement block. Unless a statement block is used, the IF or ELSE condition can affect the performance of only one Transact-SQL statement.

Stored Procedure - IF ELSE IF ELSE, This article explores the SQL IF statement and its usage with examples to write This article explores the useful function SQL IF statement in SQL Server. a variable in a Boolean expression and multiple IF statements function · Debugging stored procedures in SQL Server Management Studio (SSMS)  สร้างเงื่อนไข (SQL Server : Stored Procedure) ในหัวข้อนี้เราจะมาเรียนรู้การใช้งาน IF บน Stored Procedure เพราะคำสั่งนี้เป็นการสร้างเงื่อนไขในการทำงานที่ได้ค่ามาจาก Parameters และ Variable และมันค่อนข้างจะมีความจำเป็นและสำคัญมากในการเขียน Stored และ สามารถนำคำสั่ง If ไปประยุกต์การใช้งานได้หลากหลายในทุก ๆ ส่วนของการเขียน Stored Procedure

SQL IF Statement introduction and overview, MySQL IF ELSE statement implements a basic conditional construct when the expression evaluates to false. Its syntax is as follows:IF  I am trying to make a Stored Procedure in SQL Server 2008 R2 That Takes 3 Parameters (AnalyserID,Date_From,Date_To) Actually, The User Might not send the three parameters so, When the stored procedure is executed it might have 1 parameter value or 2 or three or not taken any parameters at all. I designed the stored procedure as Follows

Comments
  • "an error saying that different values have been returned" - could you edit in the exact text of the error message for us please?
  • Can you copy/paste the exact error message?
  • I've just edited the post
  • CREATE PROCEDURE: "Avoid the use of the sp_ prefix when naming procedures. This prefix is used by SQL Server to designate system procedures. Using the prefix can cause application code to break if there is a system procedure with the same name."
  • Yes but When you add a procedure in sql server, it verifies first if there is not a procedure with the same name before adding it
  • @@Identity works. It works before when I didn't need to use the Else If, I don't think that problem comes from it