我有一张带有虚假客户信息的平面表(姓名,地址,性别......)。我在名为customer_id的平面表中添加了一列,其中所有值都为null。现在我试图为每个客户添加不同的ID,但我不确定如何实现这一目标。这是我到目前为止所做的,因为我希望customer_id成为主键,所以我没有该表的主键
ALTER PROCEDURE [dbo].[add_id]
AS
BEGIN
declare @customer_id int
declare cursor1 cursor
for
select
customer_id
from
flat_customer
open cursor1
fetch next from cursor1 into @customer_id
while(@@FETCH_STATUS = 0)
begin
if(@customer_id is null)
begin
exec get_next_id_by_name 'Customer', @customer_id output
end
fetch next from cursor1 into @customer_id
end
END
我仍然是sql的开头,所以我不确定如何逐行解析
答案 0 :(得分:1)
这样做:
DECLARE @i int = 1;
UPDATE flat_customer
SET customer_id = @i,
@i=@i+1;