Forum Discussion
SQL query in direct query mode
- 9 years ago
amitabhk1971 wrote:
Well I am using only the select part of the stored procedue. I am not using exec procname.
In my test on SQL Server, in DQ mode, Power BI sends a query wrapped as below.
SELECT XXXX, XXXX, XXX, XX FROM ( your query in you input in the pbi desktop ) t
So in your case, the actually query is
SELECT XXXX, XXXX, XXX, XX FROM ( declare .... ... select.. ) t
That's why the you got the SQL syntax error. This is on SQL Server, and I think Power BI does the same thing to Azure SQL. As a workaround, instead of DECLARE statement, replace those variables in the SQL with a scalar value sub query.The CTEs can't work in this case as well, Try to recompose the SQL like.
From
FROM
DECLARE @LatestPackageVersion int; SELECT @LatestPackageVersion = MAX(PackageId) FROM [Packages]; DECLARE @CurrentTime AS datetime; SELECT @CurrentTime = GETUTCDATE() ;with cte as(
....
where datetime = @CurrentTime
and version = @LatestPackageVersion
)
select * from cte
TO
SELECT * FROM (
..
where datetime = GETUTCDATE()
and version = (select MAX(PackageId) FROM [Packages])
) cte - 9 years ago
Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works.
In fact the same stored procedure throws the correct result set in Power BI also when I click OK on the get data box. But it throws the error when I click LOAD button on the result box.
Amitabh
amitabhk1971 wrote:
In fact the same stored procedure throws the correct result set in Power BI also when I click OK on the get data box. But it throws the error when I click LOAD button on the result box.
Amitabh
I'm afraid that in DQ mode, executing a stored procedure is not supported. See this similar thread.
- amitabhk19719 years agoHelper I
Well I am using only the select part of the stored procedue. I am not using exec procname.
- Eric_Zhang9 years agoMicrosoft Employee
amitabhk1971 wrote:
Well I am using only the select part of the stored procedue. I am not using exec procname.
In my test on SQL Server, in DQ mode, Power BI sends a query wrapped as below.
SELECT XXXX, XXXX, XXX, XX FROM ( your query in you input in the pbi desktop ) t
So in your case, the actually query is
SELECT XXXX, XXXX, XXX, XX FROM ( declare .... ... select.. ) t
That's why the you got the SQL syntax error. This is on SQL Server, and I think Power BI does the same thing to Azure SQL. As a workaround, instead of DECLARE statement, replace those variables in the SQL with a scalar value sub query.The CTEs can't work in this case as well, Try to recompose the SQL like.
From
FROM
DECLARE @LatestPackageVersion int; SELECT @LatestPackageVersion = MAX(PackageId) FROM [Packages]; DECLARE @CurrentTime AS datetime; SELECT @CurrentTime = GETUTCDATE() ;with cte as(
....
where datetime = @CurrentTime
and version = @LatestPackageVersion
)
select * from cte
TO
SELECT * FROM (
..
where datetime = GETUTCDATE()
and version = (select MAX(PackageId) FROM [Packages])
) cte - amitabhk19719 years agoHelper I
Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works.
- Eric_Zhang9 years agoMicrosoft Employee
amitabhk1971 wrote:
Thanks Eric. That is what I did. I replaced the parameters with their select queries and now it works.
Glad to hear you've figured it out. If no further question, could you please mark the replies making sense as solution to close this thread?