January 14, 2005 at 2:09 pm
Hi,
I have a temporary table into which I insert bulk data from a stored procedure output. It will be easy for me if the temptable can have a column with sequential numbers 1,2,3,4,. I could not figure out a way to do it. This is the stripped down sql what I have in the stored procedure.
insert into temptable select name, email, date from usertable where condition = xyz.
I tried auto increment column with seed 1, increment 1. But if I delete and insert again it does not start from 1. Is there any way to accompolish this? It will make the front-end code very simple.
Thank you.
January 14, 2005 at 2:23 pm
You can use the TRUNCATE TABLE temptable to delete ALL the rows from the temp table before inserting the new data.
January 14, 2005 at 2:29 pm
Senthil, is this an example of what you are looking for?
use tempdb
create table sp_who (ID INT IDENTITY (1,1) NOT NULL
,spid varchar (10)
,status varchar (20)
,loginname varchar(20)
,hostname varchar(20)
,blk varchar(20)
,dbname varchar(20)
,cmd varchar(20))
GO
use master
INSERT INTO sp_who (spid, status, loginname, hostname, blk, dbname, cmd)
execute master..sp_who
GO
select * from tempdb.dbo.sp_who
January 14, 2005 at 2:32 pm
Hi,
I tried to change it to TRUNCATE TABLE, but it says user does not have permission to perform this operation on table 'temptable'. I checked all the permission check boxes but it is still not working. What am I missing here?
Thanks.
January 14, 2005 at 2:34 pm
From books on line:
"TRUNCATE TABLE permissions default to the table owner and are not transferable."
Ryan
January 14, 2005 at 2:40 pm
Hi,
I changed the owner of the temp table and it works fine. Thanks.
January 14, 2005 at 2:40 pm
Ok, then you can always do something like this :
declare @Results table (MyIdent int identity (1,1), fields...)
Insert into @Results (Select ....)
Select * from @Results
Or you could do some acrobatics to generate to number line by line.
January 14, 2005 at 2:54 pm
Senthil,
As a final note. See Remi's use of the table variable. If you are using this for a one time query, you really will want to do it that way. However, if you are going to use the table after you initial connection (or the completion of a GO batch) you will want to use a temp table.
If you use the temp table, make sure to go back and DROP TABLE TEMP_TABLE
If you can though, use the variable and table data type.
Ryan
January 14, 2005 at 9:15 pm
> If you are using this for a one time query, you really will want to do it that way
... unless the return is very large in which case you'll run out of physical memory and start hitting the swap file. In cases where very large returns are expected, you're better of using a permanent but Truncatable working table.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply