Click here to Skip to main content
15,909,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a stored procedure with a while loop in it and I want some messages to be printed after every loops...Procedure Executed Successfully....But I want to Print Message after every Loop...

Create Procedure [dbo].[XXXX_SP]
(
@XXX DateTime,
@YYY varchar(10) )
As
Begin
Set @TotCnt=ceiling(@TotCnt)
    While @Cnt= @TotCnt
    Begin
	Print 'File Processing Started '+Str(@Cnt)
Execute XXXXXX  ()       
 Set @SqlQry =Select  b.Zone,b.Division,a.* into  from test a left join final b on a.[aaa]=b.aaa
     Exec (@SqlQry)
     Set @Cnt=@Cnt+1
    End
End
Posted

1 solution

Instead of using PRINT use a feature of RAISEERROR called NOWAIT e.g.
SQL
RAISERROR 'File Processing Stated ' + Str(@Cnt), 0, 1) WITH NOWAIT


Using the 0 means that sql treats the "error" like a straightforward message.

These links give more details
http://www.mssqltips.com/sqlservertip/1660/using-the-nowait-option-with-the-sql-server-raiserror-statement/[^]
http://msdn.microsoft.com/en-us/library/ms178592.aspx[^]
http://www.sommarskog.se/error-handling-I.html[^]

[Edit] - Another technique I've used in the past is to have a "logging table" on the same database ... in this instance replace the print with insert into logtable values('File Processing Started' + Str(@Cnt)) - and consider either clearing the table down each time or using having timestamp column autogenerated
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900