Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net Rename a SQL table

Posted on 2014-12-25
4
Medium Priority
?
196 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 80

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 80

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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

609 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