我想創建唯一的ID。LOOP將數據插入到具有唯一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_Employee從tbl_Store
我這樣的代碼
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
而你的問題是? – Dukeling 2013-02-14 11:15:05
如何讓我的代碼穩定? – 2013-02-14 11:18:53
這不是問題。至少它與問題題目無關。 – mvp 2013-02-14 11:19:59