views:

1127

answers:

5

I'm using Visual Studio 2008 to design a SQL Server Compact Edition database (*.sdf). I had to change the schema of one of my tables adding a new column.

I want to "move" that new column from the bottom to a different position in the field list. How can I do this in the Visual Studio designer?

EDIT: I know that from a pure technical view point the order of the columns doesn't matter. I'm prototyping an application, so I have to change the schema a couple of times. If, for example I have to change the primary key, it would be "ugly" having the most important column at the end. Other developers looking at the code would be confused, I think. It's a matter of esthetics.

+2  A: 

you can't just move it in the designer. You'll have to create a new column, drop the old one, generate the script and edit the script where it inserts into the temp table from the old table, making the old column value (in the select) go into the new column.

KM
Thanks for the answer. How can I script a table in VS? I didn't find that option.
splattne
I'm using SQL Server Management Studio, so its a little different (I don't have VS). I have the "Table Designer" toolbar displayed, and I click on the icon that looks like a scroll with a diskette, and then copy/paste the text from the window that appears. Make sure to cancel out of the operation, so the changes are not run.
KM
I must be blind. I can't find the toolbar. Are you sure you used a SQL Server Compact database?
splattne
I'm not using CTE, and after checking around, you're out of luck for a Microsoft scripting solution, because there isn't one. People have said to use this: http://www.primeworks-mobile.com/Products/DataPortConsole.html
KM
Thank you, KMike! I guess the best I can do now, is drop that table and recreate it from scratch.
splattne
+1  A: 

Why? In the name of all that's holy, why?

SQL is a relational algebra. It doesn't matter where the columns are.

Edit: This was really meant to be a half-humorous answer, I'd rather you vote up a solution. This answer has a point in that it shouldn't matter since order is irrelevant to the DBMS but there may be a genuine reason for the questioner needing this.

That's why I made it community wiki so as to avoid the barrage of downvotes :-) Hopefully splattne will accept a real answer.

paxdiablo
To come out in the right order when you "SELECT *".
le dorfier
I know that it doesn't matter SQL-like. But I love conventions. And it's very ugly having that column at the end.
splattne
in a theoretical world, YES. If you had to scroll right a thousand times to view a column, you might want it moved. When adding indexes, after the fact, it is nice to keep the columns together in order, etc...
KM
@le dorfier, I don't think SQL guarantees the order of "select *" - you should be selecting specific fields if you want that.
paxdiablo
Maybe it's the viewer tools you're using that dictate order. We tend to view everything with handcrafted queries so we don't run into that problem [as an aside, @KMike, having to scroll right a thousand times probably means your table needs a schema change :-)]
paxdiablo
@Pax, Sql Server always guarantees the COLUMN order of "select *". I work daily doing manual queries at a prompt, and column order can be a huge pain in the rear. I'm not talking production queries used in the application, but if you have to list the columns each time you want to see some data, that would be a real pain! I want the PK, status, and main columns first, with all long strings last.
KM
@Pax, you say "having to scroll right a thousand times probably means your table needs a schema change :-)" and that is what he is asking about!
KM
Moving columns within a table is NOT a schema change. Moving columns to a different table would be.
paxdiablo
To be finicky: you could have to scroll right even with a handful of columns, if one of the columns is a long nvarchar field for example.
splattne
A: 

I agree with Pax. If, for a specific reason, you need to return fields in a specific order in your query, just alter the query putting the field in the place where you need it.

If, for whatever reason, you need at all costs to move that field, you can do it with a script like the following, which makes FIELD3 the first column in a table called TestTable:

/* Original sample table with three fields */
CREATE TABLE [dbo].[TestTable](
    [FIELD1] [nchar](10) NULL,
    [FIELD2] [nchar](10) NULL,
    [FIELD3] [nchar](10) NULL
) ON [PRIMARY]

/* The following script will make FIELD3 the first column */
CREATE TABLE dbo.Tmp_TestTable
    (
    FIELD3 nchar(10) NULL,
    FIELD1 nchar(10) NULL,
    FIELD2 nchar(10) NULL
    )  ON [PRIMARY]
GO
IF EXISTS(SELECT * FROM dbo.TestTable)
     EXEC('INSERT INTO dbo.Tmp_TestTable (FIELD3, FIELD1, FIELD2)
     SELECT FIELD3, FIELD1, FIELD2 FROM dbo.TestTable WITH (HOLDLOCK TABLOCKX)')
GO

DROP TABLE dbo.TestTable
GO

EXECUTE sp_rename N'dbo.Tmp_TestTable', N'TestTable', 'OBJECT' 
GO

However, I insist that probably your problem could be solved with a different approach which doesn't require restructuring table.

Diego
It's just my anal retentiveness. ;-) I can't look at a table with a column which is the most important at the end...
splattne
Ok, I hope my suggestion can be of some help then. :)
Diego
make sure you copy over all the other table items: index, fk, check constraints, etc...
KM
Hm, I'm looking for a way to script the objects. Seems to be not a trivial task for a SQL Server COMPACT database...
splattne
A: 

The database denormalization, also. For example, suppose you have two tables in a database: Orders and Order Details. The Orders table contains information about a customer's order. The individual products in each order are contained in the Order Details table.good article, i really like it. I am doing a bit on research about SQL Server Compact Edition table schema and i found also macrotesting www.macrotesting.com to be very good source. Thanks for you article.....

Regards... Meganathan..

+1  A: 

Script the database using SDF Viewer save the script, create a new blank database and reload the script, cut and paste the column definitions into your required order and then run the script.