Avatar of robertkc
robertkc
 asked on

Database add two new columns automatically

I would like to automatically add the following two tables to every table in the database

rowguid : uniqueidentifier <-- set default value to newid()
ModifiedDate : datetime <-- set default value to "getdate()

How would I do this?
Microsoft SQL Server 2008Microsoft SQL Server 2005Databases

Avatar of undefined
Last Comment
robertkc

8/22/2022 - Mon
isaackhazi


DECLARE Alter_tables_cursor CURSOR
   FOR
   select table_name from information_schema.tables where table_name<>'dtProperties' and table_type<>'VIEW'
OPEN Alter_tables_cursor
DECLARE @tablename sysname
FETCH NEXT FROM Alter_tables_cursor INTO @tablename
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
 PRINT 'Altering  Primary Key -->'+@tablename+' '
 EXEC('ALTER TABLE '+@tablename+' ADD  rowguid  DATATYPE NULL ')
 EXEC('ALTER TABLE '+@tablename+' ADD  ModifiedDate   DATETIME NULL ')
 FETCH NEXT FROM Alter_tables_cursor INTO @tablename
 
END
PRINT 'All user-defined tables have been Altered.'
DEALLOCATE Alter_tables_cursor

Open in new window

isaackhazi

Make sure u add the column as mentioned above with allow NULLs as NULL or else it will generate an error. You cannot add columsn with NOT NULL to an existing table.

Cheers
isaackhazi

DECLARE Alter_tables_cursor CURSOR
   FOR
   select table_name from information_schema.tables where table_name<>'dtProperties' and table_type<>'VIEW'
OPEN Alter_tables_cursor
DECLARE @tablename sysname
FETCH NEXT FROM Alter_tables_cursor INTO @tablename
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
 PRINT 'Altering  Primary Key -->'+@tablename+' '
 EXEC('ALTER TABLE '+@tablename+' ADD  rowguid  DATATYPE NULL ')
 EXEC('ALTER TABLE '+@tablename+' ADD  ModifiedDate   DATETIME NULL ')
 EXEC('UPDATE  '+@tablename+' SET rowguid = ' & newid() & "'"')
 EXEC('UPDATE  '+@tablename+' SET ModifiedDate = ' & date() & "'"')
 FETCH NEXT FROM Alter_tables_cursor INTO @tablename
 
END
PRINT 'All user-defined tables have been Altered.'
DEALLOCATE Alter_tables_cursor
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
isaackhazi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
robertkc

ASKER
ah, sorry only need to add them to "physcial" tables only not in any other table view etc

ROWGUID is used for database replication, it needs the default value set to "newid()" and replication to be turned on (see example below)
ModifiedDate is used to track changes made to the table, it needs the default value set to "getdate()".



Example of create table Query with database replication turn on ROWGUIDCOL
--------------------------------------------------------------------------
create table TableA (
TableAID int identity not null,
Name nvarchar(50) not null,
rowguid uniqueidentifier *ROWGUIDCOL* default (newid()) not null unique, <--- need this to be added to every physical table (as shown)
ModifiedDate datetime default (getdate()) not null,     <--- need this to be added to every physical table (as shown)
TableBID int null, constraint PK_TableA primary key (TableAID));

Open in new window

robertkc

ASKER
Requirements:

Add the following two columns to each table in the database, (excluding view tables).


ColumnName: Modified
DataType: datetime
Default value: getdate()
Unique: yes

ColumnName: rowguid
DataType: uniqueidentifier
Default value: newid()
Unique: yes
(with database replication turned on)

SOLUTION
BrandonGalderisi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Sharath S

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
robertkc

ASKER
solution has not been tested.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.