SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 drop table with variable name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

spinningtop
Starting Member

United Kingdom
29 Posts

Posted - 05/08/2012 :  06:32:14  Show Profile  Reply with Quote


Hi
If I wanted to check if a tables existed before dropping it so it doesn't kick up a table does not exist error in the program I would use the code below. However my problem is that the schema name is not [dbo] but has a variable name created by the user at runtime. I have been trying to change the code below to dynamic sql so I can add the variable $schemaname instead of [dbo] below but I can't get it to work.

Any help would be appreciated

thanks




USE [database]
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[table]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROP TABLE [dbo].[table]

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 05/08/2012 :  06:43:55  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
you could do something like this:

USE [Tempdb]
GO

CREATE SCHEMA foo
GO

CREATE TABLE foo.bar ([woo] BIT)
INSERT foo.bar VALUES (1)
GO

IF OBJECT_ID('foo.bar') IS NOT NULL
	SELECT 'Foo.Bar Exists'
ELSE
	SELECT 'Foo.Bar DOES NOT Exist'
GO

DECLARE @schemaName SYSNAME = 'foo'
DECLARE @tableName SYSNAME = 'bar'

DECLARE @QualifiedName SYSNAME = QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName)
DECLARE @sql NVARCHAR(MAX)

IF OBJECT_ID(@QualifiedName) IS NOT NULL
BEGIN
	SET @sql = 'DROP TABLE ' + @QualifiedName
	PRINT @sql
	EXEC sp_executeSql @sql
END


IF OBJECT_ID('foo.bar') IS NOT NULL
	SELECT 'Foo.Bar Exists'
ELSE
	SELECT 'Foo.Bar DOES NOT Exist'
GO

-- Cleanup
DROP SCHEMA foo
GO


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3442 Posts

Posted - 05/08/2012 :  06:45:04  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
Note I wrapped all input using QUOTENAME()

This should give good protection from sql injection. However it won't stop anyone from dropping the wrong table if they get the parameter names wrong.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

spinningtop
Starting Member

United Kingdom
29 Posts

Posted - 05/08/2012 :  11:49:07  Show Profile  Reply with Quote

Thanks very much.
This works very well
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000