I have some local temporary tables that I can't drop them, would you know if there is any way to drop them?
Example:
It's like they don't exist for me
Those are the internal names given to table variables or currently idle cached temporary tables for stored procedures.
You can't drop them with DROP TABLE.
For table variables/temp tables that are cached as part of an execution plan context then evicting the plan from the cache will also drop the table but you shouldn't do this.
Related
Is there a system view in SAP HANA that contains last modification time for a table?
The time should contain timestamp when table DDL was changed, so I am interested particularly in table metadata last modification time.
For example I'd like to know when a column was added to the table or when a column was removed from the table.
The question isn't about SYS.M_TABLE_STATISTICS that contains information about last DML statement for the table.
You can use new system view M_EXECUTED_STATEMENTS. It monitors all DDL changes to the tables including:
All SQL statements starting with CREATE, DROP, ALTER, and RENAME. For example, CREATE TABLE, CREATE USER and ALTER TABLE.
All SQL statements starting with TRUNCATE, GRANT, REVOKE, LOAD, EXPORT, IMPORT, and COMMENT.
The SET SYSTEM LICENSE and UNSET SYSTEM LICENSE ALL statements.
It is enabled by default globally and was added in HANA SPS 11 exactly for that aim, i.e. for tracking invalid metadata issues.
Suppose a temp table would be created and dropped during a stored procedure. What if multiple users run the stored procedure at the same time, does it cause any problem like unable to create that temp table because it's been already created by another user and hasn't been dropped yet in time before someone tries to create that temp table?
As long as you're talking about an actual #temp table (with a single #) there are no problems. A #temp table is scoped to the session. So you can have dozens of people on the same instance, all creating temp tables with the exact same name, at the exact same time and you'll never have a collision. (It's a different story if you're talking about ##globaltemp tables (with 2 ##)... Global temps can and do collide with other sessions)
My company has an application with a bunch of database tables that used to use a sequence table to determine the next value to use. Recently, we switched this to using an identity property. The problem is that in order to upgrade a client to the latest version of the software, we have to change about 150 tables to identity. To do this manually, you can right click on a table, choose design, change (Is Identity) to "Yes" and then save the table. From what I understand, in the background, SQL Server exports this to a temporary table, drops the table and then copies everything back into the new table. Clients may have their own unique indexes and possibly other things specific to the client, so making a generic script isn't really an option.
It would be really awesome if there was a stored procedure for scripting this task rather than doing it in the GUI (which takes FOREVER). We made a macro that can go through and do this, but even then, it takes a long time to run and is error prone. Something like: exec sp_change_to_identity 'table_name', 'column name'
Does something like this exist? If not, how would you handle this situation?
Update: This is SQL Server 2008 R2.
This is what SSMS seems to do:
Obtain and Drop all the foreign keys pointing to the original table.
Obtain the Indexes, Triggers, Foreign Keys and Statistics of the original table.
Create a temp_table with the same schema as the original table, with the Identity field.
Insert into temp_table all the rows from the original table (Identity_Insert On).
Drop the original table (this will drop its indexes, triggers, foreign keys and statistics)
Rename temp_table to the original table name
Recreate the foreign keys obtained in (1)
Recreate the objects obtained in (2)
In my Application, I need to create few temporary tables. Do I need to execute the SQL in each session to create temp tables else can I run the SQL manually once, and can use those temporary tables in each session?
From the fine manual:
Temporary Tables
Although the syntax of CREATE TEMPORARY TABLE resembles that of the SQL standard, the effect is not the same. In the standard, temporary tables are defined just once and automatically exist (starting with empty contents) in every session that needs them. PostgreSQL instead requires each session to issue its own CREATE TEMPORARY TABLE command for each temporary table to be used. This allows different sessions to use the same temporary table name for different purposes, whereas the standard's approach constrains all instances of a given temporary table name to have the same table structure.
So temporary tables are local to each session and each session will need its own CREATE TEMPORARY TABLE and the temporary tables will be different in each session even if they have the same name.
The sp_addarticle stored procedure, used to create publication articles in SQL Server speak, has a parameter #pre_creation_cmd through which dropping the table or truncating data at the target server can be specified for a snapshot. What circumstances are appropriate for each?
#pre_creation_cmd accepts one of four values:
NONE
DELETE
DROP
TRUNCATE
DELETE TABLE
Assume that your Published article
data is filtered and that the
corresponding table in the
Subscription receives data from other
sources, perhaps another Publication
for example. Using the DELETE
operation for this parameter would
delete "only" the data the meets the
criteria of the filter definition,
leaving the remaining data at the
Subscriber intact.
TRUNCATE TABLE
Removes all rows from a table without
logging the individual row deletions.
TRUNCATE TABLE is similar to the
DELETE statement with no WHERE clause;
however, TRUNCATE TABLE is faster and
uses fewer system and transaction log
resources.
DROP TABLE
Removes one or more table definitions
and all data, indexes, triggers,
constraints, and permission
specifications for those tables.