Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
Error "SQL command not properly ended" when using "order by" clause and "use last run datetime" field - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Error "SQL command not properly ended" when using "order by" clause and "use last run datetime" field
KB0856090

Error "SQL command not properly ended" when using "order by" clause and "use last run datetime" field


547 Views Last updated : Apr 8, 2025 public Copy Permalink
KB Summary by Now Assist

Issue

When customer using JDBC datasources, configure a SQL query to run on the Source DB Server.

In this SQL query if customer has Order By clause, there are following scenarios.

1. When Checkbox for Use Lastrun datetime is set to False

2. When Checkbox for Use Lastrun datetime is set to True


In Scenario 1 there is no issue observed.

In Scenario 2 when User Last run datetime is set to True and customer also uses Order By clause in the SQL query, it throws an error - "SQL command not properly ended".

Cause

When we run the datasource (Order By clause in SQL Statement and User Last run DateTime option set to true), system appends Order By '<Last_run_datetime field> clause at the end of the sql statement. Causing 2 Order By clause.


e.g - 

if original sql statement with order by clause is - 

SELECT
CUR.ISO_CODE,
EX.SDL_ID,
EX.FACTOR / USD.FACTOR AS FACTOR_USD,
EX.ACTIVE_FROM_DATE ,
EX.ACTIVE_TO_DATE,
to_char(EX.MDM_INTERFACE_UPDATE_TIMESTAMP) as LAST_UPDATE,
EX.MDM_INTERFACE_UPDATE_TIMESTAMP
FROM MDM.MV_CUR_CURRENCY CUR,
MDM.MV_CUR_EXCHANGE_RATE EX,
(SELECT BASE_CURRENCY_SDL_ID,
EX.ACTIVE_FROM_DATE,
EX.ACTIVE_TO_DATE,
FACTOR
FROM MDM.MV_CUR_EXCHANGE_RATE EX, MDM.MV_CUR_CURRENCY CUR
WHERE EX.BASE_CURRENCY_SDL_ID = CUR.SDL_ID
AND CUR.ISO_CODE = 'USD'
AND EXCHANGE_RATE_TYPE_CODE = 'QQ') USD
WHERE CUR.SDL_ID = EX.BASE_CURRENCY_SDL_ID
AND EX.ACTIVE_FROM_DATE = USD.ACTIVE_FROM_DATE
AND EX.ACTIVE_TO_DATE = USD.ACTIVE_TO_DATE
AND EX.EXCHANGE_RATE_TYPE_CODE = 'QQ'
AND EX.MDM_INTERFACE_UPDATE_TIMESTAMP >= '30-JUN-20 01.00.36.000000 AM'
ORDER BY EX.MDM_INTERFACE_UPDATE_TIMESTAMP ASC

Then SQL statement resulting due to Use Last run datetime is - 

SELECT
CUR.ISO_CODE,
EX.SDL_ID,
EX.FACTOR / USD.FACTOR AS FACTOR_USD,
EX.ACTIVE_FROM_DATE ,
EX.ACTIVE_TO_DATE,
to_char(EX.MDM_INTERFACE_UPDATE_TIMESTAMP) as LAST_UPDATE,
EX.MDM_INTERFACE_UPDATE_TIMESTAMP
FROM MDM.MV_CUR_CURRENCY CUR,
MDM.MV_CUR_EXCHANGE_RATE EX,
(SELECT BASE_CURRENCY_SDL_ID,
EX.ACTIVE_FROM_DATE,
EX.ACTIVE_TO_DATE,
FACTOR
FROM MDM.MV_CUR_EXCHANGE_RATE EX, MDM.MV_CUR_CURRENCY CUR
WHERE EX.BASE_CURRENCY_SDL_ID = CUR.SDL_ID
AND CUR.ISO_CODE = 'USD'
AND EXCHANGE_RATE_TYPE_CODE = 'QQ') USD
WHERE CUR.SDL_ID = EX.BASE_CURRENCY_SDL_ID
AND EX.ACTIVE_FROM_DATE = USD.ACTIVE_FROM_DATE
AND EX.ACTIVE_TO_DATE = USD.ACTIVE_TO_DATE
AND EX.EXCHANGE_RATE_TYPE_CODE = 'QQ'
AND EX.MDM_INTERFACE_UPDATE_TIMESTAMP >= '30-JUN-20 01.00.36.000000 AM'
ORDER BY EX.MDM_INTERFACE_UPDATE_TIMESTAMP ASC
ORDER BY <Last_run_datetime_time>

The 2 ORDER BY clauses in the SQL statement cause an error - SQL command not properly ended".

Resolution

Users don't really need to add Order By clause to the SQL Statement, as when you run the Load All Data, backend code appends the ORDER BY <Last_run_datetime_field> clause to the end of SQL Statement by default.

Please note - Running Load 20 records doesn't add the ORDER BY clause to the SQL statement.

So to the crux of it, Using ORDER BY clause to the SQL statement + Use of "Use last run datetime" is not possible.


The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.