June 13, 2013 at 1:45 pm
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
June 13, 2013 at 1:48 pm
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/
June 13, 2013 at 2:00 pm
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
June 14, 2013 at 7:41 am
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;
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply