As @ZafaSultan has pointed out, there is an error with
PRINT 'in cursor MsgId : ' + (VARCHAR(50), @msgid)
Quote:
'VARCHAR' is not a recognized built-in function name.
It should read
PRINT 'in cursor MsgId : ' + CAST(@msgid AS VARCHAR)
You are saying it is "unresponsive" but you only have PRINT statements in your cursor - remember that the output will appear on the "Messages" tab in SSMS, not in the "Results" tab.
If you want to see the results immediately then use
RAISERROR('in cursor MsgId : ' + CAST(@msgid AS VARCHAR),0,1) WITH NOWAIT
It won't stop your SP from running (as a normal error would)
Are you sure that
SELECT TOP 1 MsgID FROM HistoryMsgID
is returning a value <= 178370450 ? You have no WHERE or ORDER BY clause on that query ... perhaps you meant
SELECT MAX(MsgID) FROM HistoryMsgID
Apart from the above points there is nothing wrong with your code - apart from the fact you are using a CURSOR. As this is all to make the output visible, just use the select to get the results into the Results tab.