November 14, 2006 at 11:44 am
We've got a table with 2 fields in it, both are defined as varchar(2000). I am thinking of changing the fields to varchar(MAX), or maybe even nvarchar(MAX). If I do, will we loose the data that is correctly stored in those fields of that table? We've got 10 years worth of data in that table, which we cannot afford to loose.
Kindest Regards, Rod Connect with me on LinkedIn.
November 14, 2006 at 11:56 am
Begin Try
Begin Tran
Alter table Table1
alter column column1 varchar(max) not null
End Try
Begin Catch
Rollback
End Catch
This works.
If you are worried, try it in Development. You do have backups dont you...!!!!!
Go for it.
November 14, 2006 at 11:57 am
oops..
Forgot the Commit Tran at the end......You get the essence of it...
November 14, 2006 at 11:58 am
Feels like Monday. The commit is before the End Try...
November 14, 2006 at 12:35 pm
Ya try in developpement first. Once you get it working. Take backup of production db and run on Production DB. Take another backup and go take a break .
November 14, 2006 at 1:19 pm
If you do that I can garantee you another extended vacation right after that one .
November 14, 2006 at 1:20 pm
Another alternative to try in development first is this:
1. Create a new table called tmp_tablename that is identical to your existing table, but with the columns your are changing defined as varchar(max) or nvarchar(max).
2. Insert all data from tablename to tmp_tablename.
3. drop (or rename using the function in 4) tablename.
4. exec sp_rename 'dbo.tmp_tablename', 'tablename';
November 14, 2006 at 1:23 pm
I see no need to use an intermediate table for this... The task is too simple.
November 14, 2006 at 1:42 pm
Just an alternative, and it ensures you don't lose data if that is a concern.
November 14, 2006 at 1:58 pm
Can't lose data if you test the solution and have a backup before running the solution .
Also another solution I use to test something like this :
Select IDCol FROM dbo.MyTable WHERE ISNULL(Col1, '') <> ISNULL(CONVERT(VARCHAR(newsize)), '')
If nothing is found then the alter command will be safe so you can run something like this :
--you could even lock the entire table before runnig the exists statement just to be 100% sure
IF NOT EXISTS (Check query)
ALTER TABLE
else
error out or whatever
end
--another alter...
--unlock here
November 14, 2006 at 2:14 pm
There are several ways to change fields. In place as was originally provided; using a temporary table; and a third is to add two new columns, copy the data from the original columns to the new columns, and then drop the old columns.
Your method above is a variation of the first.
It really is up to the individual charged with making the change, plus any policies that may be in place regarding schema changes, and which way they feel the most comfortable doing it.
Beyond that, we can only provide suggestions on how to things can be done.
November 14, 2006 at 4:15 pm
Ninja,
You wrote:
Select IDCol FROM dbo.MyTable WHERE ISNULL(Col1, '') <> ISNULL(CONVERT(VARCHAR(newsize)), '')
What I am not following is the condition in the WHERE clause:
ISNULL(Col1, '') <> ISNULL(CONVERT(VARCHAR(newsize)), '')
What are you accomplishing here, please?
Kindest Regards, Rod Connect with me on LinkedIn.
November 14, 2006 at 11:05 pm
On the contrary, this is a much simpler way to handle this than creating a full backup. When making a change on a single table, I've often done a simple Select Into to "backup" a single table. In many ways this is actually safer. If others are actively using the database (developers in dev or client users/app's in production), you don't have to roll back the entire database if an error happens losing work being done by other developers or client transactions.
Of course, in SQL 2005, I can just use a snapshot and query from that directly to roll back the data in the table.
Of course, the question wasn't how to do it. The question was would he lose data if he converted a varchar(2000) column to either varchar(max) or nvarchar(max). The answer to that question is no. You can potentially lose data if you change it to a smaller or more restrictive data type. You only lose it if can not fit into the new column definition.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply