on 2010 Mar 12 6:13 PM
SA 11.0.1.2376
When I create a local temporary table by means of a SELECT INTO LOCAL TEMPORARY TABLE, I am not able to alter that table since ALTER TABLE returns a "table not found" error. Is this by design? If so, why?
(Business case: I often "copy" remote data into temp tables and prefer to omit the declaration of all columns and types – call it lazyness. However, I usually have to define PKs and the like afterwards.)
Example:
select * into local temporary table LT
from systable
select * from LT
-- works
alter table LT add test_col int null
-- returns "table LT not found"
Request clarification before answering.
[Note: Edited answer to reflect Breck's and Mark's responses:]
As Breck has explained, ALTER TABLE is not only forbidden for tables created by SELECT INTO TEMPORARY TABLE but for all those kinds of local temporary tables, i.e. it applies also to
So there seem to be two workarounds/solutions:
If one relies on having an "auto-generated table" from a result set (ignoring performance drawbacks), one has to use a permanent table instead of a local temporary table, and drop that table afterwards. (That's what I have done in the past but forgot about lateron:(
select * into PT from systable
select * from PT -- works
alter table PT add test_col int null -- works
If performance does matter and one has to use a local temporary table for that reason, one should create the table explicitly with means of CREATE/DECLARE LOCAL TEMPORARY TABLE and then use INSERT INTO ... SELECT instead.
In that respect, SQL Central is a convenient tool to copy the table schema.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Note that there are drastic performance issues with creating/dropping permanent tables... DDL forces stored procedures to be dropped from in-memory cache and must be reloaded/parsed... cached query plans will also be dropped. This is necessary since adding/dropping tables can change how table references are bound.
You can't use "ALTER TABLE" on a temporary table, but it's very well possible to do a "CREATE INDEX". I used that a few times already. 1. CREATE or DECLARE the temporary table 2. fill the table 3. create the indexes
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Interesting point - I wasn't aware of that. As one might expect, such an index isnt' shown in the system catalog, either, and can't be dropped with DROP INDEX but is dropped automatically when the table is dropped.
ooooo... so why not? Why not implement ALTER TABLE and DROP INDEX and all those other things? So what if it's not in the catalog?
Alas, the decision to not store information about local temporary tables in the system catalog was made a long time ago in a galaxy far, far away.
One side effect is this sentence in the Help: "You cannot use ALTER TABLE on a local temporary table."
Was it a good decision? If it contributed to the great speed of temp tables in SQL Anywhere, then yes, I think so.
Does that decision cause great pain? Oh, yes... from time to time... but there has not been much hue and cry over the years. Not nearly as much anguish as with other products; e.g., endless debates and discussions of the tempdb database in [this other product] and [that one].
Would it be difficult to change? Oh, gosh, I do think it would fall into the category of "changing basic architecture"... I recently had to do one of those kinds of changes, in Foxhound, not something you consider lightly 🙂
But... you're just asking for ALTER TABLE to work on local temporary tables. In theory, you could implement that without storing information in the catalog. In theory.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you know ahead of time that you need an extra column, why not add that column to the SELECT list of the query that created the temporary table? You can provide a value, cast it to the required data type, and provide an alias for the column name.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry for the confusion with the added column. The original requirement was to add PRIMARY and/or UNIQUE KEYs or indexes. I have chosen the "add column" example just because I thought it was more comprehensible. For additional columns, I generally do as you suggest, so that's no problem with local temporary tables:)
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.