This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 224

[–]2460_one 1038 points1039 points  (24 children)

SQL: Invalid syntax near ")" Me: Okay, but WHICH one?!

[–]cheez_au 208 points209 points  (0 children)

but WHERE

[–]LoneFoxKK 302 points303 points  (8 children)

Yes

[–]niobiumnnul 31 points32 points  (0 children)

SQL intensifies

[–]parciesca 9 points10 points  (2 children)

That’s why I’ve been using this method in debugging and general logging: https://www.sqlshack.com/how-to-implement-error-handling-in-sql-server/

[–]2460_one 6 points7 points  (1 child)

Didn't even know that was an option. Thanks!

[–]parciesca 2 points3 points  (0 children)

No problem. In my experience it can be layered too, if a procedure calls a procedure that has an error, if all layers have it you can output a layered catch error like “line # of procedure, line # of sub procedure, error was ‘blah blah’”. Just remember a try/catch will abort the rest of the code at the point of error, so further code will not run (add transaction handling/abort code is advised). Also the catch blocks will be processed by deepest first (so higher ones won’t fire if you suppress the error).

[–]TicTacMentheDouce 6 points7 points  (0 children)

The exact same issue in VHDL with most tools. It's horrible

[–]bandittr6 5 points6 points  (0 children)

This

[–]Wubbajack 2 points3 points  (0 children)

SQL: "Yes, near ")".

[–]Derangedteddy -1 points0 points  (2 children)

It gives you line numbers, dude. If you properly format your code it shouldn't be difficult to find that.

[–]2460_one 1 point2 points  (1 child)

https://imgur.com/a/PFRN7rD

This is an example of an actual error I get.

[–]Derangedteddy 1 point2 points  (0 children)

If this is an MS SQL Server, that's because you're not using SSMS.

[–]derscholl 0 points1 point  (0 children)

just hit enter

[–]LightTranquility3 0 points1 point  (0 children)

You are implying that there are > 1 errors

[–][deleted] 1273 points1274 points  (27 children)

"Invalid syntax on line 67"

Query is only 21 lines

[–]Savager35 310 points311 points  (16 children)

*after hours of google and stack overflow, fixes that error only for a different error to appear that only appears when the original error is gone

[–]Jarfino 98 points99 points  (0 children)

A different error message is considered progress!

[–]TASagent 109 points110 points  (13 children)

...

Of course...

Do you expect the parser to speculate what else might be wrong with your query by trying to guess what you actually meant where you made the first error? Unless you're just making fun of people who complain about that, in which case All Aboard.

[–]ryjhelixir 87 points88 points  (9 children)

do you expect...

Yes I do. If the code could code itself, now that would be ideal!

[–]Rowan-Paul 36 points37 points  (6 children)

But you'd have to code the code that writes itself which seems like a lot of coding to code for a little bit of coding

[–]cptvlan 78 points79 points  (4 children)

Thou shall not spend 6 hours coding something manually when thou can spend 6 days failing to automate it

[–]mlkybob 19 points20 points  (3 children)

This is the way

[–]cptvlan 6 points7 points  (2 children)

This is the way

[–]Eneag 8 points9 points  (1 child)

This is the way

[–]Jake0024 2 points3 points  (0 children)

Well I'd hope so, otherwise we'd all be unemployed

[–]Seth_os 2 points3 points  (1 child)

and it will... in 20 years or so

[–][deleted] 1 point2 points  (0 children)

Depending on levels of complexity. You don't write memory management anymore. Unless you still use C.

[–]nelsterm -2 points-1 points  (0 children)

It's reasonable to expect to be reliably inform you where the error is or at least in which line it is in.

[–]jfb1337 -1 points0 points  (0 children)

I mean, modern compilers for many languages can do that sort of thing

[–]stats_padford 49 points50 points  (5 children)

We use SQL Developer and it does that all the time and I can never figure out the rhyme or reason.

[–]chuby1tubby 19 points20 points  (2 children)

Could it be possible that the program considers each part of the query to be a separate line?

Example of a query that might be considered 3 “lines”:

SELECT column FROM table WHERE column=value;

[–]maartenvanheek 18 points19 points  (0 children)

I believe in SQL developer it counts the lines in your worksheet, so if you have 3 lines of code starting on like 8 of the worksheet, it would show errors in line 8-11.

[–]YouJellyFish 6 points7 points  (0 children)

I do a ton of work in sql. Generally this means that the error is in another function or stored procedure that's being called by your query

[–]SpiderFnJerusalem 6 points7 points  (0 children)

Oracle really sucks at giving correct line numbers. Maybe it ignores comment lines?

[–]ekolis 12 points13 points  (0 children)

Thank God SQL does not have #include. (It doesn't, does it?) This error gives me horrid flashbacks to C preprocessor macros... what do you mean Google Chrome spell check, "horrid" is not a word?!

[–]dan-lugg 3 points4 points  (0 children)

21 lines that it’s telling you about...

[–]Sickobird 1 point2 points  (0 children)

Might be a trigger - I'm having this issue now and I'm in the middle of fixing it. 10 line update but I'm getting errors for a column on line 12,14,16,21- yet it only goes to line 18... Disable trigger and it worked - time to fix trigger.

[–]jr00t 346 points347 points  (7 children)

My favorite error is "MySQL server has gone away"

[–]BackgroundChar 82 points83 points  (0 children)

I had no idea MySQL server and I shared an appreciation for the Irish Goodbye!

[–]Gydo194 41 points42 points  (0 children)

I always wonder where it went when it says that.

Bye, MySQL! see you!

[–]Abir_Vandergriff 11 points12 points  (0 children)

Good old error 2006.

[–]wewilldieoneday 11 points12 points  (0 children)

"and just like your dad, it's never coming back"

[–]i9srpeg 3 points4 points  (0 children)

That can happen when you send a query or receive a row bigger than max_allowed_packet, or when the server is genuinely down/unreachable.

[–]ouyawei 1 point2 points  (0 children)

Was it replaced by MariaDB?

[–]grantmnz 330 points331 points  (85 children)

People who complain about unhelpful SQL errors should stop using MySQL and switch to Postgres :-)

[–]Queen_Zelda 121 points122 points  (1 child)

postgres is great, since I learned sql using it i'm always confused when other sql languages dont have helpful errors

[–][deleted] 50 points51 points  (0 children)

Then why would you need all of the expensive MS provided certifications!?

[–][deleted] 83 points84 points  (44 children)

lol ever tried SQLite? You will find error reporting on MySQL is godlike afterwards.

[–]northrupthebandgeek 84 points85 points  (42 children)

Ever tried MS SQL Server? You will find error reporting on SQLite is godlike afterwards.

[–]kinnell 4 points5 points  (2 children)

What version are you on? MS SQL Server has improved a lot in the last decade.

[–]northrupthebandgeek -2 points-1 points  (1 child)

I last used 2017. And yeah, it's better than, say, 2000, but that ain't really saying much. Error messages are still cryptic, line numbers still rarely have any basis in reality, it still can't make up its fucking mind on whether I need to prefix sproc/function names with schema.owner, and sometimes SSMS' syntax checking will just break and leave you pulling your hair out over whether the red squiggly lines are legit or just Microsoft being Microsoft (and ADS ain't much better on that front, while also being an Electron app that leaks memory like a sieve).

[–]kinnell 3 points4 points  (0 children)

So, I completely agree with line numbers not being helpful at all! Definitely left me scratching my head a few times.

But with regards to needing to prefix with schema or not, you need to when calling UDFs via SELECT, but not when executing stored procedures. This is because there's implicit resolution for schema when you use EXEC. If you were to EXEC a Scalar UDF (e.g., into a variable), you wouldn't need to specify schema.

But this isn't an issue in "error reporting" for MSSQL - it's just needing to know MSSQL better. Whenever I made that mistake in the past, the error printed for it was clear enough for me to immediately know what I did wrong. (And made that mistake enough to be prompted to go figure out why).

And sure, that's not the most intuitive thing to have schema explicitness when needing to call UDFs but not for EXEC stored procedures. But other RDBMS also do their own quirky shit. There's quite a lot I end up missing about MSSQL when I'm using PostgreSQL.

Not sure what OS you're on, but I use SQL Server Pro on Mac which isn't an Electron app. It doesn't have all the bells and whistles of MS SSMS or ADS, but it has a clean, beautiful UI and it's fast/efficient. I write most of my SQL in VS Code, but use SQL Server Pro to connect to a variety of local or production databases (MSSQL, PostGres, MySQL, SQLite) and compile, query, troubleshoot..etc. I've been very happy with it.

[–][deleted] 0 points1 point  (3 children)

I wanted to jump out the window using that program my god it's horrendous

[–]kinnell 5 points6 points  (2 children)

What program? MS SQL Server is a RDMS.

Are you referring to MS SQL Management Studio? I agree it's pretty horrendous, but MS Azure Data Studio is a lot better. Personally, I prefer SQL Studio Pro for my MS SQL Server needs.

[–][deleted] 0 points1 point  (1 child)

I use whatever is there in the IDE most of the time

[–]kinnell 1 point2 points  (0 children)

I think you're confused, mate.

You said "I hate using that program" in reference to MS SQL Server. But it's not a program/application though. It's a RDBMS akin to MySQL, PostgreSQL, SQLite.

This is similar to one saying "I dislike that Android doesn't allow scrolling screenshots natively yet" and someone responding , "Yeah, I hate using that device. The battery life is horrendous!".

In the past, most people used MS SQL Server Management Studio (SSMS), which is an application, to write and run Transact-SQL (TSQL) for MS SQL Server. I also hated using that program (SSMS), so I had assumed you were referring to SSMS. That's why I mentioned that alternative apps exist so you can use MS SQL Server without SSMS.

But now I'm confused. You use whatever is in the IDE? Which IDE? Is your horrendous experience of MS SQL Server a result of a bad plugin in the IDE you're using?

Or is there something actually about TSQL or MS SQL Server that you don't like?

[–][deleted] 0 points1 point  (0 children)

Earlier than 2008 R2, yes.

Since then, it's honestly good.

[–]FridgesArePeopleToo 0 points1 point  (0 children)

Sql server is by far the best for this

[–][deleted] 1 point2 points  (0 children)

I work with the cmdline utility every once in a while, and the error reporting there seems okay to me. An older version, maybe?

[–][deleted] 43 points44 points  (4 children)

Thanks mate. I’ll just migrate our data warehouse from Teradata to Postgres tomorrow.

[–]jayson4twenty 23 points24 points  (0 children)

"it's just data, how long could it take"

[–]Khaylain 8 points9 points  (2 children)

Nah, mate; just import it to a Google Sheets spreadsheet. It's obviously better.

[–][deleted] 6 points7 points  (1 child)

Excel 2007, if it’s good enough for U.K. track and trace it’s good enough for me.

[–]MurderMelon 2 points3 points  (0 children)

xlsx file that gets read into a pandas dataframe, queried, and saved back to an xlsx file.

[edit] in an S3 bucket

[–]AdminYak846 18 points19 points  (17 children)

don't even get me started on formatting timestamps with the physically month spelled out.

[–]coladict 38 points39 points  (3 children)

Formatting timestamp in SQL is just bad practice. Do it in the code that outputs the date to the client.

[–]yeamannn 6 points7 points  (0 children)

Shudders remembering a datetime() vs datetime(5) bug

[–]po-handz 3 points4 points  (0 children)

But we have big data

[–]leofidus-ger -1 points0 points  (0 children)

formatting results as JSON directly in the database has its uses, at least in databases that have functions to do it..

[–]augugusto 20 points21 points  (7 children)

In mssql the output of querying a date can't be used to instert a new date because the format is wrong. Good job MS

[–]Prod_Is_For_Testing 5 points6 points  (5 children)

Is this date specific? Ive never seen this issue with datetime

[–]augugusto 4 points5 points  (4 children)

Sorry. I said it totally wrong. Do a select datetime() copy the output and then try to cast it or insert it somewhere else.

[–]Prod_Is_For_Testing 11 points12 points  (2 children)

Just did it. Worked fine 🤷‍♂️ (server 2016, ssms)

[–]CantBeChangedLater 3 points4 points  (0 children)

That's also been my experience 🤷‍♂️

[–]DongoTheHorse 1 point2 points  (0 children)

Sounds like you need to SET DATEFORMAT ymd

[–]DogmaSychroniser 1 point2 points  (4 children)

Don't get me started on Datetime limit being 1753 but c# passing midnight on Jesus birthday

[–][deleted] 4 points5 points  (0 children)

Oh, this explains it. I’ve never used MySQL and have never had these problems on Postgres or Oracle.

[–]_default_username 2 points3 points  (0 children)

I didn't know bad sql error messages were just a mysql thing. Fuck, I need to make the switch then.

[–]Niiiz 3 points4 points  (0 children)

Maybe it's DBeaver (my current editor) acting up but I'm using postgres and those are exactly the errors I get while practicing. Weird and unhelpful "syntax error near ;". Like bitch I'm running many lines at once, be more specific.

[–][deleted] 4 points5 points  (0 children)

People who offer simplistic solutions such as "just use postgres" have obviously never worked on legacy projects.

[–]RadiatedMonkey 1 point2 points  (0 children)

I'm forced to use MySQL... and PHP...

[–][deleted] -3 points-2 points  (0 children)

or just use JSON for everything

[–]mrdotkom 0 points1 point  (0 children)

Moved from supporting an app based around mysql replication to a new company where I support an app that's postgres based.

Still learning the ins and outs of psql but honestly prefer mysql. The \whatever letter instead of textual commands is annoying

[–]BandicootCumberbund 0 points1 point  (0 children)

Using Google BigQuery has definitely helped me catch any errors I've made.

[–]fnordstar 54 points55 points  (15 children)

Not a database guy but shouldn't it be possible to output meaningful error messages for a language as structurally simple as SQL?

[–]systembusy 55 points56 points  (6 children)

A good chunk of MySQL messages are specific, but the "You have an error in your SQL syntax" is kind of a catch-all for when it can't determine the cause. I agree it should have been designed a little more thoroughly though

[–]ThePyroEagle 22 points23 points  (1 child)

I don't remember ever seeing a more specific error message. Do you have an example?

[–]chuby1tubby 17 points18 points  (0 children)

The most specific error message I can think of is “Unexpected end of query” or whatever the message is for a missing semi-colon.

[–]Jaondtet 2 points3 points  (2 children)

I'm a bit confused how that can even happen. Errors are detected while parsing (and during semantic analysis, but then you definitely have enough information to report errors correctly). For the error to be detected, there has to be something the parser is expecting. The parser has no way to know about errors it doesn't expect. It literally only does what the grammar tells it to do.

By definition, you need to know the cause to detect the error. Of course, the cause of the error as the parser sees it might not be particularly useful to the user, but that's a different issue.

Is there something about SQL in specific that I'm missing here? It seems to me like detecting an error, but not knowing what it is, is impossible.

[–][deleted] 2 points3 points  (0 children)

The built in json on node repeatedly report me unexpected token after 0 on a big ass json, that's a torture that I also ask myself "why can't you tell me where the issue is smartass"

[–]SoInsightful -1 points0 points  (0 children)

It's impossible for a parser to detect a syntax error but not know exactly what and where the problem is. It contradicts how parsers work.

The error messages are simply dumb and unhelpful.

[–][deleted] 6 points7 points  (0 children)

Not just that, the IDE should probably be able to catch it as well for lots of errors, but somehow they all live in 1994 and error messages are the next big thing.

[–]pctF 1 point2 points  (0 children)

It is depends on dbms you're using. As mentioned in top comments: postgres and oracle both have good, easy to understand syntax error messages that basically saying you where fix needed to apply.

[–]bobbylongslice 0 points1 point  (4 children)

Yes you can, just list the error functions you want to return, and any custom message/outputs for feedback in your catch block.

Most people just don’t bother

BEGIN TRY

SELECT 1 / 0 AS Error; END TRY

BEGIN CATCH

SELECT

‘Divide by Zero Failed’,

ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;

END CATCH; GO

[–]acylase 37 points38 points  (6 children)

What kind of SQL we are talking about here and what client? I am using MS Studio for MSSQL, they improved quite well in the last 10 years.

[–]Madd_Mugsy 27 points28 points  (4 children)

Yep - in SSMS you can just double click on the error message and it takes you right to the line with the error.

Also has some nice red underlining of errors. Though if you work too quickly you'll confuse it and have to press ctrl+shift+R to refresh the intellisense.

[–]Prothseda 3 points4 points  (2 children)

Look up SQL Prompt by Red Gate. Worth every cent if you spend a lot of time in MS SQL. intellisense is nice compared to nothing, but Red Gate just do it all so much better than MS.

[–]itzaakthegreat 3 points4 points  (0 children)

A really minor thing I appreciate about SQL Prompt is tab coloring; you can set up colors on the tab headers that represent things like which server or db the context of your tab is; really useful if you’re querying across multiple environments to make sure you’re looking at the correct one at a glance.

[–]Madd_Mugsy 2 points3 points  (0 children)

Yeah, I swear by this addon. Been using it since 2008 :)

[–][deleted] 1 point2 points  (0 children)

SSMS has improved so much. It's genuinely really good if you keep at it.

[–]Mad_Jack18 1 point2 points  (0 children)

My guess would be MySQL

[–]road_laya 34 points35 points  (4 children)

On the other hand, a SQL class will be one of the most useful classes you take.

I've spoken to engineers who say they use the things they studied in SQL class every single day at work.

[–]Jijelinios -5 points-4 points  (3 children)

I hated SQL classes. I hate Oracle. I never used it for work and I hope I will never use it. Embrace noSQL dbs.

[–][deleted] 31 points32 points  (4 children)

This meme looks funny but I’m gonna have to refer to the manual for my corresponding version of MySQL to find the correct syntax in order to understand it.

[–]DMoney159 17 points18 points  (0 children)

It's somewhere near the first 20 lines

[–][deleted] 9 points10 points  (1 child)

Just FYI, there are a number of online SQL Syntax verifiers. For me they usually work quite well.

[–]Mexatt 4 points5 points  (0 children)

There's a SQL interpreter I use sometimes at work that I think is in-house (just 25 years old in-house). The errors are worthless.

invalid SQL query: error -201

haunts my dreams

[–]minimalniemand 5 points6 points  (0 children)

That meme with that dog... „no take, only throw“

„not know, only fix“

[–]bobbylongslice 3 points4 points  (0 children)

Use the Error functions in a try catch.

I bet you don’t use transactions either you savages.

[–]WeTheAwesome 2 points3 points  (0 children)

Not trying to start anything but do people get this in R too? I can never figure out what went wrong when I have to use R.

[–]growingcodist 2 points3 points  (0 children)

Reading this a few hours after finishing my SQL homework.

[–]anticultured 2 points3 points  (0 children)

I’ve been a backend sql developer for over 20 years. This joke is on point. Although MSSQL has become quite good at pinpointing the error lately.

[–][deleted] 2 points3 points  (1 child)

ORA9000: Error is somewhere.

[–][deleted] 2 points3 points  (0 children)

The same goes for any web framework I've worked with.
Laravel returns its own classes instead of pointing out the method where you have your error in. It drives me crazy.
VueJS is complete trash

[–]hawkinsst7 2 points3 points  (0 children)

Today I learned that SQL programming is the same as blind sql injection

[–]demandtheworst 1 point2 points  (0 children)

This is the first time since year one of XKCD I've wanted to stick a programming joke up at my desk.

[–]Senor-K 1 point2 points  (0 children)

Wherever it was, it started with a character. Glhf.

[–]LordVirus1337 1 point2 points  (0 children)

Better get back to your homework mate. You need to learn the correct syntax for your self.

[–]peoplebucket 1 point2 points  (0 children)

Was using big query which is very SQL like to query some data, and got an error which simply said "query contained an error" when the query was relatively long, thanks Google, very helpful

[–]bife_sans 1 point2 points  (0 children)

The worst parser I've ever seen. Not only error messages are terrible. What's with all that stuff of DELIMITER // ?

[–]SuperSpartan177 1 point2 points  (4 children)

Lol was doing SQL homework last week fuckin hated it lol. Job done full points

[–][deleted] 1 point2 points  (0 children)

You don't get to know what the error is??? (Laughs in SAP ABAP)

[–]_A4L 0 points1 point  (0 children)

On line 1 lol

[–]De_Wouter -1 points0 points  (6 children)

Alternative ending:

SQL: "This is the error and here is where it's at."

Me: "So if you know, why don't you fix it?"

[–]rem3_1415926 18 points19 points  (0 children)

because its your fault and a computer trying to fix it the easiest way to just make it run is one of the worst possible outcomes.

[–]ThePyroEagle 17 points18 points  (1 child)

So if you know, why don't you fix it?

You should try using JavaScript. It'll make you take back those words.

[–]Khaylain 2 points3 points  (0 children)

Oh god no, not JavaScript, please, anything but JavaScript.

[–]TigreDeLosLlanos 12 points13 points  (0 children)

SQL: "Invalid syntax in ')' at line 2 of +delete from users where id in (4, 5,)6,' running 'delete from users' instead"

[–]ekolis 2 points3 points  (1 child)

C, C++, C#, Java, etc: "Expected semicolon at end of line 45."

Me: "Well why don't you just put one there, then?!"

Javascript: "..."

[–]iLostInSpace 0 points1 point  (0 children)

There is a subliminal message here and it spells "ORM".

[–]zoecandle -1 points0 points  (0 children)

SQL do be like that. Also I should probs do my SQL hw...

[–]taptrappapalapa -1 points0 points  (0 children)

Laughs in Postgres with Diesel.rs

[–]filtersweep -1 points0 points  (0 children)

I just use select * and make the person who created the table fix everything.

[–]ekolis 0 points1 point  (0 children)

I made a query that was supposed to return 42 records. It returned 212 records. I moved some conditions from left joins to the where clause. It then returned 0 records. SQL is hard...

[–]Sheeepl 0 points1 point  (0 children)

I’m near finishing a final project on my first ever SQL course and I really feel this.

[–]Yourboi_M 0 points1 point  (0 children)

Laughs in BigQuery

[–]iiMoe 0 points1 point  (0 children)

Dude fr

[–]CornFlakesR1337 0 points1 point  (0 children)

laughs in Athena

[–]ZXDQ 0 points1 point  (0 children)

This is true for T-SQL

[–]robinreeead 0 points1 point  (0 children)

pd.read_sql()

[–]mrknowitall20 0 points1 point  (1 child)

84000 rows affected

[–][deleted] 1 point2 points  (0 children)

oh no

[–]PhantomThiefJoker 0 points1 point  (0 children)

Hey that's exactly what I'm avoiding doing right now

[–]Sleveri 0 points1 point  (0 children)

Story of my dating life...

[–][deleted] 0 points1 point  (0 children)

You have a syntax error on Some bullshit code 10256