How to use SQL Server stored procedures in Microsoft PowerBI? - sql

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
"

Related

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.

Exporting a table from SQL Server 2008 R2 to a file WITHOUT external tools

I would like to export a table from SQL Server 2008 R2 to a file. The problem is that I don't have bcp (nor can I install it or anything else) and am not able to run xpcmdshell. Anyone have any ideas on how this could be done without those permissions/tools? (I would like to have this happen on some automated basis preferably)
I'm usually using Copy/Paste from SSMS Results Pane to Excel
OR
you can right click on database in the Object Explorer and select Database->Tasks->Export Data. An SQL Server Import and Export Wizard dialog opens and you will be able to export data from any table or query to the file or another destination.
OR
you can use LinqPad - awesome, simlpe and free tool (I really love it) that doesn't require installation
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You can also use a command like this too:
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product')
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial: http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
If you have SQL Server 2012 you could add File Tables to your database. Thus you could use SQL Agent to schedule a simple stored proc to update the file table when desired.
http://technet.microsoft.com/en-us/library/ff929144.aspx#Description
it has something called "Query Analyzer"
Query Analyzer (isqlw.exe) is the SQL 2000, pre-SSMS, query tool. A very fine tool. Among other things, is capable of exporting query results to a file. See https://stackoverflow.com/a/3769766/105929:
go to the Tools -> Options menu. On the Results tab, choose to send your output to a CSV file and select the "Print column headers" option.

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).

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

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

From a Query Window, can a Stored Procedure be opened into another Query Window?

Is there command inside a Query Window that will open a stored procedure in another Query Window?
i.e.
MODIFY dbo.pCreateGarnishmentForEmployee
I am using SQL Server management Studio 2005 and Red Gate's SQL Prompt.
Currently I have to do the follwowing multiple steps:
Open Object Explorer
Navigate Programmability | Stored Procedure
Right Click the Stored Procedure name
Select Modify
A Query Window will open up with the ALTER PROCEDURE.
As I mentioned above, what I would like to do is from a Query Window type
in something to the effect of
MODIFY dbo.pCreateGarnishmentForEmployee
You are trying to mix two technologies here.
SQL and SQLSyntax
The SQL Management Tool
It is probably not possible to use TSQL to manipulate the Management Studio, which is what you appear to want. I suspect cut and paste is your only option.
I think that the only way that I'm aware of that produces an outcome similar to what you're asking for is running sp_helptext against your stored procedure name
sp_helptext 'dbo.pCreateGarnishmentForEmployee'
which will output the text as a resultset. Then click on the column header and copy/paste the resultset into the query window. You'll also need to change the
CREATE PROCEDURE ...
to
ALTER PROCEDURE ...
This method does not always produce the nicely formatted layout of your stored procedure however, so bear this in mind.
There is a way to do this from the command line (i.e., from outside of SSMS).
It requires that you save your stored procedure text (as in, click "save", not execute). Here's an example:
Ssms "C:\...\SQL Server Management Studio Projects\mySolution\myProject\myScript.sql"
See the article on MSDN for more detailed info: http://msdn.microsoft.com/en-us/library/ms162825.aspx