Home [SQL] 輸出到畫面 (Print)
Post
Cancel

[SQL] 輸出到畫面 (Print)

Print向用戶端傳回使用者自訂訊息。

SQL SERVER可以使用Print語法來將字串輸出到畫面上。雖然也可以使用select語法來達成此目的,不過每執行一次select才顯示一個項目,如果要顯示處理的過程,使用Print比較好。

語法

1
PRINT msg_str | @local_variable | string_expr 

範例

製作一個會將處理過程輸出到畫面的預存程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
--製作一個會將處理過程輸出到畫面的預存程序
create or alter proc uspTest
as
    declare @i int
begin
    print 'uspTest start ---'
    set @i = 1
    while (@i < 10)
    begin
        print 'i=' + str(@i)
        set @i += 1
    end
    print 'uspTest end ---'
end

--執行sp
exec uspTest

輸出結果:

1
2
3
4
5
6
7
8
9
10
11
uspTest start ---
i=         1
i=         2
i=         3
i=         4
i=         5
i=         6
i=         7
i=         8
i=         9
uspTest end ---

MSDN - PRINT (Transact-SQL)

This post is licensed under CC BY 4.0 by the author.