What is STATISTICS TIME in SQL Server?

In SQL Server, you can use the SET STATISTICS TIME statement to display the time it takes to execute a T-SQL statement.

More specifically, it returns the number of milliseconds required to parse, compile, and execute each statement.

When SET STATISTICS TIME is ON, the time statistics for a statement are displayed. When OFF, the time statistics are not displayed.

The setting of SET STATISTICS TIME is set at execute or run time and not at parse time.

Continue reading

SQL Server Error 113: Missing end comment mark ‘*/’

SQL Server error message 113 occurs when you omit a closing comment mark.

This can occur when you open a comment but forget to close it. It can also occur when you accidentally type an opening comment.

There may also be odd occasions where you get this error due to other factors, such as the way your SQL utility handles certain keywords etc.

Continue reading

What is STATISTICS PROFILE in SQL Server?

In SQL Server, you can use the SET STATISTICS PROFILE statement to display the profile information for a T-SQL statement.

STATISTICS PROFILE works for ad hoc queries, views, and stored procedures.

When STATISTICS PROFILE is set to ON, each executed query returns its regular result set, followed by an additional result set that shows a profile of the query execution.

Continue reading

How to Return Duplicate Keys from a JSON Document in SQL Server

If you’re trying to extract values from a JSON document, but one or more of the values on the same level have duplicate keys, you might run into problems if you try to extract those values using JSON_QUERY() or JSON_VALUE().

Both of those functions will only return the first value that matches the path. 

Fortunately, there’s another option.

The OPENJSON() function will return all values from any duplicate keys on the same level.

Continue reading

OPENJSON “Incorrect syntax near the keyword ‘with’.” in SQL Server (SOLVED)

If you’re trying to run some OPENJSON() code in SQL Server, but you’re getting error Msg 319, Level 15 “Incorrect syntax near the keyword ‘with'”, one possibility is that you really do have a syntax error.

But if you’ve check and double checked, and you’re convinced there’s no syntax error, it could actually be that the error is a side effect of having the wrong database compatibility level.

Normally you’d get error Msg 208, Level 16 “Invalid object name ‘OPENJSON’.” when using a database compatibility level of lower than 130, but in some cases, SQL Server finds a problem with the WITH clause first.

Continue reading