PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Deleting multiple columns from multiple objects
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Deleting multiple columns from multiple objects

Réponse
 
LinkBack Outils de la discussion
Vieux 02/04/2008, 00h40   #1
doverj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Deleting multiple columns from multiple objects

Is there a way to delete from multiple tables/views a column with a specific
name? For example, a database has 50 tables and 25 views all have a column
named ColumnA. Is it possible to write a simple script that will delete
every column named ColumnA from the database?

Seems to be it would be possible and I can somewhat vision it using
sysobjects but without wanting to spend too much time generating the script
(when I could in shorter time manually delete) thought I'd pose the question.

Thanks.

  Réponse avec citation
Vieux 02/04/2008, 15h30   #2
Philipp Post
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Deleting multiple columns from multiple objects

Hi,

for the tables you could create a script using a cursor and do some
dynamic sql in it with ALTER TABLE ... DROP COLUMN ... getting table
names from sys.tables or INFORMATION_SCHEMA.TABLES. Regarding views I
see no proper way of automating it.

However developing and testing of that will most likely take as much
time as doing it manually (using a script window and simply replace
the table name) for 50 tables. Further scripting delete actions for
your database objects can be dangerous. If you make a mistake in it
you quickly loose a lot of things which you did not want to...

brgds

Philipp Post
  Réponse avec citation
Vieux 02/04/2008, 22h46   #3
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Deleting multiple columns from multiple objects

doverj (u42617@uwe) writes:
> Is there a way to delete from multiple tables/views a column with a
> specific name? For example, a database has 50 tables and 25 views all
> have a column named ColumnA. Is it possible to write a simple script
> that will delete every column named ColumnA from the database?
>
> Seems to be it would be possible and I can somewhat vision it using
> sysobjects but without wanting to spend too much time generating the
> script (when I could in shorter time manually delete) thought I'd pose
> the question.


SELECT 'ALTER TABLE ' + o.name + ' DROP COLUMN nisse'
FROM sysobjects o
JOIN syscolumns c ON o.id = c.id
WHERE o.type = 'U'
AND c.name = 'nisse'

For the views, I'm afraid manual editing is the only option. Hm, I think
Red Gate has a refactoring tool, but I have not looked into it.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12192 seconds with 11 queries