Solved

VB.net Rename a SQL table

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

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 76

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

707 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

13 Experts available now in Live!

Get 1:1 Help Now