CREATE PROCEDURE dbo.Sub_Str
@String1 Varchar(50)
AS
Begin --// A
Declare @S Varchar(10)
Declare @S1 Varchar(25),@S2 Varchar(25)
Declare @I Int
--初始化变量
Set @S1 = ''
Set @S2 = ''
Set @I=1
While @I<=DataLength(@String1)
Begin --// while
Set @S=SubString(@String1,@I,1)
If (Ascii(@S)>=Ascii('A') and Ascii(@S)<=Ascii('Z')) or (Ascii(@S)>=Ascii('a') and Ascii(@S)<=Ascii('z'))
Begin --// begin if
Set @S1=@S1+@S
End --// end if
Else
Begin --// begin if
Set @S2=@S2+Convert(Varchar(25), Convert(int,@S)+1 )
End --// end if
Set @I=@I+1
End --// End while
Print @S1+@S2
End -- // A
GO
@String1 Varchar(50)
AS
Begin --// A
Declare @S Varchar(10)
Declare @S1 Varchar(25),@S2 Varchar(25)
Declare @I Int
--初始化变量
Set @S1 = ''
Set @S2 = ''
Set @I=1
While @I<=DataLength(@String1)
Begin --// while
Set @S=SubString(@String1,@I,1)
If (Ascii(@S)>=Ascii('A') and Ascii(@S)<=Ascii('Z')) or (Ascii(@S)>=Ascii('a') and Ascii(@S)<=Ascii('z'))
Begin --// begin if
Set @S1=@S1+@S
End --// end if
Else
Begin --// begin if
Set @S2=@S2+Convert(Varchar(25), Convert(int,@S)+1 )
End --// end if
Set @I=@I+1
End --// End while
Print @S1+@S2
End -- // A
GO
--把一个符强制转换成整型,是不可以这么做的!
Set @S2=@S2+Convert(Varchar(25), Convert(int,@S)+1 )
@String1 Varchar(50)
AS
Begin --// A
Declare @S Varchar(10)
Declare @S1 Varchar(25),@S2 Varchar(25)
Declare @I Int
select @s1=0,@s2='' --必须加这一句
Set @I=1
While @I<=DataLength(@String1)
Begin --// while
Set @S=SubString(@String1,@I,1)
If (Ascii(@S)>=Ascii('A') and Ascii(@S)<=Ascii('Z')) or (Ascii(@S)>=Ascii('a') and Ascii(@S)<=Ascii('z'))
Begin --// begin if
Set @S1=@S1+@S
End --// end if
Else
Begin --// begin if
Set @S2=@S2+Convert(Varchar(25), Convert(int,@S)+1 )
End --// end if
Set @I=@I+1
End --// End while
--Print @S1+@S2 --这句有问题,最好先显示转换类型再相加
select @s1,@s2--新加的
End -- // A
GO
就是没有给变量初始值