我编写了一些存储过程来将2个表数据传输到新的合并表。
表格结构:
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)
)
我写了一些存储过程来导入数据。但是当我正常执行时,然后传递所有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
答案 0 :(得分:1)
避免使用WHILE,它会影响性能,就像使用CURSOR一样。在这种情况下,我建议使用INSERT INTO ... SELECT,check this link from Pinal Dave获取有关如何使用INSERT INTO ... SELECT子句替换游标的详细说明。
查看Microsoft MSDN website了解详情
答案 1 :(得分:0)
将旧用户的Where @uid=@index;
更改为OldUsers.uid
和
猎人 Where @uid = @index;
到Hunters.id
答案 2 :(得分:0)
请你试试这两个插页吗?
INSERT INTO Users
Select username, [password], [state], name, license, lansno, curcuit_no, communism, [Uid], 0 ,'','','','','','','','','',''
FROM OldUsers
INSERT INTO Users
SELECT '', '', '', Hunter, licence, '', '', town, ID, 1, '', country, circuit, licence_territory, forest, association, hunt_ar, area, [contract], radio_frequency
From Hunters
您可以立即执行此操作,而不是按记录选择和插入数据。请检查this link to Sql Server documentation。
答案 3 :(得分:0)
我可以建议采用不同的方法吗?
尝试运行:
INSERT INTO Users
select username, password,state, name, license, lansno, curcuit_no, communism, uid,0,'','','','','','','','','',''
from OldUsers
您可能需要在插入
后添加列的名称