February 21, 2007 at 12:49 am
Hi All,
I Have a stored procedure in sql server 2000.
At the start I drop some temp tables, however if the tables do not exist an exception is thrown. If they dont exist I dont care. How can I turn off exception handling at the start of my drop table section and then turn in back on again after?
February 21, 2007 at 3:34 am
Why not just use if exsists?
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Employee]
Andy.
February 21, 2007 at 3:51 am
Hi Andy,
Yes, I've resorted to using that. It's just that I have a number of tables and I thought it would be easier to just turn error handling off at the beginning and then back on again when I want it.
February 21, 2007 at 6:20 am
why don't you use dynamic tables, that way you won't have to worry about them errors coming up.
e.g. from BOL 2005
USE AdventureWorks;
GO
DECLARE @MyTableVar table(
EmpID int NOT NULL,
OldVacationHours int,
NewVacationHours int,
ModifiedDate datetime);
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25
OUTPUT INSERTED.EmployeeID,
DELETED.VacationHours,
INSERTED.VacationHours,
INSERTED.ModifiedDate
INTO @MyTableVar;
--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
--Note that ModifiedDate reflects the value generated by an
--AFTER UPDATE trigger.
SELECT TOP (10) EmployeeID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO
February 21, 2007 at 8:29 pm
If they are temporary tables, why are you trying to drop them? They should go out of scope whenever the stored proc is completed.
My guess is that you tested the stored proc as a script in QA and found that the tables hang around when you run the script a second time in the same QA window (which is the same scope)
Simply remove the delete statements, or better yet, explicitly remove the tables at the END of your stored proc (much better programming practice).
February 26, 2007 at 9:30 am
Hi William,
I agree with Bledu. I use table variables A LOT. Generally they are every thing you want a temp table to be and more.
There are a couple of things to consider.
Give them a try. I think you will find them very useful.
February 27, 2007 at 12:18 am
Guy's the reason I'm using temp tables in this way and deleting them at the start of the script is that the script runs during an overnight process and then the data in the temp tables are used during the day by various other processes. My own personal feeling is that these shouldn't really be temp tables and that they should be permenent and at the start of the script just use a standard 'Delete from.....' statement.
However the powers that be for whatever reason want the script to run as temp tables using the Drop Table option.
February 27, 2007 at 2:35 am
this is a critical piece of info that you should have highlighted earlier.
i think you should have a table that is not really a temp table but then have an additional field that shows something like a unique run identifier. when you are done using the data you then just delete from the table based on this identifier instead of dropping the table. [you might have to create an additional table to keep track of your run number]
I think it would help if you can post your script excluding any confidential info.
March 2, 2007 at 11:07 pm
The other processes are using the same connection? Or are you talking about global temporary tables?
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply