一个存储过程,我不会!!!请高手指教!!!分数好说!!!

一些运算表达式,只有加和减,例如;
"jed('501')+jed('502')-jej(522)"(项数不定)
我如何取出各个项存放到数组里,也就是把jed(501),jed(502),jej(522)取出放到一个数组里
在javascript中,String.split(分割符,数组)就可以实现这个功能
在sql sever 中有没有这样的函数,
如果没有,请大侠帮忙写一个
分数可以在加

---------------------------------------------------------------

存储过程中不能用数组,可以用游标代替,sql中用substring(),charindex()
---------------------------------------------------------------

sql 不支持数组的呀!:(
---------------------------------------------------------------

可以建立一个临时库,将数据插入:
create table #temp (
id int indentity(1,1),
jed int,
jej int )
---------------------------------------------------------------

你可以先从SQL读入到一个文件中,在JAVASCRIP读到数组中。
---------------------------------------------------------------

建一个临时表来代替数组。
select #n = 0
while(len(String)>#n) #n为当前所在位置
begin
if(charindex(String,#n)='+' or charindex(String,#n)='-')
begin
select #n = #n + 1
select #A = substring(String,#n,10)
insert #A into temptable
select #n = #n - 1
end
select #n = #n + 1
end

---------------------------------------------------------------

TO bluejam(蓝色的果酱):
修改了一下:)

select #n = 0
while(len(String)>#n) #n为当前所在位置
begin
if(charindex(String,#n)='+' or charindex(String,#n)='-')
begin
select #n = #n - 10
select #A = substring(String,#n,10)
insert #A into temptable
select #n = #n + 10
end
select #n = #n + 1
end

---------------------------------------------------------------

create function GetStrN1
(@St varchar(8000),
@C as int)
returns varchar(8000)
as
begin

declare @Result varchar(8000)
declare @Sp1 char(1),@Sp2 char(1)
declare @iTmp int

set @Sp1='+'
set @Sp2='-'

while @C - 1 > 0 begin
if dbo.GetFirstC(@St) > 0 begin

select @St = substring(@St, dbo.GetFirstC(@St) + 1, 100)

end
else
goto end_proc
select @C = @C - 1
end

end_proc:
if dbo.GetFirstC(@St) > 0

select @Result = left(@St, dbo.GetFirstC(@St) - 1)
else
select @Result = @St

return @Result
end

--其中函数GetFirstC如下
alter function GetFirstC
(@cInput as varchar(4000))
returns int
as
begin
declare @return int
if charindex('+',@cInput)>0
set @return=charindex('+',@cInput)
if charindex('-',@cInput)>0
set @return=charindex('-',@cInput)
if charindex('+',@cInput)>0 and charindex('-',@cInput)>0 and charindex('+',@cInput)<charindex('-',@cInput)
set @return=charindex('+',@cInput)
set @return=isnull(@return,0)
return @return
end

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus