Why calling stored procedure in SQL server from x++ break AX?

I have a script for renaming column in database written in X++.

public static server void renameColumn()

{

str sql;

Connection oConnection;

Statement oStatement;

ResultSet resSet;

SqlStatementExecutePermission perm;

sql = strFmt( ‘use DBNAME; exec sp_rename ‘DBNAME.dbo.Table1.Name2’, ‘Name1’, ‘COLUMN’’);

oConnection = new Connection();

oStatement = oConnection.createStatement();

perm = new SqlStatementExecutePermission(sql);

perm.assert();

oStatement.executeUpdate(sql);

}

Which change existing column named Name2 to Name1.

The strange errors start to occures few minutes later after I call it (using job).

Cannot select a record in Current AOS instances (SysServerSessions). ServerId: 0.

The SQL database has issued an error.

SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name ‘SYSSERVERSESSIONS’.

SQL statement: SELECT T1.SERVERID,T1.AOSID,T1.INSTANCE_NAME,T1.VERSION,T1.LOGINDATETIME,T1.LOGINDATETIMETZID,T1.STATUS,T1.LOADBALANCE,T1.WORKLOAD,T1.LASTUPDATEDATETIME,T1.LASTUPDATEDATETIMETZID,T1.AOSACCOUNT,T1.RECVERSION,T1.RECID,T2.SESSIONID,T2.SERVERID,T2.VERSION,T2.LOGINDATETIME,T2.LOGINDATETIMETZID,T2.STATUS,T2.USERID,T2.SID,T2.USERLANGUAGE,T2.HELPLANGUAGE,T2.CLIENTTYPE,T2.SESSIONTYPE,T2.CLIENTCOMPUTER,T2.DATAPARTITION,T2.RECVERSION,T2.RECID FROM SYSSERVERSESSIONS T1 CROSS JOIN SYSCLIENTSESSIONS T2 WHERE ((T2.SERVERID=T1.SERVERID) AND (T2.SESSIONID=?))

Cannot select a record in Alerts - event inbox (EventInbox).

The SQL database has issued an error.

SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name ‘EVENTINBOX’.

SQL statement: SELECT COUNT(T1.RECID) FROM EVENTINBOX T1 WHERE ((PARTITION=?) AND ((((ISREAD=?) AND (USERID=?)) AND (DELETED=?)) AND (VISIBLE=?)))

Anyone have an idea how to repair it and what exactly cause it?

I suspect that AX now use the context of DBNAME database that you set by USE statement. Can you verify that?

Without use DBNAME; the code simple crash with

SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

I believe I can verify it.

It is possible that this kind of errors starts only if some information is wrong. Like attempt to rename field that do not exist.

I find a way that do not take AOS with it when crash. I am calling stored procedure without using "use DBNAME; " Instead of that I am using full name of stored procedure by “[DBNAME].[dbo].[sp_rename]”

And when this crashed I get a normal SQL error:

Cannot execute the required database operation.
The SQL database has issued an error.
SQL error description: [Microsoft][SQL Server Native Client 10.0][SQL Server]No item by the name of ‘DBNAME.dbo.Table1.Name1’ could be found in the current database ‘DBNAME’, given that @itemtype was input as ‘(null)’.
SQL statement: exec [VABO].[dbo].[sp_rename] ‘DBNAME.dbo.Table1.Name1’, ‘Name2’