Update A TIMESTAMP Column To Be Nullable
Solution 1:
I don't think you can. And a table can only have one timestamp column.
You also cannot update a timestamp column, so the old "copy/drop/update" trick won't work.
You are likely stuck with what you have.
create table #tmp (id int, timestamp null)
insert into #tmp (id) values (1)
select * from #tmp
create table #tmp2 (id int)
insert into #tmp2 (id) values (1)
alter table #tmp2 add timestamp null
select * from #tmp2
I'm testing NULL TIMESTAMP columns, and it wont actually go NULL -- I can't find any documents stating it, but I don't think a NULL TIMESTAMP is possible (even if declared null, its never null in the data).
timestamp is the synonym for the rowversion data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible. For more information, see Data Type Synonyms (Transact-SQL)...
http://msdn.microsoft.com/en-us/library/ms182776(v=sql.110).aspx
Also...
ALTER COLUMN Specifies that the named column is to be changed or altered. The modified column cannot be any one of the following: A column with a timestamp data type...
http://msdn.microsoft.com/en-us/library/ms190273(v=sql.110).aspx
Solution 2:
You can't alter a TIMESTAMP column (http://msdn.microsoft.com/en-us/library/ms190273.aspx) You'll have to do the roundabout way of renaming the old table, creating a new table with the desired schema, inserting the data from the renamed table, then dropping that old table. SSMS will probably script this for you if you change the column in the GUI.
ALTER COLUMN
Specifies that the named column is to be changed or altered.The modified column cannot be any one of the following:
•A column with a timestamp data type.
Solution 3:
Yeah, you can't do this. It almost sounds like you'd want to use a date or datetime datatype for that column if you want it to be nullable. I think of TIMESTAMP being analogous to the IDENTITY datatype in that they are both self-filling, auto-incrememting columns which don't really make sense to be null. Also note that if you try the following:
if object_id('tempdb..#timestamptable') is not null
drop table #timestamptable
create table #timestamptable (id int, ts timestamp null)
insert into #timestamptable (id, ts)
values(1, null),
(2, null)
select *
from #timestamptable
Your timestamp column will still have data:
id ts
1 0x000000004C8BED2B
2 0x000000004C8BED2C
Solution 4:
FORCE THE TYPE ALTER TABLE [MyTable] ALTER COLUMN ROW_VERSION TIMESTAMP
remove indexes if exists
ALTER TABLE SOMETABLE DROP CONSTRAINT DF__SOME__index
maybe add with DEFAULT VALUE
ALTER TABLE [MyTable] ADD DEFAULT 0 FOR ROW_VERSION
I will just drop the table and recreate if allow it
Post a Comment for "Update A TIMESTAMP Column To Be Nullable"