begin transaction

declare @tabella varchar(4000)
declare @colonna varchar(4000)
declare @qry varchar(4000)
declare @val varchar(4000)


--Colonna Ordinato

set @tabella = 'Giacenza'
set @colonna = 'Ordinato'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna PackingList

set @tabella = 'Giacenza'
set @colonna = 'PackingList'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna CaricoBolleServer

set @tabella = 'Giacenza'
set @colonna = 'CaricoBolleServer'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna ScaricoBolleServer

set @tabella = 'Giacenza'
set @colonna = 'ScaricoBolleServer'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna CaricoScontriniServer

set @tabella = 'Giacenza'
set @colonna = 'CaricoScontriniServer'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna ScaricoScontriniServer

set @tabella = 'Giacenza'
set @colonna = 'ScaricoScontriniServer'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna CaricoBolleClient

set @tabella = 'Giacenza'
set @colonna = 'CaricoBolleClient'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna ScaricoBolleClient

set @tabella = 'Giacenza'
set @colonna = 'ScaricoBolleClient'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna CaricoScontriniClient

set @tabella = 'Giacenza'
set @colonna = 'CaricoScontriniClient'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna ScaricoScontriniClient

set @tabella = 'Giacenza'
set @colonna = 'ScaricoScontriniClient'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna CaricoRettifica

set @tabella = 'Giacenza'
set @colonna = 'CaricoRettifica'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna ScaricoRettifica

set @tabella = 'Giacenza'
set @colonna = 'ScaricoRettifica'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

--Colonna GiacenzaTotale

set @tabella = 'Giacenza'
set @colonna = 'GiacenzaTotale'
 
select @val = (SELECT so.name FROM sys.sysobjects AS so INNER JOIN sys.syscolumns AS sc ON so.id = sc.cdefault LEFT OUTER JOIN(SELECT name, id FROM sys.sysobjects WHERE (xtype = 'U')) AS st ON so.parent_obj = st.id WHERE (sc.name = @colonna and st.name = @tabella))

print 'Nome tabella --> ' + @tabella
print 'Nome colonna --> ' + @colonna
print 'Nome trigger default --> ' + @val

set @qry = 'alter table ' + @tabella + ' drop constraint ' + @val
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'alter table ' + @tabella + ' alter column ' + @colonna + ' decimal(18,4) not null'
exec(@qry)
print 'Eseguito --> ' + @qry

set @qry = 'ALTER TABLE ' + @tabella + ' ADD CONSTRAINT DF_' + @tabella + '_' + @colonna + ' DEFAULT 0 FOR ' + @colonna
exec(@qry)
print 'Eseguito --> ' + @qry

commit






go