?
Solved

VB.net Rename a SQL table

Posted on 2014-12-25
4
Medium Priority
?
193 Views
Last Modified: 2014-12-26
Hi

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

Thanks
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 79

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 2000 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:Murray Brown
ID: 40518186
Great! Thanks very much
0
 
LVL 79

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

752 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