Syntax error in Alter table command

bobscott 41 Reputation points
2023-08-05T17:26:13.49+00:00

I am trying to use command string "ALTER TABLE PIN_Table ORDER BY PIN ASC" PIN_Table and column PIN exist. I am getting "Syntax error near ORDER" I don't see my problem. Have tried a number of different things and nothing fixes the problem.

Man, I am having trouble with SQLite and it has to be me!! All help appreciated.

Bob Scott

SQL Server | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Konstantinos Passadis 19,591 Reputation points MVP
    2023-08-05T18:38:29.8233333+00:00

    hello @bobscott !

    To provid addiotional helpi am posting a solution where we create a New Table from PIN_ASC with ODERED Data:

    ALTER TABLE PIN_Table ORDER BY PIN ASC;

    Then when running SELECT we have our Order

    SELECT * FROM PIN_Table ORDER BY PIN ASC;

    User's image

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Konstantinos Passadis 19,591 Reputation points MVP
    2023-08-05T18:13:36.37+00:00

    hello @bobscott !

    The ORDER BY clause is not supported in the ALTER TABLE statement in SQLite. This means that you cannot use the ORDER BY clause to sort the rows of a table while altering it.

    https://www.sqlitetutorial.net/sqlite-group-by/

    If you want to sort the rows of the PIN_Table table by the PIN column, you can use the SELECT statement with the ORDER BY clause instead. For example, you can use the following command:

    SELECT * FROM PIN_Table ORDER BY PIN ASC;

    https://www.tutorialspoint.com/sqlite/sqlite_alter_command.htm

    https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm

    https://www.tutorialspoint.com/sqlite/sqlite_order_by.htm

    https://scaron.info/blog/aggregate-functions-in-sql.html

    https://www.guru99.com/sqlite-query.html

    Another post that may help is here :

    https://www.sqlitetutorial.net/sqlite-alter-table/

    I am re creating your issue:

    ERROR MESSAGE :

    User's image


    With Select though :

    User's image

    I hope this helps!

    The answer or portions of it may have been assisted by AI Source: ChatGPT Subscription which was tested and is displayed above

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    0 comments No comments

  2. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2023-08-05T21:49:33.2966667+00:00

    I am trying to use command string "ALTER TABLE PIN_Table ORDER BY PIN ASC" PIN_Table and column PIN exist. I am getting "Syntax error near ORDER" I don't see my problem.

    I do see your problem. There is such syntax in SQL Server. Hm, you mention SQLite, but you have tagged the post SQL Server - those are two quite different product. (If you work with SQLite, you should probably find an SQLite forum somewhere; as SQLite is not a Microsoft product, this is not really the place.

    Anyway, I doubt that the syntax you are trying works in SQLite either, because it does not really make any sense. A table in a relational database is by definition an unordered object, you cannot order it. You can add an index to it which imposes a physical ordering, but that's some different from the table definition.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.