您可以使用光标:
declare @curse cursor
set @curse = cursor fast_forward for
select ID, FirstName, LastName
from Customers
declare @id int,
@firstName varchar(32),
@lastName varchar(32)
open @curse
fetch next from @curse into @id, @firstName, @lastName
while (@@fetch_status = 0)
begin
Print N'Customer ID: ' + @id
Print N'First Name: ' + @firstName
Print N'Last Name: ' + @lastName
fetch next from @curse into @id, @firstName, @lastName
end
close @curse
或者你可以不这样做:
select 'Customer ID: ' + cast(ID as varchar(32)) + char(13) + char(10) +
'First Name: ' + FirstName + char(13) + char(10) +
'Last Name: ' + LastName + char(13) + char(10)
from Customers
如果您使用Ctrl + T
第二种方式,用于将 SSMS 中的输出从网格模式更改为文本模式。