Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help with an insert Expand / Collapse
Author
Message
Posted Thursday, June 13, 2013 1:45 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, October 9, 2014 9:38 AM
Points: 1,354, Visits: 1,750
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
Post #1463287
Posted Thursday, June 13, 2013 1:48 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:46 AM
Points: 13,078, Visits: 12,528
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1463290
Posted Thursday, June 13, 2013 2:00 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:24 PM
Points: 3,747, Visits: 8,411
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1463296
Posted Friday, June 14, 2013 7:41 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 8:18 AM
Points: 938, Visits: 2,928
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

@SeanPearceSQL

About Me
Post #1463572
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse