SQL语句可以声明使用数组么?
4个回答
展开全部
sql没有数组类型的,不过你可以自定一个varchar类型的变量,里边装字符串,用逗号分开,然后进行分割就成了数组,
第一步:数组长度的函数
SQL code
Alter Function arrLen(@string varchar(1000),@SpaceMark Varchar(200))
--@String 眼拆分的字符串 @SpaceMark 分割符
Returns int
As
Begin
Declare @i Int
Set @i = (Len(@string) - Len(Replace(@string,@Spacemark,'')))/Len(@SpaceMark)
Return(@i)
End
第二步:按索引值取数组中的数据
SQL codeAlter Function arrList(@String varchar(8000),@SpaceMark Varchar(8000),@arrStation int)
returns varchar(8000)
As
Begin
Declare @newString varchar(8000)
Declare @ReturnString Varchar(8000)
Set @NewString = @String
Declare @i int
Set @i = 0
if(@arrStation>0 And @arrStation<=Dbo.arrLen(@String,@SpaceMark))
Begin
While(@i<@arrStation)
Begin
Set @NewString = SubString(@NewString,CharIndex(@SpaceMark,@NewString)+1,Len(@NewString))
Set @i = @i + 1
End
End
If(@arrStation<0)
Begin
Set @ReturnString = '上标越界'
End
Else If(@arrStation<dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = SubString(@NewString,1,CharIndex(@SpaceMark,@NewString)-1)
End
Else if(@arrStation=dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = @newString
End
Else
Begin
Set @ReturnString = '下标越界'
End
Return(@ReturnString)
End
测试程序:
Declare @a varchar(1000)
Declare @i int
Set @i = 0
Set @a = 'A,B,C,D,E,F,G,H,I'
While(@i <=Dbo.arrLen(@a,','))
Begin
Print Dbo.arrList(@a,',',@i)
Set @i = @i + 1
ENd
第一步:数组长度的函数
SQL code
Alter Function arrLen(@string varchar(1000),@SpaceMark Varchar(200))
--@String 眼拆分的字符串 @SpaceMark 分割符
Returns int
As
Begin
Declare @i Int
Set @i = (Len(@string) - Len(Replace(@string,@Spacemark,'')))/Len(@SpaceMark)
Return(@i)
End
第二步:按索引值取数组中的数据
SQL codeAlter Function arrList(@String varchar(8000),@SpaceMark Varchar(8000),@arrStation int)
returns varchar(8000)
As
Begin
Declare @newString varchar(8000)
Declare @ReturnString Varchar(8000)
Set @NewString = @String
Declare @i int
Set @i = 0
if(@arrStation>0 And @arrStation<=Dbo.arrLen(@String,@SpaceMark))
Begin
While(@i<@arrStation)
Begin
Set @NewString = SubString(@NewString,CharIndex(@SpaceMark,@NewString)+1,Len(@NewString))
Set @i = @i + 1
End
End
If(@arrStation<0)
Begin
Set @ReturnString = '上标越界'
End
Else If(@arrStation<dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = SubString(@NewString,1,CharIndex(@SpaceMark,@NewString)-1)
End
Else if(@arrStation=dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = @newString
End
Else
Begin
Set @ReturnString = '下标越界'
End
Return(@ReturnString)
End
测试程序:
Declare @a varchar(1000)
Declare @i int
Set @i = 0
Set @a = 'A,B,C,D,E,F,G,H,I'
While(@i <=Dbo.arrLen(@a,','))
Begin
Print Dbo.arrList(@a,',',@i)
Set @i = @i + 1
ENd
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-30
展开全部
sql没有数组类型的,不过你可以自定一个varchar类型的变量,里边装字符串,用逗号分开,然后进行分割就成了数组,第一步:数组长度的函数
SQL code
Alter Function arrLen(@string varchar(1000),@SpaceMark Varchar(200))
--@String 眼拆分的字符串 @SpaceMark 分割符
Returns int
As
Begin
Declare @i Int
Set @i = (Len(@string) - Len(Replace(@string,@Spacemark,'')))/Len(@SpaceMark)
Return(@i)
End
第二步:按索引值取数组中的数据
SQL code Alter Function arrList(@String varchar(8000),@SpaceMark Varchar(8000),@arrStation int)
returns varchar(8000)
As
Begin
Declare @newString varchar(8000)
Declare @ReturnString Varchar(8000)
Set @NewString = @String
Declare @i int
Set @i = 0
if(@arrStation>0 And @arrStation<=Dbo.arrLen(@String,@SpaceMark))
Begin
While(@i<@arrStation)
Begin
Set @NewString = SubString(@NewString,CharIndex(@SpaceMark,@NewString)+1,Len(@NewString))
Set @i = @i + 1
End
End
If(@arrStation<0)
Begin
Set @ReturnString = '上标越界'
End
Else If(@arrStation<dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = SubString(@NewString,1,CharIndex(@SpaceMark,@NewString)-1)
End
Else if(@arrStation=dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = @newString
End
Else
Begin
Set @ReturnString = '下标越界'
End
Return(@ReturnString)
End
测试程序:
Declare @a varchar(1000)
Declare @i int
Set @i = 0
Set @a = 'A,B,C,D,E,F,G,H,I'
While(@i <=Dbo.arrLen(@a,','))
Begin
Print Dbo.arrList(@a,',',@i)
Set @i = @i + 1
ENd
SQL code
Alter Function arrLen(@string varchar(1000),@SpaceMark Varchar(200))
--@String 眼拆分的字符串 @SpaceMark 分割符
Returns int
As
Begin
Declare @i Int
Set @i = (Len(@string) - Len(Replace(@string,@Spacemark,'')))/Len(@SpaceMark)
Return(@i)
End
第二步:按索引值取数组中的数据
SQL code Alter Function arrList(@String varchar(8000),@SpaceMark Varchar(8000),@arrStation int)
returns varchar(8000)
As
Begin
Declare @newString varchar(8000)
Declare @ReturnString Varchar(8000)
Set @NewString = @String
Declare @i int
Set @i = 0
if(@arrStation>0 And @arrStation<=Dbo.arrLen(@String,@SpaceMark))
Begin
While(@i<@arrStation)
Begin
Set @NewString = SubString(@NewString,CharIndex(@SpaceMark,@NewString)+1,Len(@NewString))
Set @i = @i + 1
End
End
If(@arrStation<0)
Begin
Set @ReturnString = '上标越界'
End
Else If(@arrStation<dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = SubString(@NewString,1,CharIndex(@SpaceMark,@NewString)-1)
End
Else if(@arrStation=dbo.arrLen(@String,@SpaceMark))
Begin
Set @ReturnString = @newString
End
Else
Begin
Set @ReturnString = '下标越界'
End
Return(@ReturnString)
End
测试程序:
Declare @a varchar(1000)
Declare @i int
Set @i = 0
Set @a = 'A,B,C,D,E,F,G,H,I'
While(@i <=Dbo.arrLen(@a,','))
Begin
Print Dbo.arrList(@a,',',@i)
Set @i = @i + 1
ENd
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询