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
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3374 Visits: 1948
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
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59130 Visits: 17947
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
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39775 Visits: 19802
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
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3812 Visits: 3436
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