sql server - Change all spaces to underscores for all tables and fields without losing data -


i learning sql server 2008 r2 , have created db, naming convention flawed.

  1. how can change spaces underscores tables , fields without losing data.
  2. must stored procedure or there better alternative?

any in creating 'code' appreciated!

here expansion on accurate answer provided @sonam. keep in mind references tables broken after renaming.

declare @oldtablename sysname declare @newtablename sysname declare tablecursor cursor fast_forward     select         name             sys.tables             name '% %'     order         name  open tablecursor  fetch next      tablecursor       @oldtablename  while @@fetch_status = 0 begin     set @newtablename = replace(@oldtablename, ' ', '_')      print 'renaming table:'     print '  old: ' + @oldtablename     print '  new: ' + @newtablename      exec sp_rename @oldtablename, @newtablename      fetch next          tablecursor               @oldtablename end close tablecursor deallocate tablecursor 

Comments