How can i remove temp table after creating it - sql

Hi How can i remove temp table after creating it
because i get this error There is already an object named '#tmp_statement' in the database.
SELECT * FROM #tmp_statement
DROP TABLE #tmp_statement

You are getting error on creating it, not when dropping it. Try putting this before creating table:
IF object_id('tempdb..#tmp_statement') IS NOT NULL
DROP TABLE #tmp_statement
Or just put it before any other statements you have.

Related

How can I view the definition of a temporary table?

I make a temporary table in SQL Server:
create table #stun (name varchar(40),id int,gender varchar(40))
How can I view its definition afterwards?
you can check this way-
SELECT *
INTO #TempTable
FROM table_name -- any table from database
EXEC tempdb..sp_help '#TempTable'
DROP TABLE #TempTable
Solution 1 :
You can query data against it within the current session :
SELECT
*
FROM
#yourtemporarytable;
Sometimes, you may want to create a temporary table that is accessible across connections. In this case, you can use global temporary tables.
Unlike a temporary table, the name of a global temporary table starts with a double hash symbol (##).
CREATE TABLE ##global_temp (
...
);
SELECT
*
FROM
##global_temp
Solution 2 :
Using SSMS, you can find the table in the left pane >> Design >> get the table structure.

User defined type dropped while in use

I have an Oracle database with a table as follows:
create table Table1
(Column1 number(5,0),
Column2 special_type);
Now, due to some data errors, the support team decided the fix would be to drop and recreate the Type.
I now have a table as follows:
Table1
Column1 number(5,0),
Column2 null
The problem is, I cannot drop the table, I get a "Table has errors" message. I cannot alter the table, I get a "Table has errors" message. I have tried to manipulate the DDL in Oracle SQL Developer, guess what I get? A "Table has errors" message.
Can anyone point me in the right direction?
It seems support team have dropped the type forcefully. In normal scenario, you cannot drop a type if it has any dependent table.
See demo:
SQL> CREATE OR REPLACE TYPE NUU AS TABLE OF NUMBER;
/
Type created.
SQL> CREATE TABLE TBLLL(NUM NUU)
NESTED TABLE NUM STORE AS VVVVV ;
/
Table created.
Now when i try to do a simple drop type, i get the below error:
SQL> drop type nuu;
drop type nuu
*
ERROR at line 1:
ORA-02303: cannot drop or replace a type with type or table dependents
So i drop it forcefully:
SQL> drop type nuu force;
Type dropped.
And when i try to make a select i get the error:
SQL> select * from tblll;
select * from tblll
*
ERROR at line 1:
ORA-04063: table "USER.TBLLL" has errors
So in order to Alter the table you first need to create the type back. Once the type is created back, your table definition becomes correct and then you can Alter your table.
Other solution is to drop the table and recreate it which you already mentioned is not working.
You can try the following -
re-create the type
create another table with same table structure
insert the data into the second table
use the second table now.
You may want to not recreate the type since it caused issues earlier. Find an alternative. It might solve your issue.

drop table #temp vs drop myTable if it's not null

So far i was using
IF OBJECT_ID(''tempdb..#tempTable'') IS NOT NULL -- dropping the temp table
DROP TABLE #tempTable
Is there a way in which I could use the same statement for a table which is not a temp one?
Tried like this and it didn't work:
IF OBJECT_ID(''myOwnDb.dbo.myTable'') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Strings in MS SQL server should be enclosed in single quotes.
So neither OBJECT_ID(''myOwnDb.dbo.myTable'') nor OBJECT_ID("myOwnDb.dbo.myTable") will work.
But OBJECT_ID('myOwnDb.dbo.myTable') will work perfectly.
In addition to what other users have suggested wrt Object_ID which is fine, you can explore below method to detect if table exist or not using INFORMATION_SCHEMA
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'Your Table Name')
BEGIN
Drop table <tablename>
END
The reason it did not work is because you have the extra quotes instead of single quotes.
i.e. You should be doing this:
IF OBJECT_ID('myOwnDb.dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
However, note that when you actually drop the table. You aren't even referencing the database. So you can just do:
IF OBJECT_ID('dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Unless you are calling this command from another database. Then you will need to include the database name in the DROP TABLE command as well.

Deleting the Global Temp table and then creating the Global temp table with same name is giving ambiguity error

Below is the SQL statement for Deleting the global temp table and creating it again.
if object_ID('tempdb..##TempTable123') is not null
begin drop table tempdb..##TempTable123 end
GO
Select * into ##TempTable123 from ##TempTable1
After executing the Select statement, I am getting the following Error
"The reference to temp table name '##TempTable123' is ambiguous and cannot be resolved. Use either '##TempTable123' or '##TempTable123'."
Try to add tempdb.. to the select:
Select * into tempdb..##TempTable123 from tempdb..##TempTable1

Creating a new table using a synonym in SQL Server

I have a synonym created for this table ParaPITD.dbo.BM_00_PolicyMaster that points to ParaPITDYYYYMM.dbo.BM_00_PolicyMaster
There is no table yet - so it acts as a place holder.
I have a stored procedure that creates a temp table #BM_00_PolicyMaster and now I want to insert or select into the place holder where the synonym points to and create the table in ParaPITDYYYYMM.dbo.BM_00_PolicyMaster
If I run this:
select *
into ParaPITD.dbo.BM_00_PolicyMaster
from #BM_00_PolicyMaster
it creates the table in ParaPITD.dbo.BM_00_PolicyMaster and ignores the synonym
If I run this:
INSERT INTO ParaPITD.dbo.BM_00_PolicyMaster
SELECT *
FROM #BM_00_PolicyMaster
it gives me an invalid object error acting like the table must exist before it will insert.
Appreciate any help - thanks
From the doc (http://technet.microsoft.com/en-us/library/ms187552.aspx):
You cannot reference a synonym in a DDL statement
And any statement that creates a table, including SELECT INTO counts as a DDL statement. (See here: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1085a84c-faac-4d26-8f35-c64d8b901034/insert-into-is-sentence-dml-or-ddl)