Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL reporting invalid syntax when run in Power BI

I have written an SQL script which runs fine when executed directly in SQL Management Studio. However, when entering it into Power BI as a source, it reports that it has an incorrect syntax.

This is the query:

            EXEC "dbo"."p_get_bank_balance" '2'

However, the syntax is apparently incorrect? See Picture:

enter image description here

Any help is much appreciated.

EDIT ***

When the double quotes are removed (as per Tab Alleman's suggestion):

enter image description here

like image 697
Rowan Richards Avatar asked Jan 21 '16 14:01

Rowan Richards


People also ask

What is syntax error in Power BI?

A syntax error is displayed with a yellow warning symbol appearing below the DAX expression. The message should help the author fix the code, but sometimes the text suggests a possible action without describing the underlying issue.

Can you use SQL statements in Power BI?

Using SQL and Power BI together takes the data analysis to the next level. We can easily connect the SQL Server to Power BI and extract the data directly into it. Power BI enables the users to toggle connections with a click to apply in-memory queries to a larger dataset.

How do I run a SQL code in Power BI?

Go to 'Home' tab of Power BI and click on 'Get Data' drop down and further click on 'Sql Server'. A new SQL Server database window will open. Provide server name and database name. Select 'DirectQuery' as data connectivity mode.


2 Answers

I found time ago the same problem online on power bi site:

http://community.powerbi.com/t5/Desktop/Use-SQL-Store-Procedure-in-Power-BI/td-p/20269

You must be using DirectQuery mode, in which you cannot connect to data with stored procedures. Try again using Import mode or just use a SELECT statement directly.

like image 68
nicolò grando Avatar answered Oct 17 '22 07:10

nicolò grando


In DirectQuery mode, PowerBI automatically wraps your query like so: select * from ( [your query] ), and if you attempt this in SSMS with a stored procedure i.e.

select * from (exec dbo.getData)

You get the error you see above.

The solution is you have to place your stored procedure call in an OPENQUERY call to your local server i.e.

select * from OPENQUERY(localServer, 'DatabaseName.dbo.getData')

Prerequisites would be: enabling local server access in OPENQUERY with

exec sp_serveroption @server = 'YourServerName' 
   ,@optname = 'DATA ACCESS' 
   ,@optvalue = 'TRUE' 

And then making sure you use three-part notation in the OPENQUERY as all calls there default to the master database

like image 24
Kyle Hale Avatar answered Oct 17 '22 07:10

Kyle Hale