Solved

Alter column , entire table

Posted on 2009-05-05
8
235 Views
Last Modified: 2012-06-27
Please rectify where clause of the following code to eliminate identity column.
select not_null = 'alter table ' + table_name + ' alter column ' 

                  + column_name + ' ' + data_type

                  

                  + case when data_type = 'numeric' then '(' else '' end 

                  + case when data_type = 'numeric' then convert(varchar,numeric_precision_radix) else '' end

                  + case when data_type = 'numeric' then ',' else '' end

                  + case when data_type = 'numeric' then convert(varchar,numeric_scale) else '' end 

                  + case when data_type = 'numeric' then ')' else '' end

                  + ' not null '

,default_0 = 'alter table ' + table_name 

                  + ' add default 0 for ' + column_name

from information_schema.columns

where data_type not like '%char%'

and   data_type not like '%time%'

and   data_type not like '%image%'

and   data_type not like '%binary%'

and   column_default is null

order by data_type

Open in new window

0
Comment
Question by:Mehram
  • 4
  • 4
8 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24302362
below script remove listing column with identity



select not_null = 'alter table ' + table_name + ' alter column ' 

                  + column_name + ' ' + data_type

                  

                  + case when data_type = 'numeric' then '(' else '' end 

                  + case when data_type = 'numeric' then convert(varchar,numeric_precision_radix) else '' end

                  + case when data_type = 'numeric' then ',' else '' end

                  + case when data_type = 'numeric' then convert(varchar,numeric_scale) else '' end 

                  + case when data_type = 'numeric' then ')' else '' end

                  + ' not null '

,default_0 = 'alter table ' + table_name 

                  + ' add default 0 for ' + column_name

from information_schema.columns

where data_type not like '%char%'

and   data_type not like '%time%'

and   data_type not like '%image%'

and   data_type not like '%binary%'

and   column_default is null

and columnproperty(object_id(table_name), column_name,'IsIdentity') = 0

order by data_type

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302478
is it working properly?
0
 

Author Comment

by:Mehram
ID: 24302703
is it working properly?

100 Percent.

Thanks and regards.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302707
welcome
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:Mehram
ID: 24302739
BTW,

How to avoid these tables from the list,

MSpub_identity_range
syspublications
syncobj_0x3931454436303542
syncobj_0x3931454436303542
syncobj_0x3031323230353944
syncobj_0x3031323230353944
syncobj_0x3031323230353944
syncobj_0x3031323230353944
syncobj_0x3031323230353944
MSpeer_lsns
MSpeer_lsns

I tried to take help from objectproperty but could not suceed.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302760
can't you use something like this ?



select not_null = 'alter table ' + table_name + ' alter column ' 

                  + column_name + ' ' + data_type

                  

                  + case when data_type = 'numeric' then '(' else '' end 

                  + case when data_type = 'numeric' then convert(varchar,numeric_precision_radix) else '' end

                  + case when data_type = 'numeric' then ',' else '' end

                  + case when data_type = 'numeric' then convert(varchar,numeric_scale) else '' end 

                  + case when data_type = 'numeric' then ')' else '' end

                  + ' not null '

,default_0 = 'alter table ' + table_name 

                  + ' add default 0 for ' + column_name

from information_schema.columns

where data_type not like '%char%'

and   data_type not like '%time%'

and   data_type not like '%image%'

and   data_type not like '%binary%'

and   column_default is null

and columnproperty(object_id(table_name), column_name,'IsIdentity') = 0

and table_name not like '%#%'

and table_name not like '%syncobj%'

and table_name not like '%MSpub%'

and table_name not like '%MSpeer%'

order by data_type

Open in new window

0
 

Author Comment

by:Mehram
ID: 24302831
ok.
0
 

Author Closing Comment

by:Mehram
ID: 31577912
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 ListBox Question 4 32
Near realtime alert if SQL Server services stop. 20 53
T-SQL: Subtracting Amounts from "Among Rows" 3 41
grouping logic 6 46
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now