Pyodbc - Inserting values into SQL Server db error - sql-server

I am trying to use python to insert values into a table.
TableName = "Table_Test1"
This is the code that I am trying:
cursor.execute("Insert into " + TableName + " (TestCol1,TestCol2) VALUES (?,?)", Val1, Val2)
What is the mistake that I am making here?
It is giving me this error - <class 'pyodbc.ProgrammingError'> returned a result with an error set

TestCol2 in the db was declared as nvarchar and the Val2 that the program returned was 0 which is an integer. I changed the datatype in the db and then the program started working.

Related

Oracle Select query doesnt return any results when using trim on char(2) column and database field has only spaces

Oracle Select query is not returning any results when using trim on char(2) column. The database field has only spaces. However, when the field has a space and a character, I get the correct results.
e.g.
Action_code char(2)
val = " 8"
Select * from abc where trim(Action_code) = trim(val)
Here I get the rows having " 8".
But,
val = " "
Select * from abc where trim(Action_code) = trim(val)
Here I don't get any rows from database where Action_code is " ". But I have such rows in database.
Can someone please help me how to get rows in 2nd case?
I just found that when you trim a variable or database column which has only spaces, it is treated as null by Oracle. So the following query works:
val = " "
Select * from abc where trim(Action_code) is null and trim(val) is null
Now I get the rows from database where Action_code is " ".
Why you don't try to use the nvl function ?
nvl(valueIsNull,returnOtherValue)
?

PreparedStatement not populating

I have PreparedStatement(ps) below, populate it as shown and when executed MSSQL errors as shown last below. Of course there is NO value of "TRADEIN" in any data that can be seen. Ideas?
String update = "UPDATE IM_ITEM SET "
+ "LST_COST=?, PRC_1=?, IS_TXBL=?, TAX_CATEG_COD=?,CATEG_COD=?"
+ " WHERE ITEM_NO="+itemNo;
populate:
ps.setString(++i, q.get("LST_COST"));
ps.setString(++i, q.get("PRC_1"));
ps.setString(++i, q.get("IS_TXBL"));
ps.setString(++i, q.get("TAX_CATEG_COD"));
ps.setString(++i, q.get("CATEG_COD"));
debugged: "sqlCommand" and "userSQL" are still parameterized only
UPDATE IM_ITEM SET LST_COST=?, PRC_1=?, IS_TXBL=?, TAX_CATEG_COD=?, CATEG_COD=? WHERE ITEM_NO=101316
error:
Severe: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'TRADEIN' to data type int.
Answered, kindly, by Seanlange in the chat.

sqlQuery failing with memory error when row_at_time too large

I am sending this query to a sql server in R using RODBC::sqlQuery
MERGE "mytable" AS Target USING ( VALUES ('myname','POLYGON ((148.0000000000000000 -20.0000000000000000, 148.0000000000000000 -20.0000000000000000, 148.0000000000000000 -20.0000000000000000, 148.0000000000000000 -20.0000000000000000, 148.0000000000000000 -20.0000000000000000))')) AS Source ("name","polygon")
ON (Target."name" = Source."name")
WHEN MATCHED THEN
UPDATE SET Target."polygon" = Source."polygon"
WHEN NOT MATCHED BY TARGET THEN
INSERT ("name","polygon")
VALUES (Source."name", Source."polygon")
OUTPUT $action, Inserted.*, Deleted.*
It fails when row_at_time argument of sqlQuery is more than 10,
Error in odbcQuery(channel, query, rows_at_time) :
'Calloc' could not allocate memory (107374182400 of 1 bytes)
but works if row_at_time < 10. (still the query takes quite a few seconds which is surprising as the table is indexed and very small: less than 100 rows)
Any idea why?
Thank you
EDIT: This is the structure of the table I am writing on:

An invalid floating point operation occurred POWER SQL Server

I am have a problem with the code, when I run I get
An invalid floating point operation occurred
My code is the following:
select POWER( ISNULL(value1,0) / NULLIF(value2,0) , 12 / CONVERT(numeric(6,2),value3))
where value1(data type: money), value2(data type: money), value3(data type: int) are columns in my database.
I get that error when value1 = Null, value2 = 164083520.00 and value3 = 177
The weird is that if I replace with these numbers I get right result, but not while running this.
Any ideas?
Executing the following script I'm not getting any errors on SQL Server 2012. AFAICT you won't get any on other versions either. You must be having a calculation somewhere else that results in an invalid floating point operation.
DECLARE #values TABLE(value1 MONEY,value2 MONEY,value3 INT);
INSERT INTO #values(value1,value2,value3)VALUES(NULL,164083520.00,177);
SELECT
result=POWER(ISNULL(value1,0)/NULLIF(value2,0), 12.0/CONVERT(numeric(6,2),value3))
FROM
#values;
Result
0.00

Sybase BulkCopy WriteToServer error: Incorrect syntax near ','

Is it possible to populate a temp table using AseBulkCopy.WriteToServer?
I am calling the below method twice in my test app: firstly with a non-temp table and secondly with a temp table. The code runs fine with the non-temp table, but when trying to populate a temp table the error:
Incorrect syntax near ','.
is raised.
In both cases the target and source tables have just a single column, defined as an INT with the same name.
I have tried using a DataTable and an IDataReader as the source of the data and both result in the same error being raised.
I have tried using both "EnableBulkLoad=1" and "EnableBulkLoad=2" in the connection string.
I have tried using both the raw temp table name and the name prefixed with "dbo."
The data to be loaded is a single int value (ie, 1 row, 1 column) although it also happens if have longer rows or multiple rows.
It's worth noting that I can insert data into the temp table (using AseCommand.ExecuteNonQuery) and can execute a 'SELECT COUNT (1)' from the temp table (using AseCommand.ExecuteScalar) successfully.
Here is the code:
private static void BulkCopyInsertIntoTable(string tableName)
{
IDataReader dataSource = null;
SqlConnection sourceConnection = null;
MssqlCommand.GetDataReader(SqlServerConnectionString, out sourceConnection, out dataSource);
AseConnection targetConnection = new AseConnection(SybaseConnectionString);
try
{
targetConnection.Open();
AseCommand cmd = new AseCommand();
AseBulkCopy blk = new AseBulkCopy(targetConnection);
blk.DestinationTableName = tableName;
//blk.ColumnMappings.Clear();
//blk.ColumnMappings.Add(new AseBulkCopyColumnMapping(0, 0));//Doesn't make any difference with a datasource, causes an error to be raised with a datatable.
Console.WriteLine("bulkcopy insert into the table " + tableName + " ..starting: datasource");
//blk.WriteToServer(dataSource);
Console.WriteLine("bulkcopy insert into the table " + tableName + " ..starting: datatable");
blk.ColumnMappings.Clear();
DataTable dt = SybaseCommand.GetFakeDataTable(); ;
blk.WriteToServer(dt);
}
catch (AseException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
targetConnection.Dispose();
Console.WriteLine("bulkcopy insert into the table " + tableName + " ..ended");
}
}
Firstly, is it possible to populate a temp table using WriteToServer?
Assuming it is, what might I being doing wrong?
UPDATE:
When I change the line
blk.DestinationTableName = tableName;
to
blk.DestinationTableName = "XXXX";
I get the same error, so are there rules about how the temp table is named when using WriteToServer? The value of tableName is what I was using for the direct INSERT and SELECT COUNT(1) queries so I expected it to be correct.
Thanks
In my experience, the answer is no, you can't use AseBulkCopy.WriteToServer to populate a temporary table.

Resources