Need help with an insert

  • Hello,

    I am trying to insert into a log table that stores the reason behind the non execution of a stored proc. The stored proc runs only if a specific condition is met and for that to execute I am using an if condition. Now the log table comes into picture in the else part and here is what I have in my code for logging:

    else

    select @ID1=ID from [CBH] where [SID] = 2

    select @ID2=ID from [PBH] where [SID] = 2

    INSERT into dbo.Logtable

    (

    AdditionalInfo

    )

    values('Another Process with ID:'+@ID1+'is currently in use')

    INSERT into dbo.Logtable

    (

    AdditionalInfo

    )

    values('Another Process with ID:'+@ID2+'is currently in use')

    END

    end

    But what I really want to do is:

    If @ID1 exists then insert that value in to the logtable

    and if @ID2 exists then insert that value in to the logtable.

    If both @ID1 and @ID2 exists then insert both into the logtable with the same text.

    Please note that the @ID1 and @ID2 are of type bigint and I am trying to insert that along with the text into the log table.

    Thanks for your help on this.

    “If your actions inspire others to dream more, learn more, do more and become more, you are a leader.” -- John Quincy Adams

  • How about some basic ddl and a little bit bigger piece of the puzzle? The details are so sparse we are left guessing what it is you want to do.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This is just a guess based on what you posted.

    else

    BEGIN

    SELECT @ID1 = NULL,

    @ID2 = NULL

    select @ID1=ID from [CBH] where [SID] = 2

    select @ID2=ID from [PBH] where [SID] = 2

    IF @ID1 IS NOT NULL

    INSERT into dbo.Logtable

    (

    AdditionalInfo

    )

    values('Another Process with ID:'+CAST(@ID1 AS varchar(20))+'is currently in use')

    IF @ID2 IS NOT NULL

    INSERT into dbo.Logtable

    (

    AdditionalInfo

    )

    values('Another Process with ID:'+CAST(@ID2 AS varchar(20))+'is currently in use')

    END

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • INSERT INTO

    dbo.Logtable (AdditionalInfo)

    SELECT

    'Another Process with ID: ' + CAST(ID AS VARCHAR(25)) + ' is currently in use')

    FROM

    [CBH]

    WHERE

    [SID] = 2;

    INSERT INTO

    dbo.Logtable (AdditionalInfo)

    SELECT

    'Another Process with ID: ' + CAST(ID AS VARCHAR(25)) + ' is currently in use')

    FROM

    [PBH]

    WHERE

    [SID] = 2;

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply