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

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?
Microsoft Dynamics AX 2012 R3
Microsoft SQL Server 2012
P.S.: It can not be used without use DBNAME; because
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 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'
Axapta is using Microsoft SQL Server also so the
use DBNAME;
must break some inner settings.

Related

INSERT failure with SQLAlchemy ORM / SQL Server 2017 - Invalid Object Name

Firstly, I have seen many answers which is specific to the Invalid Object Name error working with SQL Server, but None of them seem to solve my problem. I don't have much idea on SQL Server dialect, but here is my current setup required on the project.
SQL Server 2017
SQLAlchemy (pyodbc+mssql)
Python 3.9
I'm trying to insert a database row, using the SQLAlchemy ORM, but it fails to resolve the schema and table, giving me the error of type
(pyodbc.ProgrammingError) ('42S02', "[42S02] [Microsoft][ODBC Driver
17 for SQL Server][SQL Server]Invalid object name 'agent.dbo.tasks'.
(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Statement(s) could not be prepared. (8180)")
I'm creating a session with the following code.
engine = create_engine(connection_string, pool_size=10, max_overflow=5,
pool_use_lifo=False, pool_pre_ping=True, echo=True, echo_pool=True)
db_session_manager = sessionmaker()
db_session_manager.configure(bind=engine)
session = db_session_manager()
I have a task object defined like
class Task(BaseModel):
__tablename__ = "tasks"
__table_args__ = {"schema": "agent.dbo"}
# .. field defs
I'm trying to fill the object fields and then do the insert like the usual
task = Task()
task.tid = 1
...
session.add(task)
session.commit()
But this fails, with the error mentioned before. I tried to execute a direct query like
session.execute("SELECT * FROM agent.dbo.tasks")
And it returned a result set.
The connection string is a URL object, which prints like
mssql+pyodbc://task-user:******#CRD4E0050L/agent?driver=ODBC+Driver+17+for+SQL+Server
I tried to use the SQL Server Management Studio to insert manually and check, there it shown me a sql dialect with [] as field separators
like
INSERT INTO [dbo].[tasks]
([tid]..
, but SQLAlchemy on echo did not show that, instead it used the one I see in MySQL like
INSERT INTO agent.dbo.tasks (onbase_handle_id,..
What is that I'm doing wrong ? I thought SQLAlchemy if configured with a supported dialect, should work fine (I use it on MySQL quite well). Am I missing any configuration ? Any help appreciated.

SQL to SAS ODBC connection - truncation of NVARCHAR(max) but getting ERROR: The option dbtype is not a valid output data set option

I have a table stored in SQL Server Management Studio version 15.0.18369.0 and I have a working and established ODBC connection to SAS language program World Programming Software version 3.4.
Previous import/reading of this data has been successful but the operator of the SQL server may have recently converted their data type to NVARCHAR(max). It may also be due to a driver change (I got a new laptop and reinstalled what I thought was the exact same OBDC for SQl driver as I had before but who knows) I have 64-bit ODBC Driver 17 for SQL server.
The VARCHAR(max) type in SQL causes the data to only be 1 character long in every column.
I have tried to fix it by:
Adding the DB max text option to libname
libname odbclib odbc dsn="xxx" user="xxx" password="xxx" DBMAX_TEXT=8000;
This did nothing so I also tried to add DB type option:
data mydata (dbtype=(mycol='char(25)')) ;
set odbclib.'sql data'n
run;
And I get ERROR:
The option dbtype is not a valid output data set option.
I have also tried DBSASTYPE, and putting both options in the set statement and this yields the same error.
I also tried with proc SQL:
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract(compress=no dbsastype=(mycol='CHAR(20)')) as select * from
connection to odbc ( select * from dbo.'sql data'n
); disconnect from odbc; quit;
And I get
NOTE: Connected to DB: BB64 (Microsoft SQL Server version 12.00.2148)
NOTE: Successfully connected to database ODBC as alias ODBC. 3915
create table extract(compress=no
dbsastype=(mycol='CHAR(20)')) as 3916 select *
from connection to odbc 3917 ( 3918 select *
from dbo.'sql data'n ERROR: The option dbsastype is not a valid
output data set option 3919 3920 ); 3921
disconnect from odbc; NOTE: ERRORSTOP was specified. The statement
will not be executed due to an earlier error NOTE: Statements not
executed because of errors detected 3922 quit;
One thing to try would be putting the DBTYPE or DBSASTYPE in the right place.
data mydata;
set odbclib.'sql data'n(dbtype=(mycol='char(25)'));
run;
It is an option that would go on the "set" not the "data" line.
I would also encourage you to contact the developers, as it seems like this is a bug in their implementation; or perhaps try a different ODBC SQL driver.
Maybe a workaround in the meanwhile is to use CONVERT in the passthrough?
proc sql noprint;
CONNECT TO ODBC(dsn="xxx" user="xxx" password="xxx"); create table
extract as select * from
connection to odbc ( select a,b,c,convert(NCHAR(20),mycol) from dbo.'sql data'n
); disconnect from odbc; quit;

Incorrect syntax near Go with Pypyodbc

I am using the pypyodbc library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
This is the python script that I am using:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
The workaround for this problem is to delete the Use SL_Site1_App Go line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.
GO is a batch separator used by sqlcmd and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App", and then remove the USE and GO statements in your SQL Statement.

Query join with SSIS parameter

It is possible to use parameter (SSIS variable) after left join in query in OLE DB Source? The query looks like:
...
FROM X
LEFT JOIN ?
...
where "?" is string variable, which I dynamically changing (based on loop iteration).
When I put this string, except "?" - it works. When I deleted it and parametrized I got:
TITLE: Microsoft Visual Studio
Parameters cannot be extracted from the SQL command. The provider
might not help to parse parameter information from the command. In
that case, use the "SQL command from variable" access mode, in which
the entire SQL command is stored in a variable.
------------------------------ ADDITIONAL INFORMATION:
Syntax error, permission violation, or other nonspecific error
(Microsoft SQL Server Native Client 10.0)
------------------------------ BUTTONS:
OK
I use MS 2008R2.

Parameter type cannot be determined for at least one variant parameter

I'm tring to execute stored procedure placed in SQL Database (Azure) via linked server. Local SQL Server version - 2012 (11.0.3128.0). When sp doesn't have parameters everthing is fine. With parameter I receive error:
DECLARE #p1 int
execute sever_name.db_name.[dbo].[sp1] #p1=1
OLE DB provider "MSDASQL" for linked server "sever_name" returned message "Parameter type cannot be determined for at least one variant parameter.".
Msg 7212, Level 17, State 1, Line 29
Could not execute procedure 'sp1' on remote server 'sever_name'.
rpc=true
I had a similar problem with a linked server call from 2014 over to a 2000 server today. The fix? Don't name the parameter:
execute sever_name.db_name.[dbo].[sp1] 1

Resources