Solved

VB.net Rename a SQL table

Posted on 2014-12-25
4
175 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

912 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

19 Experts available now in Live!

Get 1:1 Help Now