我有一个数组 string[] arry = ['aa',"bb","CC"',"DD",......] 有一数据表 table1 ,其中有一字段cont 原来默认为空,现在要用数组中的值 aa 替换cont的第一条空记录,用bb替换cont 的第二条空记录,用cc替换cont 的第三条空记录.......以此类推。如果数组值用完,空记录还没更新完,则又从aa 开始,继续往下填充,直到填空完为止。这个语句该怎么写。请大侠们指教。学生不胜感激。
谢谢老师。只不过学生水平低,看不懂。能不能按我说的那个条件,帮我写个完整的代码。最好是c#的我好懂些,然后我对照自己的需求慢慢领悟学习。学生也少用百度提问,没有分送给老师。很是抱歉。希望继续得到老师的指点。谢谢
/*数组函数1-数组长度*/
create function Get_StrArrayLength
(
@str varchar(5000), --要分割的字符串
@split varchar(10) --分隔符号
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end
/*数组函数2-数组元素*/
create function Get_StrArrayStrOfIndex
(
@str varchar(5000), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(5000)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
return substring(@str,@start,@location-@start)
end
/*测试数据*/
CREATE TABLE #t1(
id INT IDENTITY(1,1),
cont VARCHAR(100)
)
DECLARE @t INT
SET @t=0
WHILE @t<20
begin
INSERT INTO #t1(cont) VALUES(null)
SET @t+=1
end
DECLARE @sr VARCHAR(100)
DECLARE @n int
DECLARE @m INT
DECLARE @i int
SET @sr='a,b,c,d,e'
SET @n=dbo.Get_StrArrayLength(@sr,',')
SELECT @m=count(*) FROM #t1
SET @i=0
WHILE @i<=@m
BEGIN
UPDATE #t1 SET cont=dbo.Get_StrArrayStrOfIndex(@sr,',',(@i % @n)+1) WHERE id=@i+1
SET @i+=1
end
/*效果*/追问
create function Get_StrArrayLength
(
@str varchar(5000), --要分割的字符串
@split varchar(10) --分隔符号
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end
/*数组函数2-数组元素*/
create function Get_StrArrayStrOfIndex
(
@str varchar(5000), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(5000)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
return substring(@str,@start,@location-@start)
end
/*测试数据*/
CREATE TABLE #t1(
id INT IDENTITY(1,1),
cont VARCHAR(100)
)
DECLARE @t INT
SET @t=0
WHILE @t<20
begin
INSERT INTO #t1(cont) VALUES(null)
SET @t+=1
end
DECLARE @sr VARCHAR(100)
DECLARE @n int
DECLARE @m INT
DECLARE @i int
SET @sr='a,b,c,d,e'
SET @n=dbo.Get_StrArrayLength(@sr,',')
SELECT @m=count(*) FROM #t1
SET @i=0
WHILE @i<=@m
BEGIN
UPDATE #t1 SET cont=dbo.Get_StrArrayStrOfIndex(@sr,',',(@i % @n)+1) WHERE id=@i+1
SET @i+=1
end
/*效果*/追问
谢谢老师,如果数据表经过长期增删,ID已经不连续了,比如ID 有可能是100 。112,115 ,120,123 这个程序还适用吗。
温馨提示:答案为网友推荐,仅供参考