Click here to Skip to main content
15,905,963 members
Home / Discussions / Database
   

Database

 
AnswerRe: Can i add column to the middle of table in sql server. Pin
Paddy Boyd17-May-06 4:15
Paddy Boyd17-May-06 4:15 
GeneralRe: Can i add column to the middle of table in sql server. Pin
sharma sanjeev17-May-06 4:24
sharma sanjeev17-May-06 4:24 
AnswerRe: Can i add column to the middle of table in sql server. Pin
Paddy Boyd17-May-06 4:27
Paddy Boyd17-May-06 4:27 
GeneralRe: Can i add column to the middle of table in sql server. Pin
sharma sanjeev17-May-06 4:31
sharma sanjeev17-May-06 4:31 
GeneralRe: Can i add column to the middle of table in sql server. Pin
Paddy Boyd17-May-06 4:36
Paddy Boyd17-May-06 4:36 
GeneralRe: Can i add column to the middle of table in sql server. Pin
sharma sanjeev17-May-06 4:40
sharma sanjeev17-May-06 4:40 
GeneralRe: Can i add column to the middle of table in sql server. Pin
Paddy Boyd17-May-06 4:42
Paddy Boyd17-May-06 4:42 
AnswerRe: Can i add column to the middle of table in sql server. Pin
Eric Dahlvang17-May-06 7:48
Eric Dahlvang17-May-06 7:48 
It can be done, but it isn't pretty.

Whenever there is a way to do something in the Enterprise Manager, and I want to know the TSQL, I just:
1) turn on the SQL Profiler
2) do my thing in Enterprise Manager
3) turn off the SQL Profiler
4) look for the lines that did what I want to know how to do
5) copy them into SQL Query Analyzer
6) tweak it to my heart's content

--create a table with three columns
CREATE TABLE dbo.tblMyTable
	(
	Col1 int NOT NULL,
	Col2 varchar(50) NULL,
	Col3 int NULL
	)  ON [PRIMARY]


/******Add a column in 2nd position*****/
/*Now create a temporary table that is just the same,
  only insert a column between Col1 and Col2*/
CREATE TABLE dbo.Tmp_tblMyTable
	(
	Col1 int NOT NULL,
	InsertedCol int NULL,
	Col2 varchar(50) NULL,
	Col3 int NULL
	)  ON [PRIMARY]

--Take all the data from the old table, and put it in the new one
INSERT INTO dbo.Tmp_tblMyTable (Col1, Col2, Col3)
		SELECT Col1, Col2, Col3 FROM dbo.tblMyTable 

DROP TABLE dbo.tblMyTable --drop the old table

EXECUTE sp_rename 'dbo.Tmp_tblMyTable', 'tblMyTable' --rename new one

/*Add the primary key CONSTRAINT into the new table 
  (be sure to add any other indexes and triggers)*/
ALTER TABLE dbo.tblMyTable ADD CONSTRAINT
	PK_tblMyTable PRIMARY KEY CLUSTERED 
	(
	Col1
	) ON [PRIMARY]


----------
There go my people. I must find out where they are going so I can lead them.
- Alexander Ledru-Rollin

AnswerRe: Binding Select Query Result into an DataGrid Pin
Paddy Boyd17-May-06 3:45
Paddy Boyd17-May-06 3:45 
QuestionCurrencyManager Pin
ADY00717-May-06 1:28
ADY00717-May-06 1:28 
AnswerRe: Catch Error in SP Pin
Colin Angus Mackay17-May-06 1:19
Colin Angus Mackay17-May-06 1:19 
QuestionGetting last affected row Pin
NICE TO MEET16-May-06 23:53
NICE TO MEET16-May-06 23:53 
AnswerRe: Getting last affected row Pin
Colin Angus Mackay17-May-06 1:23
Colin Angus Mackay17-May-06 1:23 
QuestionRe: Getting last affected row Pin
NICE TO MEET17-May-06 2:42
NICE TO MEET17-May-06 2:42 
AnswerRe: Getting last affected row Pin
Colin Angus Mackay17-May-06 2:52
Colin Angus Mackay17-May-06 2:52 
QuestionMaximum size of XML in OPENXML Pin
NICE TO MEET16-May-06 23:30
NICE TO MEET16-May-06 23:30 
QuestionMultiple column with in keyword Pin
Shajeel16-May-06 23:30
Shajeel16-May-06 23:30 
AnswerRe: Multiple column with in keyword Pin
Paddy Boyd16-May-06 23:44
Paddy Boyd16-May-06 23:44 
QuestionRe: Multiple column with in keyword Pin
Shajeel16-May-06 23:55
Shajeel16-May-06 23:55 
AnswerRe: Multiple column with in keyword Pin
Rana Muhammad Javed Khan17-May-06 0:00
Rana Muhammad Javed Khan17-May-06 0:00 
QuestionRe: Multiple column with in keyword Pin
Shajeel17-May-06 0:10
Shajeel17-May-06 0:10 
AnswerRe: Multiple column with in keyword Pin
Rana Muhammad Javed Khan16-May-06 23:57
Rana Muhammad Javed Khan16-May-06 23:57 
QuestionBulk Update by OPEN XML in SQL 2005 Pin
NICE TO MEET16-May-06 23:28
NICE TO MEET16-May-06 23:28 
QuestionOPEN XML IN SQL 2005 Pin
NICE TO MEET16-May-06 23:27
NICE TO MEET16-May-06 23:27 
QuestionCatch the Error in Stored procedure and through into an DA Class Exception Pin
NICE TO MEET16-May-06 22:11
NICE TO MEET16-May-06 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.