Solved

VB.net Rename a SQL table

Posted on 2014-12-25
4
180 Views
Last Modified: 2014-12-26
Hi

What VB.net code would I use to rename a SQL table

Thanks
0
Comment
Question by:murbro
  • 2
4 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 40517438
You have to make sure there are constraints,indexes, etc. that could prevent,

You would issue "alter table tablename rename to new tablename"
Alternatively, you could create a new table using the old ones schema, copy the data, and drop the old table.

Or use the stored procedure, sp_rename old name,newname, table
0
 
LVL 3

Accepted Solution

by:
Ali HND earned 500 total points
ID: 40517472
Sql Command:
"ALTER TABLE Old_Name   RENAME TO New_Name;"

Or:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim strConnection As String = "sp_rename 'OldTableName','NewTableName'"
        Dim cnn As SqlConnection = New SqlConnection("Data Source=(local);Database='YourDataBase';Integrated Security=yes;")
        Dim cmdDatabase As SqlCommand = New SqlCommand(strConnection, cnn)

        cnn.Open()

        cmdDatabase.ExecuteNonQuery()
        cnn.Close()
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40518186
Great! Thanks very much
0
 
LVL 77

Expert Comment

by:arnold
ID: 40518683
NOTE that you have to check for errors, as constraints, foreign keys, indexes, etc. could through an error denying the change. To change the table name in such circumstances, index, statistics, constraints, triggers have to be dropped.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now