Solved

Alter column , entire table

Posted on 2009-05-05
8
237 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302707
welcome
0
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

679 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