Unix Technical Forum

Delete SQL Table using a variable that refers to the Table Name

This is a discussion on Delete SQL Table using a variable that refers to the Table Name within the SQL Server forums, part of the Microsoft SQL Server category; --> SQLLY challenged be gentle -- Trying to create code that will drop a table using a variable as the ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:36 AM
LVande
 
Posts: n/a
Default Delete SQL Table using a variable that refers to the Table Name

SQLLY challenged be gentle --

Trying to create code that will drop a table using a variable as the
Table Name.

DECLARE @testname as char(50)
SELECT @testname = 'CO_Line_of_Business_' +
SUBSTRING(CAST(CD_LAST_EOM_DATE
AS varchar), 5, 2) + '_' + LEFT(CAST(CD_LAST_EOM_DATE AS varchar),
4)+ '_' + 'EOM'
FROM TableName

Print @testname = 'blah...blah...blah' (which is the actual table
name on the server)

How can I use this variable (@testname) to drop the table? Under
severe time constraints so any help would be greatly appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:36 AM
David Portas
 
Posts: n/a
Default Re: Delete SQL Table using a variable that refers to the Table Name

SET @testname = 'DROP TABLE ['+@testname+']'
EXEC (@testname)

From your naming convention it looks like you have some tables that contain
sets of data based on dates. If so then I suggest you revise your design.
Why not just leave the data in one table with a date column and avoid this
kind of messy dynamic SQL manipulation?

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:36 AM
BUSHII
 
Posts: n/a
Default Re: Delete SQL Table using a variable that refers to the Table Name

DECLARE @TestName VarChar(50)
DECLARE @vQString VarChar(8000)

SET @TestName = 'MyTmpTable'
SET @vQString =
'IF object_id('''+@TestName+''') IS NOT NULL '+ /* if table @TestName
exist -> then drop this table */
'DROP TABLE '+@TestName

Exec (@vQString)


ps.
you shoulnd use Char(50). In this cas better is VarChar(50).

Best Regards
Piotr Leib


Uzytkownik "LVande" <lvandehaar@fhlbdm.com> napisal w wiadomosci
news:6b4d0472.0407141010.85ec0bf@posting.google.co m...
> SQLLY challenged be gentle --
>
> Trying to create code that will drop a table using a variable as the
> Table Name.
>
> DECLARE @testname as char(50)
> SELECT @testname = 'CO_Line_of_Business_' +
> SUBSTRING(CAST(CD_LAST_EOM_DATE
> AS varchar), 5, 2) + '_' + LEFT(CAST(CD_LAST_EOM_DATE AS varchar),
> 4)+ '_' + 'EOM'
> FROM TableName
>
> Print @testname = 'blah...blah...blah' (which is the actual table
> name on the server)
>
> How can I use this variable (@testname) to drop the table? Under
> severe time constraints so any help would be greatly appreciated.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:11 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com