How to create automatically a table populate script from current data in SQL Server? - sql

Is there a tool that allows me to create a sql populate script for a table using its current data?

Old question, but I got to the question before figuring out a way to solve the problem. So I'm just posting the method here in case it might help anyone else.
In SSMS:
Right click on db -> "Tasks" -> "Generate Scripts..."
Click "Next" and, at "Choose Objects" step, select the specific table
Click "Next" and, at "Set Scription Options" step, click "Advanced"
Set value for "Type of data to script", depending on what you need
Continue to finish

Check out the SSMS Addins (for both SSMS 2005 and 2008):
alt text http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=SSMSAddins&DownloadId=48526
One of the tools included in the "DataScripter" which will script out INSERT statements for the table(s) you want to.

Maybe this is just enough for you: http://snipplr.com/view/12003/generate-insert-statements/

You could look at purchasing the Data Generator by Red-Gate which can create random sets of data based on your database schema.
Note: I'm not sure if the tool will generate data based on your current data though

You can do it with Database Publishing Wizard. Here is a tutorial: Generate Script with Data from Database – Database Publishing Wizard

Another way to do it, even more convenient for developers, is within Visual Studio 2015 or 2017 (possibly earlier versions too):
Main menu 'View' -> 'SQL Server Object Explorer'
In the tree view connect to the Database of choice and select a table
Right click on the table and select 'View Data' menu item
In the [Data] window click on a 'Script' or 'Script to File' button

Related

How to use SQL Server stored procedures in Microsoft PowerBI?

I want to generate reports from my SQL Server tables.
I have some already made stored procedures that I would like to use to generate reports from.
I haven't found a way to do so.
Only by rewriting the queries.
Thanks :)
To execute your SP in Power BI-->
1.In SQL Server right click on your SP and select Execute. Your code is executed and a new query window opens up which was responsible for execution. Copy that Query.
2.In Power BI Query Editor, select New Source-->SQL Server. After giving the server and database, in the same window click on "Advanced Options", paste the query in the "SQL Statement" that opened up.
Check "Navigate using full hierarchy" and click OK.
3.You will see data for the parameters you passed in SP only.
On Applying these changes, you will see the dataset for this in Power BI Desktop from where you can create reports.
NOTE: This works in"Import Query" option.
Hope this works for you as it did for me, Cheers!
You can use openquery. The following syntax will work with both import data and direct query methods in Power BI Desktop.
SELECT *
FROM OPENQUERY ([server name],
'EXEC dbname.dbo.spname #parametername = ''R1''');
Expand the Advanced options in the Get Data -> SQL Server dialog, and write a SQL Statement to execute your stored procedure such as:
EXEC [dbo].[usp_NameOfYourStoredProcedure]
Firstly I think its best to make the point out that Power BI isn't Reporting Services, it expects to be given existing table data or views that you then model within it's own environment before creating your dashboards.
If you can get Power BI to do the work of your stored procedures. Once you have your data model defined within Power BI it can be reused.
There is a great introduction course for this on the Power BI website:
https://powerbi.microsoft.com/en-us/guided-learning/powerbi-learning-2-1-intro-modeling-data/
"Hi,
In an Excel workbook, open the ""Power Query"" tab.
Then choose the “From Database” drop down button and select “From SQL Server Database”
Fill in the Server and Database textboxes and click OK
In the Navigator window, double-click the desired table
In the Query Editor window, click on to the column to be displayed.
Click close and select the ""Load"" button
Connect to stored procedures.
Create another power window and fill in the server and database textboxes.
In the SQL statement textbox, type “EXECUTE  Procedure_Name”
Click Close and Load button
"

Is there an SQL editor for the data itself?

I'm looking for an SQL (MSSQL) tool that will allow me to edit/insert/etc. data without the need to type sql statements. I want to simply enter data into a grid. I can't find this functionality in SSMS. Is there any tool that does that (preferably by MS)?
Table Editor
If you right-click on a table in SSMS and click edit, you can edit the data in there directly.
Query Designer
If you select a row or cell in the table editor, you have access to the Query Designer menu on the tool bar. By clicking on the pane menu, it will open a sub-menu that will give you access to the SQL, Criteria and a Diagram. This will allow you to design queries visually.
SSMS does let you do it, although it's not a great tool by any stretch. If you right click on a table and go to Edit Top 100 rows, you can get the grid. If you want to be able to edit any number of rows, go to Tools -> Options -> SQL Server Object Explorer -> Commands and change both values in the Table and View options to 0, which means unlimited.
You can also edit the query that is used to generate the grid. In the toolbar, there's a button that says SQL and the hovertext is "Show SQL Pane". You can then edit the SQL to include/exclude columns or add conditions. This may help with large tables. You can also use this method to overcome the row number limit if you didn't change the defaults as in the above paragraph.
Right click the table in SSMS and click Edit Top 200 Rows:.
Using MS SQL Managment Studio you can right click on a table and select Edit. This will let you edit existing rows and add new rows by just typing into the 'grid'.
try this. may be this can help
http://www.toadworld.com/platforms/sql-server/w/wiki/10392.editing-an-sql-server-table-in-excel.aspx
A real easy thing to do is to use Access as a front end to SQL Server. It would take only a few minutes to create an ODBC connection from Access to SQL Server and you can quickly use Access's grids and forms to enter data.

show Create commands for table in SQL Management Studio (2008)

I've just started with SQL Management Studio and I am wondering if I can show create commands for already existing tables, I've been able to do that in Oracle SQL Developer.. I've tried to ask uncle google but maybe used just wrong search command.. Anyway.. could someone give me quick hint as I stuck with this for like 25 minutes?
Thanks
In Object Explorer, expand your server/database, expand Tables, right-click the table in question, and choose Script Table as > CREATE To > New Query Editor Window.
If you want to script multiple tables, you can turn on Object Explorer Details (F7 or from the View menu), highlight "Tables" on the left, then use Shift+ or Ctrl+ to select multiple tables in the right pane (just like you would select multiple files in Windows Explorer). Then you can do the same thing, right-click, Script Table as > ...
If you want to generate script for one table, right click a table and select "Script table as", you get all sorts of options including creating the create script of the table.
If you want to generate scripts for more than one table, you have option
to generate for many at once. Just follow the wizard instructions.

Generating a SQL script of my database's data (SQL SERVER)

I cannot find how to generate a script with all the INSERT i have done so far in my database
I managed to generate a script for my database itself but not for the data.
How could i do this ?
Thanks in advance
Try this, using Sql Server Management Studio:
Right click the database
Select Tasks -> Generate Scripts
(Click next if you get the intro screen)
Select "Select specific database objects"
Pick the objects to generate scripts for (tables, stored procedures, etc...)
Click Next, then specify the output filename
This will generate the schemas only. If you want to do data generating scripts as well, click the Advanced button and scroll down to the "Types of data to script" and change it from "Schema only" to "Data only" or "Schema and data"
Click Finish to generate the script
If you are using SQL Server 2008, you can generate the script for data in an sql server database by setting the Script Data option in Generate script dialog box
For bringing up Generate Scripts dialog do the following.
1. Right click on a database name in server explorer
2. Select Tasks -> Generate Scripts
3. Click next until you reaches script options
4. Under table/view options there is setting "Script Data". Change it to true to generate script for data.
If you version is the one prior to SQL Server 2008, then are many tools available like
Sql Compare from RedGate!

How do I export a table's data into INSERT statements?

How can I export a table from a SQL Server 2000 database to a .sql file as a bunch of INSERT INTO statements?
One of the fields in the table is a Text datatype and holds HTML so doing this by hand would be rather time-consuming.
I have access to SQL Server Management Studio 2008 to access the SQL Server 2000 database.
Updating since this Q&A was at the top of the search results when I was looking for the answer.
In MSSQL 2008 R2:
Right Click on database: Tasks -> Generate Scripts...
The Generate and Publish Scripts dialog will pop up. The Intro page is worthless. Click "Next"
Choose "Select Specific database objects" and then select the Table(s) you want to get Inserts for. Click Next and the dialog will advance to the "Set Scripting Options".
Click on Advanced and you should see:
Scroll down the list of Options until you find "Types of data to script". Click on that row and choose "Data Only" from the pull-down. Click "OK". Choose your Save options and click "Next" a few times.
Note - The output also includes the following after every 100 inserts.
GO
print 'Processed 200 total records'
Check out the SSMS Tool Pack - it's a great, FREE add-on for SQL Server Management Studio which does a lot of things - among other it can generate INSERT statements from a given table.
I have been using this stored procedure for a long time: sp_generate_inserts: the 2000 version and the 2005 (and up) version.
You use it like this:
sp_generate_inserts 'thetablename'
or if you want to filter:
sp_generate_inserts 'thetablename', #from='from ... where ... order by ...'
The sp will return inserts statements as query results. Don't forget to modify setting: increase the maximum number of characters displayed in each column (tools - options - query results).
If you can use other DB management apps the quickest way would be using a tool like SqlDbx which has a built-in "Export as inserts (SQL)" function (just execute a query like SELECT * FROM Table and then use the contextual menu from the result grid).
If you need to stick to SQL Management Studio then you could use a stored procedure like this one:
http://vyaskn.tripod.com/code/generate_inserts.txt
It generates a set of results with the SQL INSERT statement for each row of the target table. Then you can exports the results to a file, or just copy them to the clipboard and paste in the query window (it works fine even with several megabytes of data).