SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with an insert


Need help with an insert

Author
Message
sasken
sasken
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1978 Visits: 1933
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25954 Visits: 17519
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.

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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16383 Visits: 19076
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
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1766 Visits: 3432
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search