Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My Experiments with SQLServer
Browse by Tags · View All
SQLServer 126
SQLServer 2008 R2 91
SQLServer 2008 86
SQLServer 2005 82
SQL 60
Database 59
Sql And Me 57
Tips & Tricks 28
SQL Server 27
SQL FAQ 26

Archive · View All
May 2011 25
June 2011 21
July 2011 21
August 2011 19
April 2011 16
January 2013 4
May 2012 3
April 2012 3
October 2011 3
February 2013 2

Vishal Gajjar's Blog

SQL Server – ALTER COLUMN – Management Studio v. T-SQL

Aug 4 2011 12:16AM by Vishal Gajjar   

Whenever we need to update a column length, we can do the same by using wither Table designer in Management Studio or by using T-SQL. Changing this using Management Studio adds an overhead of recreating the table, which can cause external fragmentation in the database. Let’s compare both these methods, I have created a sample table as below:

USE   SqlAndMe
 
– Table structure
CREATE Table ProductList
(
      ProductID   INT,
      Name        VARCHAR(50)
)
 
– Sample Data
INSERT INTO ProductList
VALUES (1,'Adjustable Race'),
       (2,'Bearing Ball'),
       (3,'BB Ball Bearing'),
       (4,'Headset Ball Bearings')

You can check the data and index pages of the table by using DBCC IND(@Databasename, @Tablename, @IndexID).

–@IndexID -1 – for all Index and data pages
DBCC IND('SqlAndMe', ProductList, -1)

Partial Result Set:

PageFID       PagePID       IAMFID        IAMPID        ObjectID        ……
——-           ——-           ——            ——            ———-            ……
1             268           NULL          NULL          1461580245      ……
1             267           1             268           1461580245          ……

From the output of DBCC IND, we can see that paged occupied by the table ProductList are 268 and 267.

Now, let’s alter column [Name] of ProductList to VARCHAR(100):

1. ALTER COLUMN using Management Studio:

You can launch the design view by right-clicking table in object explorer and choosing “Design”, and make the changes:

The script generated by Management Studio for this change is as below:

/* To prevent any potential data loss issues,
you should review this script in detail before
running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_ProductList – (1)
(
      ProductID int NULL,
      Name varchar(100) NULL
)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_ProductList SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.ProductList) – (2)
       EXEC(‘INSERT INTO dbo.Tmp_ProductList (ProductID, Name)
             SELECT ProductID, Name FROM dbo.ProductList WITH
             (HOLDLOCK TABLOCKX)’)
GO
DROP TABLE dbo.ProductList – (3)
GO
EXECUTE sp_rename N'dbo.Tmp_ProductList', N'ProductList', 'OBJECT'  – (4)
GO
COMMIT

As you can see from this script that Management Studio does not modify existing table. Instead it,

  1. Creates a new table with modified structure,
  2. Copies data from old table to new table,
  3. DROPs old table, and finally
  4. Renames new table to old table’s name

This approach causes the table to move to different set of pages in the database, causing external fragmentation. Also, if you have a large table, this can be time consuming.

After, changing the column using Management Studio, you can verify the movement of the table by using DBCC IND:

DBCC IND('SqlAndMe', ProductList, -1)

Partial Result Set:

PageFID       PagePID       IAMFID        IAMPID        ObjectID
——-           ——-           ——            ——            ———-
1             279           NULL          NULL          1461580245
1             276           1             279           1461580245

From the output you can see that the PagePID have changed.

2. ALTER COLUMN using T-SQL:

Changing column length using T-SQL is straight forward, you can change it using below T-SQL code:

ALTER TABLE ProductList
ALTER COLUMN [Name] VARCHAR(100)

Also, this approach changes the structure in-place, un-like Management Studio it makes changes to original table itself. You can verify the same by checking PagePIDs from the output of DBCC IND.

DBCC IND('SqlAndMe', ProductList, -1)

Partial Result Set:

PageFID       PagePID       IAMFID        IAMPID        ObjectID
——-           ——-           ——            ——            ———-
1             268           NULL          NULL          1461580245
1             267           1             268           1461580245

Hope This Helps! Cheers!


Republished from Sql&Me [31 clicks].  Read the original version here [32134 clicks].

Vishal Gajjar
46 · 4% · 1276
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Just two days ago I had some fun with this topic. But I hadn't the time to verify why changing the field length via SSMS took so long. In some cases it was impossible to change it via SSMS, so I needed to use the console.

    Thanks Vishal for your very interesting posting!

    Best wishes,

    Michael

    commented on Oct 14 2011 4:13AM
    Michael Mikic
    584 · 0% · 61

Your Comment


Sign Up or Login to post a comment.

"SQL Server – ALTER COLUMN – Management Studio v. T-SQL" rated 5 out of 5 by 3 readers
SQL Server – ALTER COLUMN – Management Studio v. T-SQL , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]