TechGuy TechGuy - 1 month ago 12
SQL Question

Pass Wrong Information in stored procedure

I wrote some stored procedure to transfer 2 tables data to a new, merged table.


  • Table 1 -> OldUsers table

  • Table 2 -> Hunters Table

  • Table 3 -> Users table



Table structures:

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)
)


I wrote some stored procedure to import data. BUT when I normally execute then pass ALL NULLS (whole table filled with NULLS). After that I remove

Where @uid = @index;


and then execute. Then I saw one id repeated everywhere.

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

Answer

Could you please try this two inserts?

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

Instead of selecting and inserting data record by record you can do it at once. Please check this link to Sql Server documentation.