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
sfazalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
does one of the variable contains NULL?
sfazalAuthor Commented:
if i comment the if statement, it will run.
Éric MoreauSenior .Net ConsultantCommented:
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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sfazalAuthor Commented:
Null to be inserted in the table column. I am passing it in the INSERT statement.
Éric MoreauSenior .Net ConsultantCommented:
Then you need something like this:

declare @sUserID varchar(100)

  IF @UserID = 0
                SET @sUserID = 'NULL'
else
               SET @sUserID = cast(@UserID as varchar(100))
   

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

EXEC (@sql)
    

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KipropCommented:
you cant cast NULL to nvarchar.
Vitor MontalvãoMSSQL Senior EngineerCommented:
sfazal, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.