将过程输出传递给变量

时间:2013-12-24 20:43:56

标签: sql-server

我是SQL服务器的新手。我将过程输出传递给变量但总是得到“NULL”。这个程序有什么问题吗?我测试将etext的初始值声明为'a',它返回一个。

  create procedure encrypt 
    @ptext as varchar(500)
    , @etext as varchar(500) OUTPUT
  as 
  begin
set nocount on
declare @key as tinyint = 3
declare @pc as varchar(1)
declare @i as smallint = 1
declare @n as smallint

set @n = len(@ptext)

while @i <= @n
begin 
set @pc = substring (@ptext, @i, 1)

if ascii(@pc) between 48 and 57 
begin
    if ascii(@pc) + @key < 58
        set @pc = char((ascii(@pc) + @key))
    else 
        set @pc = char((ascii(@pc) + @key)-10)
end

else if ascii(@pc) between 65 and 90 
begin
    if ascii(@pc) + @key < 91
        set @pc = char((ascii(@pc) + @key))
    else 
        set @pc = char((ascii(@pc) + @key)-26)
end

if ascii(@pc) between 97 and 122 
begin
    if ascii(@pc) + @key < 123
        set @pc = char((ascii(@pc) + @key))
    else 
        set @pc = char((ascii(@pc) + @key)-26)
end

set @etext = @etext + @pc
set @i = @i + 1
end 
end


DECLARE @etext char(500);
exec encrypt 'time', @etext OUTPUT
select @etext 

1 个答案:

答案 0 :(得分:2)

当您在set @etext = @etext + @pc中连接字符串时,您需要确保@etext不为null,否则结果将为null。

将该行更改为

et @etext = isnull(@etext, '') + @pc

通过这种方式,您可以将null传递给您的过程,它仍然有效。