Possible to load .SQL file to Power bi? - sql

Just started exploring power bi and I came across a basic question that I find difficult to get a correct solution.
Is it possible to load .sql file in power bi and create a report for it? thanks

It is not possible to load a .SQL file in a Power BI report, but you can specify a SQL query when adding a data source, if you expand the Advanced Options:
If your query is complex you can create a stored procedure to wrap all of the code and then call the stored procedure from Power BI, e.g. EXEC NameOfYourStoredProcedure

I read the SQL as text file, add a custom column, pivot and adjust to be text combine with delimiter, drop excess column, and then go into advanced editor and make SQL database query.
Here is some code to put in advanced editor. Replace the bolded with your inputs:
let
Source = Table.FromColumns({Lines.FromBinary(File.Contents("**absolute_file_path.sql**"))}),
#"for pivot" = Table.AddColumn(Source, "Custom", each 1),
pivoted = Table.Group(#"for pivot", {"Custom"}, {{"Sql", each Text.Combine([Column1], "
"), type text}}),
#"SQL Query Readable" = pivoted{[Custom=1]}[Sql],
#"Output" = Sql.Database("**your-server.database.windows.net**", "**your-database-name**", [Query=#"SQL Query Readable"])
in
#"Output"
This way you do not need to go in and change the query content in the Power BI report. Nice if changing the file a lot because then you do not need to republish report every single time. Makes dataset depend on SQL in file and not SQL in query editor.
This probably has some security flaws and is more of a loophole, if file is formatted weird perhaps this could break? However the output is the same as if you do the method of copy and paste the query into the box as described in the other answer. The main draw of doing it this way is that it allows users to
not need to republish reports every time the SQL feeding the reports is updated.

Related

Is there really no easy way to add an export statement (output to .csv) in SQL Server?

I have worked in SAS for much of my career and always found it easy to write a dataset out to csv using a proc export. I now am working in SQL Server and am not finding any similar functionality. Everything I find on the web refers to copy/paste the data into excel or using an export wizard. I don't like those options as the end goal is to automate this query and have the data output where it can be utilized by other programs. Is there any code based way to achieve this?
Are you using SSMS? CTRL-T then execute your query, you'll get a text (csv) result. Alternatively, Query -> Results to -> Results to Text. Alternatively, you can right click the results grid and select Save Results As
For automation, you can use SQL Server Agent to schedule a job the writes the output of a query to a file.

oracle sql (toad) - executing multiple queries, save to individual excel files

There are 8 separate queries I need to run and save to individual excel files. What's the best way of running this in toad rather than executing/saving each query?
Just figured out how to do this and I'm posting so that anyone else with the same problem can look at this as a possible solution.
Use the Automation Designer feature in TOAD for Oracle. Select export dataset and write in your query that you want exported and into what format you want it exported (I chose excel file). Repeat this process for all your queries. Select all "actions" and run.

Outputting SQL SERVER Query Results to CSV file

I'm using a basic query to gather data from a few joined tables, and I need to be able to export the data to a CSV (or text) file in order to be imported into Excel. The query format is:
SELECT
Item1 as 'blah'
FROM
table1 JOIN table2
WHERE Condition
GROUP BY ...
HAVING ....
I have the proper output setup correctly through the query, so I'm only looking for a way to output it to a file. If it would be easier to use a stored procedure, then it would be no problem to throw that around the query. I'm just looking for something that can write the output to a file, WITHOUT using a third-party tool, as this needs to be moderately portable.
If you need more detail from the query, I can supply that (but it really is basic).
Use a BCP utility [MSDN]
That's what most commonly used.
If you have access to SSIS (through the Business Intelligence Development Studio), create a data flow task with an OLE DB Source going to a Flat File Destination. Or, you can go straight to Excel, if you don't want to worry about converting the delimited file.

SSIS - Any other solution apart from Script Task

Team,
My objective is to data load from Excel to Sql Tables using SSIS. However the excels are quite dynamic i.e. their column count could vary OR the order of existing columns may change. But the destination table will be the same...
So I was contemplating on few options like:
1) Using SQL Command in "Excel Source" - But unfortunately I have to keep "first row as header" setting as false(To resolve the issue of Excel Connection Mngr sensing the datatype as numeric based on first few records). So the querying based on header doesnt work here.
2) The other oprtion in my mind is Script Task and write C# code to read excel based on the columns I know. So in this case the order and insertion/deletion of new columns won't matter.
Suggest me whether Script Task is the only option available for me? Any other simple way to achieve the same in SSIS? Also if possible suggest me a reference for the same.
Thanks,
Justin Samuel.
If you need to automate the process, then I'd definitely go with a script component / OleDbDataAdapter combo (you can't use a streamreader because Excel is a proprietary format). If not, go with the import wizard.
If you try to use a connection manager based solution, it's going to fail when the file layout changes. With the script component / OleDbDataAdapter combo, you can add logic in to interpret the fields and standardize the record layout before loading. You can also create an error buffer and gracefully push error values to it with Try / Catch.
Here's some links on how to use the script component as a source in the data flow task:
http://microsoft-ssis.blogspot.com/2011/02/script-component-as-source-2.html
http://beyondrelational.com/modules/2/blogs/106/posts/11126/ssis-script-component-split-single-row-to-multiple-rows.aspx
This could be done easily using "Import and Export Data" tool available with SQL Server.
Step 1: Specify your Excel as source and your SQL Server DB as destination.
Step 2: Provide necessary mappings.
Step: 3 In the final screen, you can specify to "Save as SSIS Package" and to File System. A relevant dtsx SSIS package would be created for you.
After the SQL Server Import and Export Wizard has created the package and copied the data, you can use the SSIS Designer to open and change the saved package by adding tasks, transformations, and event-driven logic.
(Since it works based on Header, order should not matter. And if a particular column is missing, it should automatically take NULL for that)
Reference: http://msdn.microsoft.com/en-us/library/ms140052.aspx

How to backup Sql Server to sql file?

In "Back UP" I only get a bak file, but I would like to create .sql file
Use SQL Server's Generate Scripts commend
right click on the database; Tasks -> Generate Scripts
select your tables, click Next
click the Advanced button
find Types of data to script - choose Schema and Data.
you can then choose to save to file, or put in new query window.
results in CREATE and INSERT statements for all table data selected in bullet 2.
This is a possible duplicate of: SQL script to get table content as "SELECT * FROM tblname"
To do a full database backup to File/Query you can use the 'Generate Scripts...' option on the Database.
Open SQL Server Management studio, right click on the database and choose 'Tasks->Generate Scripts...'
Then use the wizard to backup the database. You can script the whole database or parts of it. Two important options: In the 'Advanced' section, you will probably want to ensure 'Type of backup = 'Schema and Data' and the 'Script Statistics' is on.
This will produce a *.sql file that you can use as a backup that includes the schema and table data.
Ok, I read through most of these, but I had no "advanced button". But, there is still a way to do it, it's just a little hard to find, so here you go:
You can generate a script from a database, see http://msdn.microsoft.com/en-us/library/ms178078.aspx
If you want to create a script of your database you right-click on the databases and Generate Scripts (it's in different sub-menus depending on what version of SQL and Enterprise Manager / SQL Server Management studio you're using).
That will, however, only get you the database objects. It will not generate scripts for data. Backing up a database will give you all of the database objects as well as the data, depending on what recovery model your database is set to.
This fellow may have achieved what you are trying to do by creating the backup, and then restoring it and giving it a new name.
This approach copies the data along with all of the database objects.
If you want a file with insert statements for your data have a look here:
This procedure generates INSERT statements using existing data from the given tables and views. Later, you can use these INSERT statements to generate the data. It's very useful when you have to ship or package a database application. This procedure also comes in handy when you have to send sample data to your vendor or technical support provider for troubleshooting purposes.
http://vyaskn.tripod.com/code.htm#inserts