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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.