1

我编写了一些存储过程来将 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  
4

4 回答 4

1

避免使用 WHILE,它会严重影响性能,就像使用 CURSOR。在这种情况下,我建议使用 INSERT INTO...SELECT,查看 Pinal Dave的此链接,了解如何用 INSERT INTO...SELECT 子句替换游标的详细说明。

查看Microsoft MSDN 网站了解更多信息

于 2012-05-23T09:42:41.507 回答
0

我可以建议一种不同的方法吗?

尝试调整这个:

INSERT INTO Users 
select username, password,state, name, license, lansno, curcuit_no, communism, uid,0,'','','','','','','','','',''
from OldUsers

您可能必须在插入后添加列名

于 2012-05-23T10:33:19.903 回答
0

更改Where @uid=@index;OldUsers.uid老用户

Where @uid = @index;Hunters.id猎人

于 2012-05-23T09:52:24.947 回答
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

您可以一次完成,而不是逐条选择和插入数据记录。请检查此链接到 Sql Server 文档

于 2012-05-23T09:57:50.933 回答