Solved

VB.net SQL Rename a table

Posted on 2013-10-28
3
1,015 Views
Last Modified: 2013-10-29
Hi

What VB.net code would I use to rename an SQL table using
SQL Client style of coding similar to the following:

            Dim myConnection As SqlConnection = New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
            Dim myCommand As SqlCommand

            Dim ra As Integer

            Dim sSQL As String = "ALTER TABLE [" & oTable & "] DROP COLUMN [" & oColumn & "]"


            myConnection.Open()

            myCommand = New SqlCommand(sSQL, myConnection)
            ra = myCommand.ExecuteNonQuery()

            DropColumnSQL = True

            myConnection.Close()
0
Comment
Question by:murbro
3 Comments
 
LVL 6

Assisted Solution

by:ButlerTechnology
ButlerTechnology earned 250 total points
ID: 39605300
You will want to use a stored procedure to rename a table.  Here's an example:
Exec sp_rename Old Name, New Name

Open in new window



Tom
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39605939
Since it is usually something you do only once, I would not use a stored procedure. And if you are able to create a store procedure, you are also able to rename the table "manually".

Simply use the SQL command provided by ButlerTechnology in your sSQL variable.

Or you might prefer to work with the Microsoft.SqlServer.Management.Smo library, that has been designed specifically to work with the server, while SqlSlient was designed for data access.

Dim srv AsServer srv = New Server("(local)")
Dim db As Database = srv.Databases("AdventureWorks2012");
Dim tb as Table  = New Table(db, "Test Table");
tb.Rename("New Name")

Any way you chose, be aware that it won't work if the user does not have the proper rights on the database.

And also, the rename does not trigger down to whathever was already created to work with the table, such as views, stored procedures, and naturally, external code that already works on the table.
0
 

Author Closing Comment

by:murbro
ID: 39608178
Thanks very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question