Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
shavish
Helper I
Helper I

SQL syntax error

Hello all,

 

Am trying to add data from SQL server through direct query option. When i add data and i am getting following error:

Microsoft SQL: Incorrect syntax near the keyword 'DECLARE'. Incorrect syntax near ')'

 

Query is:

 

--Declare variables

DECLARE @sortin INT=1;

DECLARE @sortlabel INT=1;

DECLARE @name NVARCHAR(50)= 'Example_Name';

DECLARE @ID NVARCHAR(50)= 'Example_ID';

 

--Main Query

SELECT

Table1.names as Name,

Table1.id as ID,

 

FROM

Table1.Names as Name

Table1.id as ID,

 

1 ACCEPTED SOLUTION
hnguy71
Super User
Super User

Hi @shavish 

Power BI wraps your query statement in another SELECT statement, therefore, you won't be able to use the DECLARE function.

 

You can try this instead:

SELECT
CONVERT(varchar(50), Table1.names) As Name,
CONVERT(varchar(50), Table1.id) As ID,

FROM
Table1

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

7 REPLIES 7
v-tsaipranay
Community Support
Community Support

Hi @shavish ,

Thank you for using Microsoft Community Forum.

 

The issue comes from the DECLARE statements, Power BI’s Direct Query doesn’t support procedural SQL commands like DECLARE.

Here are a few ways to fix this:

If the values are fixed, you can hardcode them directly in the WHERE clause:

 

SELECT

    Table1.names AS Name,

    Table1.id AS ID

FROM

    Table1

WHERE

    Table1.names = 'Example_Name'

    AND Table1.id = 'Example_ID';

 

If you need flexibility, consider creating a SQL view on the database side with the logic you need. Then connect Power BI to that view for a cleaner setup.

 

If the T-SQL logic must stay dynamic switching to Import mode instead of Direct Query will support variables and more advanced SQL syntax, though it won’t reflect live data changes.

 

I hope my suggestions give you good idea, if you need any further assistance, feel free to reach out.

 

If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.

 

Thankyou.

Hi @shavish ,


I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


Thank you.

 

Hi @shavish ,

 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

 

Thank you.

Hi @shavish ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


Thank you.

 

shavish
Helper I
Helper I

No the solution didn't work. Got an error for must declare scalar variable '@name'

hnguy71
Super User
Super User

Hi @shavish 

Power BI wraps your query statement in another SELECT statement, therefore, you won't be able to use the DECLARE function.

 

You can try this instead:

SELECT
CONVERT(varchar(50), Table1.names) As Name,
CONVERT(varchar(50), Table1.id) As ID,

FROM
Table1

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

hnguy is correct but just included an extra comma.

 

SELECT
CONVERT(varchar(50), Table1.names) As Name,
CONVERT(varchar(50), Table1.id) As ID

FROM
Table1

 You cannot use the key word declare in the sql query window from power bi 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.