Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a retrieved some data from a table and inserted them into a temporary table lets say tblTemp.
The output of this table is:
siteid p_id desc code
11 1 test 1 22
11 1 test 2 22
11 1 test 3 22
11 1 test 4 22

I am trying to accomplish increment the siteid of that temporary table and insert to a table. How can I do this?
The final outcome should be like:
siteid p_id desc code
11 1 test 1 22
12 1 test 2 22
13 1 test 3 22
14 1 test 4 22

I was thinking of a looping through the result set and insert them. But is there a better way? Can someone lead me how can i solve it? Thanks

I have tried:
SQL
DECLARE @curcount INT
SET @curcount = 0 
WHILE(@curcount<select count(code) from tblTemp)
BEGIN 
INSERT INTO tblACC (siteid, p_id, desc, code)
SET @curcount = @curcount + 1
END


Also tried:
SQL
DECLARE CUR CURSOR FAST_FORWARD READ_ONLY FOR
select * from tblTemp
DECLARE @varsiteId int
DECLARE @varpId int
DECLARE @vardesc varchar(100)
DECLARE @varcode int

OPEN CUR
FETCH NEXT FROM CUR INTO @varsiteId, @varpId, @vardesc, @varcode
WHILE @@FETCH_STATUS = 0
BEGIN 
UPDATE tblTemp
SET siteid = select max(siteid) + 1 from tblTemp where siteid = @varsiteId
FETCH NEXT FROM CUR INTO @varsiteId, @varpId, @vardesc, @varcode
END
CLOSE CUR
DEALLOCATE CUR 

I failed to increment the site id.
Posted
Updated 22-Mar-13 6:01am
v3
Comments
joshrduncan2012 22-Mar-13 11:31am    
Where is your code with what you have attempted so far?
wonder-FOOL 22-Mar-13 12:01pm    
please see my updates.
Maciej Los 22-Mar-13 12:02pm    
How do you retrieve data from database into tblTemp?
phil.o 22-Mar-13 12:34pm    
There is someting that annoys me : you are getting the siteid value from another table, right?
So, if you change it in your temporary table, how will you know to which original line it is refering?
But maybe you simply do not need to know that... Was just curious ^^

Have a look at this example:
SQL
CREATE TABLE #tblACC (siteid INT, p_id INT, descr VARCHAR(30), code INT)

DECLARE @curcount INT
DECLARE @code INT

SET @curcount = 0
SET @code = 10

WHILE(@curcount<@code)
BEGIN
    INSERT INTO #tblACC (siteid, p_id, descr, code)
    VALUES(11, 1, 'test' + CONVERT(VARCHAR(30), @curcount), 22)
    SET @curcount = @curcount + 1
END

--SELECT *
--FROM #tblAcc

SET @curcount = 0

SELECT @code =COUNT(code) from #tblACC
WHILE(@curcount<@code)
BEGIN
    SET @curcount = @curcount + 1
    UPDATE #tblACC SET siteid = CONVERT(INT, 10 + @curcount) WHERE descr = 'test' + CONVERT(VARCHAR(10), @curcount)
END

SELECT *
FROM #tblAcc

DROP TABLE #tblACC
 
Share this answer
 
Maybe something like adding another column to the temp table
Not tested.
DECLARE @mid INT
SELECT @mid = max(siteid) FROM tblTEMP
ALTER TABLE tblTEMP
ADD inc INT (@mid, 1) NOT NULL;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900