keyongtech


  keyongtech > sqlserver.* > sqlserver.programming > 06/2007

 #1  
06-28-07, 02:51 PM
vovan
I'm trying to use a table type variable in my stored procedure:

DECLARE @Temp3 table
(
RecordID int IDENTITY (1, 1) Primary key NOT NULL ,
FAccountID int, [Name] varchar(100), FParentAccountID int, Sublevel int,
FAccountNumber varchar(100),
FAccountType varchar(100), TotalBalance money
)

I would like to populate this table with some records and use them for some
manipulations. No problem so far. But I need to use the table variable in
loop. I would like to truncate the table and repopulate it again with a new
data.

WHILE @RowNumber <= @MaxRows
--Here is some manipulation with data from another @Temp2 variable of
table type

Then after I'm done with the current row in @Temp2 I'm trying to use
TRUNCATE TABLE @Temp3

and I get an error Incorrect syntax near '@Temp3'

I think TRUNCATE TABLE doesn't work with table type variables.
If so then how do I delete all rows from @Table3 and reset its Identity? Or
another approach - how do I deallocate @Table3 and declare and populate it
again as a fresh variable?

Thank you
vovan
 #2  
06-28-07, 03:31 PM
SQL Menace
On Jun 28, 9:51 am, "vovan" <some> wrote:
[..]
>
> and I get an error Incorrect syntax near '@Temp3'
>
> I think TRUNCATE TABLE doesn't work with table type variables.
> If so then how do I delete all rows from @Table3 and reset its Identity? Or
> another approach - how do I deallocate @Table3 and declare and populate it
> again as a fresh variable?
>
> Thank you
> vovan




use a #temp table instead that way you can use TRUNCATE and also DBCC
CHECKIDENT

Denis The SQL Menace
[url down]
http://sqlblog.com/blogs/denis_gobo/default.aspx
 #3  
06-28-07, 03:39 PM
vovan
Thank you Denis.
I used to use temporary tables in my previous development. With my current
task I tried to use Cursor because I need to loop through records in the
selected set of records to make some manipulations. Looking for some samples
for using cursors I found some articles suggesting to use table type
variables instead of cursors and temporary tables because both of them are
much slower.

vovan

"SQL Menace" <denis.gobo> wrote in message
news:8550
[..]
Similar Threads
Grant permission to table for truncate table

hi, we have object-level user security. That user need permission for truncate one table. I can't set the user as sysadmin or DBO. any way that I could grant the user to have...

Truncate table

Hi I want to truncate all data in database ... pls help me how i ll truncate?

truncate table using dao

Access 97, SQL2k How can I truncate a linked sql table using Dao? I've tried several different ways, including the following, and continually get errors. Dim db As...

truncate table

Can you explicity grant access to truncate a specific table to a login id without giving dbo permissions on a database. I have a job that first truncates a table and then...

Truncate Table vs. other?

Hey all, I am trying to research information on the best(?) method(s) for a nightly data repopulate. This situation is this: We have a packaged/purchased system one of...


All times are GMT. The time now is 10:01 PM. | Privacy Policy