我编写了一些存储过程来将 2 个表数据传输到一个新的合并表。
- 表 1 -> OldUsers 表
- 表 2 -> 猎人表
- 表 3 -> 用户表
表结构:
CREATE TABLE [dbo].[Users]
(
[uid] [int] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NULL,
[password] [varchar](50) NULL,
[state] [int] NULL,
[name] [varchar](50) NULL,
[license] [varchar](50) NULL,
[lansno] [varchar](50) NULL,
[curcuit_no] [varchar](50) NULL,
[communism] [varchar](100) NULL,
[Olduid] [int] NULL,
[Is_hunter] [bit] NULL,
[free_text] [text] NULL,
[country] [varchar](50) NULL,
[curcuit] [varchar](50) NULL,
[license_territory] [varchar](50) NULL,
[forest] [varchar](50) NULL,
[association] [varchar](50) NULL,
[hunt_ar] [varchar](50) NULL,
[area] [varchar](50) NULL,
[contract] [varchar](50) NULL,
[radio_frequency] [varchar](50) NULL,
CONSTRAINT [PK_timeuser]
PRIMARY KEY CLUSTERED ([uid] ASC)
)
我写了一些存储过程来导入数据。但是当我正常执行时,然后传递 ALL NULLS(整个表都填充了 NULLS)。之后我删除
Where @uid = @index;
然后执行。然后我看到一个 id 到处重复。
ALTER PROCEDURE [dbo].[UserTransfer]
AS
declare @OldUser_tbl_count int;
declare @Hunters_tbl_count int;
declare @index int;
set @index = 1;
declare @uid int;
declare @usr varchar(50);
declare @psw varchar(50);
declare @stat int;
declare @name varchar(50);
declare @license varchar(50);
declare @lansno varchar(50);
declare @curcuit varchar(50);
declare @commu varchar(100);
declare @sid int;
--declare @text text;
declare @country varchar(50);
declare @curc varchar(50);
declare @terri varchar(50);
declare @forest varchar(50);
declare @assoc varchar(50);
declare @hunt varchar(50);
declare @area varchar(50);
declare @contract varchar(50);
declare @radio varchar(50);
declare @town varchar(100);
declare @lans varchar(50);
--SET @OldUser_tbl_count = (SELECT COUNT (*) from OldUsers)
SELECT @OldUser_tbl_count = COUNT (*) from OldUsers;
SELECT @Hunters_tbl_count = COUNT (*) from Hunters;
-- entering from OldUsers to Users tbl
while(@index <= @OldUser_tbl_count)
BEGIN
Select @uid=OldUsers.uid , @usr=OldUsers.username , @psw=OldUsers.password , @stat=OldUsers.state,
@name = dbo.OldUsers.name, @license=OldUsers.license,@curcuit= OldUsers.curcuit_no,@commu= OldUsers.communism,@lans=OldUsers.lansno
FROM OldUsers
Where @uid=@index;
INSERT INTO Users
Values (@usr,@psw,@stat,@name,@license,@lans,@curcuit,@commu,@uid,0,'','','','','','','','','','');
SET @index = @index+1;
END
-- entering from Hunters to Users tbl
While (@index <= @Hunters_tbl_count)
BEGIN
SELECT @uid=Hunters.id,@license=Hunters.licence,@name=Hunters.hunter,@country=Hunters.country,@curc=Hunters.circuit,
@terri=Hunters.licence_territory,@forest=Hunters.forest,@assoc=Hunters.association,@hunt=Hunters.hunt_ar,@area=Hunters.area,
@contract=Hunters.contract,@radio=Hunters.radio_frequency,@town=Hunters.town
From Hunters
Where @uid = @index;
INSERT INTO Users
VALUES ('','','',@name,@license,'','',@town,@uid,1,'',@country,@curc,@terri,@forest,@assoc,@hunt,@area,@contract,@radio);
SET @index=@index+1;
END
-exec UserTransfer