Link to home
Start Free TrialLog in
Avatar of sfazal
sfazalFlag for Canada

asked on

SQL Server Stored Procedure.

Hi.
I am trying TO call this sp AND it dont EXECUTE the @sql STATEMENT, IF i look it IN watch its EMPTY. what could be the reaosn FOR it.

EXEC Update_Transaction  '22','0',0

CREATE PROCEDURE [dbo].[Update_Transaction]
    @TransactionEntryID INT ,
    @UserID INT ,
    @ID INT OUT
AS
    BEGIN

      DECLARE       @sql VARCHAR(max);

    SET NOCOUNT ON;
        IF @UserID = 0
            BEGIN

                SET @UserID = NULL;

            END
               
                  BEGIN            

SET @sql = 'INSERT  INTO + table1' +
                '( TransactionEntryID ,
                  UserID ,
               
                )
   VALUES ('+ cast(@TransactionEntryID as varchar(100))+', '+ cast(@UserID as varchar(100))   +')'

                                                       EXEC (@sql)
END      
    END
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

does one of the variable contains NULL?
Avatar of sfazal

ASKER

if i comment the if statement, it will run.
so your statement does not run when @UserId = 0?

This is because you are concatenating a NULL with a string and the NULL is propagating (turning everything to NULL).

Are you trying to send a NULL to your SP?
Avatar of sfazal

ASKER

Null to be inserted in the table column. I am passing it in the INSERT statement.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you cant cast NULL to nvarchar.
sfazal, do you still need help with this question?