1

when I execute attached query I get error:

Msg 0, Level 11, State 0, Line 0 A severe error occurred on the current command. The results, if any, should be discarded.

When try-catch is commented then it works, same when other column is used. When partition by is removed it also works.

Can someone say where is the root of error here or is it some sql server bug?

Query:

BEGIN TRY 
    DECLARE  @vSqlCmd NVARCHAR(MAX) = N'
    SELECT TOP 1 V = d.collation_name, C = COUNT(1) OVER (PARTITION BY d.collation_name)
    FROM sys.all_columns d;';
    SELECT @vSqlCmd;

    EXECUTE (@vSqlCmd);
END TRY
BEGIN CATCH
    PRINT 'CATCH!!!';

    THROW;
END CATCH
5
  • This sounds more like a problem on the server, not the SQL. What version of SQL Server (include the build number). How often do you check for things like corruption in the database?
    – Thom A
    Feb 18, 2020 at 16:16
  • 1
    Why use a "dynamic" statement here though? There's nothing dynamic about it.
    – Thom A
    Feb 18, 2020 at 16:17
  • This is a bug. Please post a feedback item here: feedback.azure.com/forums/908035-sql-server Feb 18, 2020 at 16:20
  • @Larnu - this isn't database corruption. It is easily reproducible in at least 2017 or up (not sure the exact version the problematic PDW rows were addded) Feb 19, 2020 at 8:04
  • Thank You for all input! I tested this sample at 3 instances - min SQL 2012 (13.0.5366) - all gave same error. Purpose of this part of query is negligible, it is prepared just for this post from bigger solution, but thx for byway and little rageposting ;-).
    – skk
    Feb 20, 2020 at 15:15

1 Answer 1

1

This is a bug. I have reported it here

It is not clear why you need this query, the following explains the issue and how to avoid it if there is some reasonable reason for needing this query.

The following query returns two object_ids -103085222 and -593

SELECT object_id ,name
FROM sys.system_columns
where system_type_id in (35,99,167,175,231,239) and collation_name IS NULL

These relate to the objects below

+------------+----------------------------------+
|     id     |               name               |
+------------+----------------------------------+
| -103085222 | pdw_nodes_pdw_physical_databases |
|       -593 | pdw_table_mappings               |
+------------+----------------------------------+

For some reason both of these have a string column (called physical_name) that SQL Server can not resolve the collation for. As these objects are only relevant to Parallel Data Warehouse and don't even properly exist in other versions of the product this is not usually a problem.

sys.all_columns references the sys.system_columns view which uses the following expression for collation_name : convert(sysname, ColumnPropertyEx(object_id, name, 'collation'))

A simpler case that reproduces the issue is

BEGIN TRY
    SELECT columnpropertyex(-593, 'physical_name', 'collation')
END TRY
BEGIN CATCH
END CATCH 

Or

SET XACT_ABORT ON;
SELECT columnpropertyex(-593, 'physical_name', 'collation')

Both return

Msg 0, Level 11, State 0, Line 33

A severe error occurred on the current command. The results, if any, should be discarded.

When run outside the TRY block this returns NULL (when XACT_ABORT is off). Behind the scenes it throws an exception that is handled internally and the end user is unaware. The call stack when the exception is thrown shows that GetColumnCollate function ends up using the Algebrizer to try and resolve the column and it eventually fails in CAlgTableMetadata::RaiseBadTableException (possibly as a related object definition is missing somewhere).

enter image description here

When run in the TRY ... CATCH context or with XACT_ABORT ON something goes wrong with the bit that is supposed to silently ignore the error and return NULL. Inserting a duplicate key into an index with ignore_dup_key ON also raises an internal error that is ignored but does not have the same problem.

So one way of resolving the issue is to wrap the reference to collation_name in a CASE expression so it is never evaluated for those two problematic object_id when run inside the TRY block.

  BEGIN TRY 
    DECLARE  @vSqlCmd NVARCHAR(MAX) = N'
    SELECT TOP 1 V = ca.safe_collation_name, C = COUNT(1) OVER (PARTITION BY ca.safe_collation_name)
    FROM sys.all_columns d
    CROSS APPLY (SELECT CASE WHEN object_id NOT IN (-593,-103085222) THEN collation_name END) ca(safe_collation_name);
    ';
    SELECT @vSqlCmd;

    EXECUTE (@vSqlCmd);
END TRY
BEGIN CATCH
    PRINT 'CATCH!!!';

    THROW;
END CATCH

This does not protect you against different cases of problematic metadata being added to the product in the future. Again I question the need for this query however. The query you have provided is so odd that it is difficult to advise you what you should replace it with.

1
  • I suspect the issue is either a missing or incorrect collation_id value in the hidden [mssqlsystemresource] database for that particular column of those two specific objects (i.e. IDs -593 and -103085222). (I added this and some extra info for reference to that feedback item). Apr 22, 2020 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.