Giter Site home page Giter Site logo

mikekiwa / reorder-columns Goto Github PK

View Code? Open in Web Editor NEW

This project forked from froebel/reorder-columns

0.0 1.0 0.0 15 KB

Let's you reorder the columns in a table on sql-server

License: GNU Lesser General Public License v3.0

SQLPL 3.60% PLSQL 96.40%

reorder-columns's Introduction

reorder-columns

This mini-project should offer an easy script-only-way to reorder columns in an existing table on sql-server. We all know this: Over the years, new columns are always added to the right end of a table. And when debugging the application then, you have to scroll far the right to get the important information.

For a single database that you access directly, SSMS seems to be able to do that. I never tried that, since all our databases are only deployed and updated using scripts. And that's why I wrote this script: It automates the process of recreating the table with the new order of the columns.

It's very easy to use: Download and execute fSplit1NVCWithRow.sql and pReorderColumns.sql. Then you can easily reorder the columns of any table by using pReorderColumns. You can find an example in Example.sql.

How it works

  • The script analyses the existing table and it's relations to other tables and views.
  • All relations are deleted then.
  • The existing table is then renamed to a temporary name.
  • A new table is created with the new order of columns.
  • All data is copied
  • All relations to other tables and views are created again.

Supported features

  • All types of columns
    • Identity-columns (only of type (1,1))
    • Default-constraints
    • Check-constraints
  • Foreign keys
    • Incoming from other tables
    • Outgoing to other tables
    • ON DELETE CASCADE
    • ON UPDATE CASCADE
  • Indexes
    • Nonclustered and clustered
    • Unique and non-unique
    • Partial indexes (WHERE ...)
    • Indexes with INCLUDE
  • Views
    • Recreate views that reference the table
    • Does also work for indexed views

If you miss anything, please feel free to add it and let me know.

Caveats

  • For the whole process, the table has to be taken offline.
  • There will be issues if you select all columns (SELECT * FROM Foo) and access the columns via index (you should not do this anyway!)
  • If you use sql-features, that are not yet supported by this script, these will be lost. E.g. during development of htis script, we remembered to recreate the views, but forgot about indexes on the views.

Versions of Sql-Server

Currently, this is only tested in SqlServer 2008 R2. Please feel free to let me know if it works in other versions as well - or provide a patch that makes it work.

reorder-columns's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.