Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

Print issue of the SQL Server stored procedure

I have a problem about that I will get reverse sequence message if the stored procedure called from linked server. I'm much appreciated if anyone know the root cause or provide a solution. Thanks in advance.

This is my test code:

CREATE PROCEDURE [dbo].[TestSP]
AS
BEGIN
    SET NOCOUNT ON;

    PRINT '1'
    PRINT '2'
    PRINT '3'
END

Calling it like this:

EXEC [dbo].[TestSP] (call by local)

Output:

1
2
3

It will show output with reverse order if executed by another linked server.

For example,

EXEC [XXX.XXX.XXX.XXX].[dbo].[TestSP]

Output:

3
2
1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Print is a debug command not a functionnal one. If you want to have a consistant order of the sequencement, you must use the RAISERROR at level 10 with the keyword NOWAIT.

CREATE PROCEDURE [dbo].[TestSP]
AS
BEGIN
    SET NOCOUNT ON;
    RAISERROR('1', 10, 1) WITH NOWAIT;
    RAISERROR('2', 10, 1) WITH NOWAIT;
    RAISERROR('3', 10, 1) WITH NOWAIT;
END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...