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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
RogerSteinberg
Post Patron
Post Patron

Error encountered near the keyword 'user'

Hi,

 

The following query ran in SSMS works perfectly. It takes every table name from a database and looks up the most recent  value from the last_update_stamp column of each table. 

 

My query:

drop table if exists ##productUpdate
drop table if exists ##temp

CREATE TABLE ##productUpdate([table_name] varchar(30),[last_update_stamp] datetime)
   
SELECT [table_name], ROW_NUMBER() OVER (Order by [table_name] asc) as rowid 
INTO ##temp 
FROM (
	select distinct [name] as [table_name]
	from sys.Objects 
	) as a


declare @count int, @rowMax int, @date datetime, @sql nvarchar(max);
set @count = 1
set @rowMax =(SELECT Max(rowid) FROM ##temp);

WHILE @count <= @rowMax
    BEGIN
        set @sql = 'SELECT @x = MAX(last_update_stamp) FROM '+(SELECT [table_name] FROM ##temp WHERE rowid=@count)+';'
        exec sp_executesql @sql, N'@x datetime out', @date out
        INSERT INTO ##productUpdate SELECT (SELECT [table_name] FROM ##temp WHERE rowid=@count), @date
        set @count=@count+1
    END

SELECT * FROM ##productUpdate


I get the following error message: Details: "Microsoft SQL: Incorrect syntax near the keyword 'user'."

 

I made sure to use Import connection with my SSMS db.

 

Any way to solve this ?

 

5 REPLIES 5
parry2k
Super User
Super User

@RogerSteinberg reason you posted this question here this in power bi forum is that you are running these sql statements in Power Query? not sure how your question is relevent to Power BI.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

well its relevant to power bi because it works when running it in ssms but not in power bi 

maybe its in the wrong category. i created the question in the power query forum instead. thank you !

@RogerSteinberg are you doing all these steps in Power Query?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

no just when I follow the Get Data steps with SQL Server DB . ANd i insert the query in the query box.

@RogerSteinberg I don;t think you can have seperate sql statement in the query, probably better to create a stored procedure and execute that in Power BI.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.