MS SQL Server with management studio - how to script a table with its data (as insert statements)? [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get script of SQL Server data
I want to move some rows from 1 table (on one server) to another (on another server).
The table schema is the same, what I need to do is generate some insert statements. Is there a way to do this using some built in / best practice way?
Import / Export seems completely extreme and excessive.
Thanks in advance.

Right-click on the database name and choose Tasks -> Generate Scripts. On the Scripting Options screen set Script Data to true.

You can do it with the publish wizard
Maybe it’s well-known, but new to me, you can just run “C:\Program
Files (x86)\Microsoft SQL
Server\90\Tools\Publishing\1.4\SqlPubWiz.exe”. This nifty little tool
can generate INSERT script very easily.
Reference here

Related

SQL Server comparing mechanism

I have a creation script for a SQL Server database and beside it I have a test script
My test Script should compare the result of my original script and the database and let me know if it is a match
I need to store the resulting script in a repository.
I've already got a script and the database. It's not hard to write the test script, but I want an app that automates the process.
I've already got Redgate tools and I heard it can make the job for me, but how? If not, are there any other tools that could do such a thing?
If you're looking to compare the structure of the two databases this can be done using Redgate SQL Compare.
Set the location of your script as a "Scripts Folder" source within the tool.
Set the database where you have run the script as the target.
Run the comparison. This will show you if there are any differences.
Generate a report of the differences which can be referred back to at a later date. (https://documentation.red-gate.com/display/SC12/Generating+a+report)
To automate this process you can make use of the SQL Compare command line. (https://documentation.red-gate.com/display/SC12/Simple+examples+using+the+command+line)
Full disclosure: I'm a developer at Redgate for SQL Compare.
I'm not sure if you are searching something to compare your data or the structure of your databases.
If it's for the structure (like Red-Gate SQL Compare and not SQL Data Compare), you can install SSDT (Microsoft SQL Server Data tools - It's free and available for visual studio).
With SSDT you will be able to create a database project and compare 2 databases structures.
Here is a quick presentation that I found on youtube:
https://www.youtube.com/watch?v=FKSsbWnFMD8
You can completely automate comparison and reporting with Apex Diff. Not sure which SQL Server are you using, but it's free for SQL Server Express.
Here is a step by step tutorial, which you should follow with one difference - set Script folder as data source:
https://solutioncenter.apexsql.com/how-to-keep-a-source-control-repository-updated-with-the-latest-sql-server-database-changes/

SQL - way to align the code in SSMS [duplicate]

This question already has answers here:
Format SQL in SQL Server Management Studio
(4 answers)
Closed 8 years ago.
Are there any provision available in SSMS to align the SQL code. Arrange the SQL SP scripts in a readable format. Whenever I align the SP scripts and run a ALTER PROC. The scripts got misaligned when i retrieve the SP code using sp_helptext.
Like how we align the C# OR .aspx codes in Visual Studio using the shortcut key Ctrl +KD, Any option available in SSMS?
Redgate: http://www.red-gate.com/products/sql-development/sql-prompt/
ApexSQL: http://www.apexsql.com/sql_tools_refactor.aspx
SQL Pretty Pinter: http://www.dpriver.com/products/sqlpp/ssms_index.php
There's a bunch -- some free, some not. Google 'SQL Format SSMS'...
You can try SQL Prompt. However it is not free.
http://www.red-gate.com/products/sql-development/sql-prompt/
You might also want to look at this answer -
SQL Formatter for SQL Management Studio
There is auto-formatting function available in SSMS. Please refer book online topic on Manage Code Formatting.

Transfer table between two SQL Server databases [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server 2005 - Export table programatically (run a .sql file to rebuild it)
I always using xls files to transfer data between two tables. But this way is very horrible. Any alternatives? Note: There is no direct network connection between the servers.
You should look into SQL Data Compare from RedGate. It will make your life much easier. Per no direct network connection, you can save the comparison in a script, and execute it on the other server.

Anyone know a program to automatically dump a bunch of test (dummy) data into a sql database? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Tools for Generating Mock Data?
I am running SQL Server 2005 and I want to dump some dummy data into a large table with about 50 columns (I did not design it :P) - anyone know a tool to do this automatically? I want the rows to have all sorts of different data, and I would rather not write a script if there is already a tool out there - thanks.
Check out Sql Data Generator by Red Gate.
Do you want the tool to generate the data, or just get it into the database? If you just want to be able to easily dump in some canned data, use Excel.
Edit: These options only apply if you already have data from another system that you want to import in. After re-reading your question I don't think my answer is relevant...
Inside SQL Server 2005 (and 2008), you have a few built-in (aka free) options to import data:
1) Management Studio
This is by far the simplest option for importing data.
1) Connect to your database inside Management Studio.
2) In Object Explorer, right click on your database then select Tasks -> Import Data
3) From here you can specify your data source, do some minor transformations on the columns, and specify what table(s) you want to dump the data to.
4) You can save this as a job in case you want to repeat this (even automate this) as a future task.
2) SQL Server Integration Services
You can use Business Intelligence Development Studio (BIDS) to create an SSIS project and have much more granularity over your data source, the transformations on your data, and how you want to import it into your destination database(s).
This tool takes some time to master, but is very useful once you get the hang of it.
And, like the previous option, SSIS packages can be put into a job and set as an automated task.
3) Command line BCP Utility
If you're interested I'd recommending Googling this option. It is a time consuming option, and there are easier ways to get data into a system. :)
I hope that helps.

How to get script of SQL Server data? [duplicate]

This question already has answers here:
What is the best way to auto-generate INSERT statements for a SQL Server table?
(24 answers)
Closed 6 years ago.
I'm looking for a way to do something analogous to the MySQL dump from SQL Server. I need to be able to pick the tables and export the schema and the data (or I can export the schema via SQL Server Management Studio and export the data separately somehow).
I need this data to be able to turn around and go back into SQL Server so it needs to maintain GUIDs/uniqueidentifiers and other column types.
Does anyone know of a good tool for this?
From the SQL Server Management Studio you can right click on your database and select:
Tasks -> Generate Scripts
Then simply proceed through the wizard. Make sure to set 'Script Data' to TRUE when prompted to choose the script options.
SQL Server 2008 R2
Further reading:
Robert Burke: SQL Server 2005 - Scripting your Database
SQL Server Management Studio
This is your best tool for performing this task. You can generate a script that will build whichever tables you wish from a database as well as insert the data in those tables (as far as I know you have to export all of the data in the selected tables however).
To do this follow these steps:
Right-click on your database and select Tasks > Generate Scripts
In the Generate and Publish Scripts wizard, select the "Select specific database objects" option
Expand the "Tables" tree and select all of the tables you wish to export the scheme and data for, then click Next
In the next screen choose how you wish to save the script (the Output Type must remain set as "Save scripts to a specific location"), then click the Advanced button in the top right corner
In the newly opened window, under the General section is a setting called "Types of data to script", set this to "Scheme and data" and click OK
Click Next, review the export summary and click Next again. This will generate the script to your selected destination.
To restore your database, simply create a new database and change the first line of your generated script to USE [Your.New.Database.Name], then execute. Your new database will now have all of the tables and data you selected from the original database.
I had a hell of a time finding this option in SQL Management Studio 2012, but I finally found it. The option is hiding in the Advanced button in the screen below.
I always assumed this contained just assumed advanced options for File generation, since that's what it's next to, but it turns out someone at MS is just really bad at UI design in this case. HTH somebody who comes to this thread like I did.
If you want to script all table rows then
Go with Generate Scripts as described by Daniel Vassallo. You can’t go wrong here
Else
Use third party tools such as ApexSQL Script or SSMS Toolpack for more advanced scripting that includes some preprocessing, selective scripting and more.
Check out SSMS Tool Pack. It works in Management Studio 2005 and 2008. There is an option to generate insert statements which I've found helpful moving small amounts of data from one system to another.
With this option you will have to script out the DDL separately.
SqlPubWiz.exe (for me, it's in C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2>)
Run it with no arguments for a wizard. Give it arguments to run on commandline.
SqlPubWiz.exe script -C "<ConnectionString>" <OutputFile>
BCP can dump your data to a file and in SQL Server Management Studio, right click on the table, and select "script table as" then "create to", then "file..." and it will produce a complete table script.
BCP info
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=319
http://msdn.microsoft.com/en-us/library/aa174646%28SQL.80%29.aspx
I know this has been answered already, but I am here to offer a word of warning.
We recently received a database from a client that has a cyclical foreign key reference. The SQL Server script generator refuses to generate the data for databases with cyclical references.