LOOP将数据插入具有唯一ID的表

时间:2013-02-14 11:10:00

标签: sql sql-server-2008 tsql

我想创建唯一ID。

案例是有表事务,但没有主表

像这样的例子

TBL_Store

StoreID  StoreName EmployeeName EmployeePosition CityCode  
S001     Alfa Mart Denny        HeadStore        C001
S002     Wart Mart Willy        Manager          C002
S003     Hypermart Ahmad        Manager          C002

所以我想从tbl_Store

创建tbl_Employee

我的代码就像这样

 declare @a int, @jml as int 
    set @a = 1 
    CREATE TABLE #Tempabc (
         EmployeeName varchar(100), EmployeeCPosition varchar(100), CityCode varchar(10) 
        )
    insert into #Tempabc 
    select   distinct a.EmployeeName ,a.EmployeePosition ,a.CityCode    from tbl_Store a where  a.EmployeeName is not null 

        select @jml = count( * )   from    #Tempabc ac
    while (@a <= @jml )  
    begin
        --insert into tbl_Employee  (EmployeeName , EmployeePosition ,CityCode,CreatedBy,LastEditBy,LastEditBy, LastEditDate ,EmployeeCode )
        select distinct a.EmployeeName ,a.EmployeePosition ,a.CityCode ,'Admin','Admin', CONVERT(VARCHAR(10), 
        GETDATE(),120) AS Createdate,CONVERT(VARCHAR(10),GETDATE(),120) AS LastEditDate , 
        case when @a < 10 then 'SPC00'+ cast (@a as varchar(3))
             when @a < 100 then 'SPC0' + CAST(@a as varchar(3)) 
             else 'SPC' + CAST(@a as varchar(3))    
        end as abv                                                      
         from tbl_Store a where a.EmployeeName is not null 

        set @a = @a +1
    end 

从上面的代码中可以像这样进行循环

EmployeeName EmployeePosition CityCode  EmployeeCode
Denny        HeadStore        C001      SPC001
Willy        Manager          C002      SPC001
Ahmad        Manager          C002      SPC001
Denny        HeadStore        C001      SPC002
Willy        Manager          C002      SPC002
Ahmad        Manager          C002      SPC002
Denny        HeadStore        C001      SPC003
Willy        Manager          C002      SPC003
Ahmad        Manager          C002      SPC003

2 个答案:

答案 0 :(得分:1)

您可以在临时表上设置标识和主键,如:

CREATE TABLE #Tempabc (
     [ID] [int] IDENTITY(1,1) NOT NULL, EmployeeName varchar(100), EmployeeCPosition varchar(100), CityCode varchar(10) 
    )

每次插入时ID都会递增,然后您可以调用#Tempabc上的更新来创建Employee代码

UPDATE #Tempabv SET EmployeeCode = 'SPC' + RIGHT('0000'+CONVERT(VARCHAR(3), ID), 3)

我所说的是先插入并创建所有内容,然后只在最后插入到Employee表中。即使您需要创建另一个临时表。确保清理这些临时表

答案 1 :(得分:0)

感谢CR41G14的回答。

 CREATE TABLE #Tempabc (
         EmployeeCode  int IDENTITY(1,1) NOT NULL, EmployeeName varchar(100), EmployeeCPosition varchar(100), CityCode varchar(10) 
        )

 insert into tbl_Employee  (EmployeeName , EmployeePosition ,CityCode,CreatedBy,LastEditBy,LastEditBy, LastEditDate ,EmployeeCode )
        select distinct a.EmployeeName ,a.EmployeePosition ,a.CityCode ,'Admin','Admin', CONVERT(VARCHAR(10), 
        GETDATE(),120) AS Createdate,CONVERT(VARCHAR(10),GETDATE(),120) AS LastEditDate , 
        case when a.EmployeeCode < 10 then 'SPC00'+ cast (a.EmployeeCode   as varchar(3))
             when a.EmployeeCode < 100 then 'SPC0' + CAST(a.EmployeeCode   as varchar(3)) 
             else 'SPC' + CAST(a.EmployeeCode as varchar(3))    
        end as abv                                                      
         from  #Tempabc a