Sybase Dropping Temporary Table
-
27-09-2019 - |
Question
Does anybody face an issue when you drop a temporary table at the Sybase ASE 12 it still persists at a current session. So you encounter "Table already exists" when trying to select data into it again
Solution
Well, you need to read the manuals, at least the syntax for the commands you expect to use, before you write code. Otherwise you will face issues at every turn. It depends on what you are trying to do.
SELECT ... INTO #MyTable
creates a table and succeeds because it does not exist. So a secondSELECT ... INTO #MyTable
will try to create#MyTable
, find that it exists, and fail.If you want to perform a second
SELECT
into the same table,TRUNCATE
the table, then useSELECT ... INTO EXISTING TABLE #MyTable
.Or
DROP TABLE
and skip theEXISTING TABLE
modifier.If you want the table to contain the sum of several
SELECTS
, obviously, skip theTRUNCATE
.
OTHER TIPS
I normally do this:
1) CREATE TABLE #temptable ( ....
)
INSERT INTO #temptable SELECT .....
This will never give error.
This solves another possible error . If the WHERE clause accompanying the "select INTO " yields no rows, the temporary table will not have zero rows but the temporary table won't be created at all. This could make the stored proc blow up later.