Sql 服务器游标结果显示不正确?

Sql server cursor results not displayed properly?

请帮忙解答。为什么我的 CURSOR 结果显示不正确?查看两张照片。

        declare @FullName varchar;
        declare @InvoiceID int;
        declare @CustomerID int;
        declare @DeliveryInstructions varchar;
        declare Salescur cursor for
        --select---
            select p.FullName, InvoiceID,CustomerID,DeliveryInstructions
            from Sales.Invoices s 
            inner join Application.People p
            on s.SalespersonPersonID = p.PersonID
            where InvoiceDate = '2016-05-31'
            and s.SalespersonPersonID = 8;

        open Salescur;
        fetch next from Salescur into @FullName,@InvoiceID,@CustomerID,
        @DeliveryInstructions;
        while @@FETCH_STATUS = 0
        begin 
            --print @InvoiceID + CHAR(13);
            print @FullName + 'has delivery instructions to these addresses: ' + @DeliveryInstructions
            fetch next from Salescur into @FullName,@InvoiceID,@CustomerID,
        @DeliveryInstructions;
        end
        close Salescur;
        deallocate Salescur;
    end;

Cursor Result

Table selected

为什么 你正在使用 cursor,你也可以通过 concat() 功能

select concat(p.FullName, ' has delivery instructions to these addresses: ', p.DeliveryInstructions)
from Sales.Invoices s 
inner join Application.People p 
      on s.SalespersonPersonID = p.PersonID
where InvoiceDate = '2016-05-31' and 
      s.SalespersonPersonID = 8;

对于旧的版本你可以使用

select p.FullName + ' has delivery instructions to these addresses: ' +  p.DeliveryInstructions 
. . .

正如 Jeroen 所说,您已将变量声明为 VARCHAR 类型,但尚未指定字段长度。这将导致长度为 1。

你需要做的

DECLARE @FullName VARCHAR(50)

通过

DECLARE @FullName VARCHAR

你是在含蓄地做

DECLARE @FullName VARCHAR(1)

这就是为什么你只得到第一个字符