March 28, 2017 at 8:08 am
Hi Experts ,
i want to create a function for drop temp table. but below function got error while execute. please suggest me solution.
-- Temp table :
create table ##Anand(id int)
select * from ##Anand
-- Function
Alter function optimise_table_fn(@tab varchar(100))
returns varchar(100)
as
begin
declare @check_tab varchar(100) = 'tempdb..'+@tab
if OBJECT_ID(@check_tab) is not null
begin
declare @drop varchar(200) = ('drop table ' + @tab)
EXECUTE sp_executesql @drop
end
return ( @tab + ' dropped_successfully');
end
-- Execute function :
select dbo.optimise_table_fn('##Anand')
March 28, 2017 at 8:30 am
You would need to use to stored procedure to do that not a function.
March 28, 2017 at 8:37 am
Anandkumar-SQL_Developer - Tuesday, March 28, 2017 8:08 AMHi Experts ,
i want to create a function for drop temp table. but below function got error while execute. please suggest me solution.-- Temp table :
create table ##Anand(id int)
select * from ##Anand-- Function
Alter function optimise_table_fn(@tab varchar(100))
returns varchar(100)
as
begin
declare @check_tab varchar(100) = 'tempdb..'+@tab
if OBJECT_ID(@check_tab) is not null
begin
declare @drop varchar(200) = ('drop table ' + @tab)
EXECUTE sp_executesql @drop
end
return ( @tab + ' dropped_successfully');
end-- Execute function :
select dbo.optimise_table_fn('##Anand')
What complexity might you hide by encapsulating your drop statement?
IF OBJECT_ID('TempDB..#MyLocalTempTable') IS NOT NULL DROP TABLE #MyLocalTempTable;
IF OBJECT_ID('TempDB..##MyGlobalTempTable') IS NOT NULL DROP TABLE ##MyGlobalTempTable;
Why are you using a global temp table?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 28, 2017 at 9:16 am
I see no benefit to creating a function to drop a table.
Creating the function just adds a layer of problems.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 28, 2017 at 11:58 am
ChrisM@Work - Tuesday, March 28, 2017 8:37 AMAnandkumar-SQL_Developer - Tuesday, March 28, 2017 8:08 AMHi Experts ,
i want to create a function for drop temp table. but below function got error while execute. please suggest me solution.-- Temp table :
create table ##Anand(id int)
select * from ##Anand-- Function
Alter function optimise_table_fn(@tab varchar(100))
returns varchar(100)
as
begin
declare @check_tab varchar(100) = 'tempdb..'+@tab
if OBJECT_ID(@check_tab) is not null
begin
declare @drop varchar(200) = ('drop table ' + @tab)
EXECUTE sp_executesql @drop
end
return ( @tab + ' dropped_successfully');
end-- Execute function :
select dbo.optimise_table_fn('##Anand')What complexity might you hide by encapsulating your drop statement?
IF OBJECT_ID('TempDB..#MyLocalTempTable') IS NOT NULL DROP TABLE #MyLocalTempTable;
IF OBJECT_ID('TempDB..##MyGlobalTempTable') IS NOT NULL DROP TABLE ##MyGlobalTempTable;
Why are you using a global temp table?
Just to be safe - you should pass in the second parameter to insure you are looking for tables:
IF OBJECT_ID('TempDB..#MyLocalTempTable', 'U') IS NOT NULL DROP TABLE #MyLocalTempTable;
IF OBJECT_ID('TempDB..##MyGlobalTempTable', 'U') IS NOT NULL DROP TABLE ##MyGlobalTempTable;
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply