如何用循环增加varchar

时间:2017-10-12 09:04:49

标签: sql-server-2008 tsql while-loop

declare @id int, @tmp_Code varchar(30), @TotalCount int
set @id=0
set @tmp_Code='ABC3-DE6-FG'
set @TotalCount=(select COUNT(*) from tbl_Code where TL_No='233246579836')
while (@id<@TotalCount)
begin
set @tmp_Code=@tmp_Code+ CONVERT(varchar(20),@id )
update [Demo_DB].[dbo].[tbl_Code]
set [tmp_Code] = @tmp_Code
where TL_No='233246579836'
set @id=@id+1
end

我已搜索并将此代码放在一起以自动增加附加到我的值的int。但我继续使用上述代码的价值是;

ABC3-DE6-FG0123456789101112131,
ABC3-DE6-FG0123456789101112131,
ABC3-DE6-FG0123456789101112131,
ABC3-DE6-FG0123456789101112131,
ABC3-DE6-FG0123456789101112131,

我对SQL很陌生,所以我不知道这里有什么问题,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

A large part of your problem is that your update statement will update all the records with each value in each iteration of your loop, and so all the records will have the highest (last) value of @tmp_Code.

It is unclear what the value of "tmp_Code" in your table is before this operation begins, so I've made the assumptions (a) that it might be populated and (b) that the column is nullable.

This solution retains your loop, but in order to update only one record at a time, it first sets all your tmp_code values to null. Ahead of the loop, I set rowcount 1 so that the loop only processes a single row at a time. Then we set rouwcount 0 again after the loop so that we can select back all the results.

Finally, I don't think the construction of your @tmp_code value is doing what you expect. I've moved the concatenation into the Set statement.

-- Set up a table variable for the example.
declare @tbl_code table (TL_No varchar(30), tmp_Code varchar(30))
-- populate with some test data
insert into @tbl_code values ('233246579836', 'a')
insert into @tbl_code values ('233246579836', 'b')
insert into @tbl_code values ('233246579836', 'c')
insert into @tbl_code values ('233246579836', 'd')
insert into @tbl_code values ('233246579837', 'e')

declare @id int, @tmp_Code varchar(30), @TotalCount int
    set @id=0
    set @tmp_Code='ABC3-DE6-FG'
    set @TotalCount=(select COUNT(*) from @tbl_Code where TL_No='233246579836')

-- so that we can select an unprocessed row in the loop
update @tbl_code 
set tmp_Code = null
where TL_No='233246579836' 

-- restrict actions to one row at a time
set rowcount 1

while (@id<@TotalCount)
begin
    update  @tbl_Code
    set [tmp_Code] = @tmp_Code+ CONVERT(varchar(20),@id )
    where TL_No='233246579836' 
    -- this condition is used to get a row that has not yet been processed
    and tmp_code is null 
    set @id=@id+1
end

-- process all rows from here on out
set rowcount 0 

-- get the results
select * from @tbl_code

Results:

TL_No                          tmp_Code
------------------------------ ------------------------------
233246579836                   ABC3-DE6-FG0
233246579836                   ABC3-DE6-FG1
233246579836                   ABC3-DE6-FG2
233246579836                   ABC3-DE6-FG3
233246579837                   e
相关问题