Comment puis-je exclure les colonnes générées automatiquement de SQL Server Générer des données de script uniquement?

StackOverflow https://stackoverflow.com/questions/5021027

Question

SQL Server Generate Script fait un excellent travail de création d'un script pour les données des tables à l'aide de l'option Data uniquement pour les "types de données au script" dans l'option avancée.Cependant, le script généré comprend également tous les identificateurs tels que les ID Rowid () et les entiers.Compréhensible, ceci est pour l'intégrité référentielle, mais existe-t-il un moyen d'exclure de telles colonnes?

Était-ce utile?

La solution

Not in SSMS itself.

You can use a 3rd party tool such as the free SSMS Tools pack which has a "generate script from grid results" option. So you can generate INSERTs for SELECT col1, col3, col6 FROM MyTable (skipping some columns).

May be useful...

enter image description here

Autres conseils

There doesn't seem to be a way to do this in SQL Server, but a quick workaround is to create an initial script and then use that to create a temporary table. Then you can delete the columns you don't want and use the temporary table to generate a SQL statement for the remaining columns.

Let's say you want to copy an Events table, but you don't want to include id:

  • Go to tasks > Generate Script and generate a SQL Server script from Events. In "Advanced" options, make sure that you are copying both data and schema. Also make sure you are not scripting primary keys or foreign keys (you will still see the columns in your SQL script, but this will make it easier to quickly delete them from the temporary table).
  • Open the generated SQL script in a text editor and use find and replace to change the name of the table, i.e. from Events to EventsTemporary
  • Use this script in SQL Server to create and seed the EventsTemporary table. Delete the columns you don't want to copy from this table, such as id.
  • Generate a second script from the EventsTemporary table, but this time just copy "data" without schema. Open this new SQL script in a text editor and change the name of the table back to Events
  • Use the second script in SQL Server to import the data into your actual Events table.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top