Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Go Back  Xtreme Visual Basic Talk > > > Can I rename a Table in SQL using a WildCard?


Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2008, 05:34 AM
rosmiq rosmiq is offline
Regular
 
Join Date: Nov 2005
Posts: 71
Default Can I rename a Table in SQL using a WildCard?


Hi,

I'm importing a .csv file into my SQL Server (2005) using SSIS on a schedule, after its imported I would like to change the name of the file. The name of the imported file is always different as its coming from an external source.

Is there anyway of changing the table name?

I've looked at using the stored procedure sp_rename, i.e.


Code:
USE TESTDATABASE1
GO
EXEC sp_rename 'dbo.SalesTerr5673A1889', 'Values';
GO

What I would like to use is a wildcard, does anybody know how this can be done please in Transact-SQL?

Thanks,

rosmiq
Reply With Quote
  #2  
Old 09-24-2008, 08:09 PM
HorrorGamer HorrorGamer is offline
Regular
 
Join Date: Aug 2008
Location: Surprise, AZ
Posts: 89
Default

You can query can query the system view sys.tables for your tables, and sp_rename to rename the table.

When you say you want to use a wildcard, do you mean as in renaming a series of tables at once, or just to find one table with a particular prefix?

I think you will have to use a cursor to handle multiple table renames at once though.

Here is an example of renaming a single table with the prefix of SalesTerr
Code:
DECLARE @TableName NVARCHAR(128);
DECLARE @NewName   NVARCHAR(128);
DECLARE @SchemaID  INT;

SET @NewName = N'Values';
SELECT TOP 1 @SchemaID = schema_id FROM sys.schemas WHERE [name] = N'dbo';
SELECT TOP 1 @TableName = [name] FROM sys.tables tableview where tableview.name LIKE 'SalesTerr%' AND schema_id=@SchemaID;

IF @TableName IS NOT NULL
BEGIN
	SET @TableName = 'dbo.' + @TableName;
	EXEC sp_rename @TableName, @NewName;
END;
__________________
Play Tank Games
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard? Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
 
Can I rename a Table in SQL using a WildCard?
Can I rename a Table in SQL using a WildCard?
 
-->