I have 20 tables in my data base with the same columns.
i want to change the default value of genre(column in the table) in all of the table.
it is possible with sql statement?
I have 20 tables in my data base with the same columns.
i want to change the default value of genre(column in the table) in all of the table.
it is possible with sql statement?
With a quick research I got this:
ALTER TABLE table_name
ALTER COLUMN column_name datatype SET DEFAULT default_value
I'm not an SQL expert though, but this seems like it would do the job. Source: http://www.w3schools.com/sql/sql_default.asp. A good resource for sql questions anyway.
Just to expand on Faust's answer slightly, this could be done using SP_MSFOREACHTABLE (Undocumented procedure), removing the need to write for all tables:
EXECUTE SP_MSFOREACHTABLE
@Command1 = 'ALTER TABLE ? ADD CONSTRAINT DF_Genre DEFAULT (''Default'') FOR Genre ',
@WhereAnd = 'AND o.Name IN (SELECT Table_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = ''Genre'')'
ALTER TABLE dbo.Tbl_1
DROP CONSTRAINT DF_genre
ALTER TABLE dbo.Tbl_1 ADD CONSTRAINT
DF_genre DEFAULT ('new value') FOR genre
Replace 'DF_genre' with whatever the constraint is named on each table.