Groups | Blog | Home
all groups > sql server programming > october 2003 >

sql server programming : temporary table and dynamic sql


Don Peterson
10/20/2003 5:12:33 PM
You might want to look into using the DBCC CHECKIDENT function to reseed the
table after it's built. Also, if you will be using the IDENTITY_INSERT for
a table, the Identity column is automatically reseeded to the highest value
that is inserted.


[quoted text, click to view]

Delbert Glass
10/20/2003 5:19:07 PM

Hard code it, but RESEED it.

DECLARE @STARTFROM INTEGER
SET @STARTFROM = 11
--SET @STARTFROM = 22

CREATE TABLE #tmp_descriptions (
desc_id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
language_id int NOT NULL,
description nvarchar(256) NOT NULL
)

DBCC CHECKIDENT ('#tmp_descriptions', RESEED, @STARTFROM)

INSERT INTO #tmp_descriptions
(language_id,description)
VALUES (66,'Old English')

INSERT INTO #tmp_descriptions
(language_id,description)
VALUES (77,'English')

SELECT *
FROM #tmp_descriptions

DROP TABLE #tmp_descriptions

Bye,
Delbert Glass

Adnan Selimovic
10/20/2003 11:50:44 PM
Hi folks!

The SELECT statement reports following error:

Invalid object name '#tmp_descriptions'

Why is that? When I create this table without using the dynamic sql it
works.

Cheers,

Adnan

DECLARE
@SqlString nvarchar(512)


SET @SqlString =
N'CREATE TABLE #tmp_descriptions (
desc_id int IDENTITY NOT NULL PRIMARY KEY,
language_id int NOT NULL,
description nvarchar(256) NOT NULL
)'

EXEC sp_executesql @SqlString

SELECT *
FROM #tmp_descriptions

DROP TABLE #tmp_descriptions


Adnan Selimovic
10/20/2003 11:59:25 PM
The reason why I use dynamic sql is that I want identity seed to be
dynamical:

SET @SqlString =

N'CREATE TABLE #tmp_descriptions (
desc_id int IDENTITY(' + CAST(@desc_id AS nvarchar(10)) + N', 1) NOT NULL
PRIMARY KEY,
language_id int NOT NULL,
description nvarchar(256) NOT NULL

)'


[quoted text, click to view]

Chris Taylor
10/21/2003 1:16:38 AM
Hi,

Local temporary tables are only visible in the session that created them and
as soon as that session ends the table is droped. In your case the call to
sp_executesql scopes the temporary table to the SQL executed by the
sp_executesql.Therefore once the sp_executesql returns the temporary table
is no longer accessable.

Hope this helps

Chris Taylor

[quoted text, click to view]

Adnan Selimovic
10/21/2003 10:56:25 AM
Thanks its ok now

Bye

[quoted text, click to view]

AddThis Social Bookmark Button