Following Sql Query is throwing an ODBCException:
cmd.CommandText = "Select Max(" + primaryKey + ") from " + tableName + " where " + primary_key + " >= " + strt_rng[ftr_index] + " and " + primary_key + " < " + end_rng;
Actual Query is as follows:
SELECT MAX(FtreId) FROM tbmftre WHERE FtreId >=101 AND FtreId < 1000
Am i missing quotes in above query?
Related
The following works fine in my C# program:
string dbSQL = "SELECT stationID, stationLatitude, stationLongitude FROM stations_noaa" +
" WHERE stationLatitude BETWEEN " + LatLow + " AND " + LatHi +
" INTERSECT " +
"SELECT stationID, stationLatitude, stationLongitude FROM stations_noaa" +
" WHERE stationLongitude BETWEEN " + LongLow + " AND " + LongHi;
I did invest quite a bit of time trying to get the multiple between criteria in one SELECT statement before settling on this. Is it possible?
I am executing a SQL query from C# but get the error:
MySql.Data.MySqlClient.MySqlException: 'The used command is not allowed with this MariaDB version'
Any help / pointers would be appreciated.
Here is my code:
private bool SaveCsvLines(string strFileName)
{
bool bResult = false;
if (ConnectToDatabase())
{
// Here we load the string variable with the SQL we want to run
string strLineInsert = "LOAD DATA LOCAL INFILE " +
"'" + strFileName + "' " +
"INTO TABLE pfetest1.maerskInvoiceLine " +
"FIELDS TERMINATED BY ',' " +
"LINES TERMINATED BY '\r\n' " +
"IGNORE 1 LINES " +
"(container, " +
"size, " +
"vessel, " +
"portOfLoad, " +
"porCityCode, " +
"portOfDischarge, " +
"podCityCode, " +
"postCode, " +
"invoiceNo, " +
"SET createdTimestamp = NOW(), " +
"createdUserId = 1, " +
"updatedTimestamp = NOW(), " +
"updatedUserId = 1, " +
"headerId = " + m_iFileId + ", " +
"eta = STR_TO_DATE(#eta, '%d/%m/%Y')";
//Now we execute that statement
MySqlCommand command = new MySqlCommand(strLineInsert, con);
bResult = (command.ExecuteNonQuery() == 1);
}
return bResult;
}
Switched to using MySQLBulkloader. That way I can define the columns etc. first then pump the data in.
I am trying to create a list of members only if they are not in another list. When the other list has at least one member then the query works (member not in the other list are returned). However, when the other list contains no members (returns NULL) then no values are returned.
String selectQry = ("SELECT at_cub_details.cd_id, at_cub_details.cd_surname, at_cub_details.cd_first_name, " +
"at_cub_details.cd_archived " +
"FROM at_cub_details, at_account_group " +
"WHERE at_account_group.acc_id = ? AND at_account_group.grp_id = at_cub_details.grp_id " +
"AND ( " +
" SELECT at_cub_details.cd_id " +
"FROM at_group, at_account_group group1, at_account_group group2, at_accounts " +
"LEFT JOIN at_account_cub_association ON at_accounts.acc_id = at_account_cub_association.acc_id " +
"LEFT JOIN at_cub_details ON at_account_cub_association.cd_id = at_cub_details.cd_id " +
"WHERE (at_accounts.acc_id = ? " +
"AND (group1.acc_id = ? " +
"AND group1.grp_id = group2.grp_id " +
"AND group2.acc_id = at_accounts.acc_id)) " +
"AND (group2.grp_id = at_group.grp_id) LIMIT 1) IS NOT NULL " +
"AND at_cub_details.cd_id NOT IN ( " +
" SELECT at_cub_details.cd_id " +
"FROM at_group, at_account_group group1, at_account_group group2, at_accounts " +
"LEFT JOIN at_account_cub_association ON at_accounts.acc_id = at_account_cub_association.acc_id " +
"LEFT JOIN at_cub_details ON at_account_cub_association.cd_id = at_cub_details.cd_id " +
"WHERE (at_accounts.acc_id = ? " +
"AND (group1.acc_id = ? " +
"AND group1.grp_id = group2.grp_id " +
"AND group2.acc_id = at_accounts.acc_id)) " +
"AND (group2.grp_id = at_group.grp_id)) " +
"ORDER BY at_cub_details.cd_surname, at_cub_details.cd_first_name;");
OK, to simplify the second and third "SELECT" works the first "SELECT" only works when the third "SELECT" returns a value not when it returns "NULL". Initially I only had the third "SELECT". I then added the second "SELECT" to check for "NULL" first. However, that did not correct the issue. So:
This works:
String selectQry = ("SELECT at_cub_details.cd_id, at_cub_details.cd_surname, at_cub_details.cd_first_name, " +
"at_cub_details.cd_archived " +
"FROM at_cub_details, at_account_group " +
"WHERE at_account_group.acc_id = ? AND at_account_group.grp_id = at_cub_details.grp_id " +
"AND at_cub_details.cd_id NOT IN ( '20' ) " +
"ORDER BY at_cub_details.cd_surname, at_cub_details.cd_first_name;");
This does not work:
String selectQry = ("SELECT at_cub_details.cd_id, at_cub_details.cd_surname, at_cub_details.cd_first_name, " +
"at_cub_details.cd_archived " +
"FROM at_cub_details, at_account_group " +
"WHERE at_account_group.acc_id = ? AND at_account_group.grp_id = at_cub_details.grp_id " +
"AND at_cub_details.cd_id NOT IN ( NULL ) " +
"ORDER BY at_cub_details.cd_surname, at_cub_details.cd_first_name;");
As to whether you down vote me or not, I do not really care as I am doing this not for profit and learning as I go (I am not a programmer).
The people at CodeRanch have been very helpful. The answer is to use "IFNULL" to return 0 if null instead of checking for NULL.
String selectQry = ("SELECT at_cub_details.cd_id, at_cub_details.cd_surname, at_cub_details.cd_first_name, " +
"at_cub_details.cd_archived " +
"FROM at_cub_details, at_account_group " +
"WHERE at_account_group.acc_id = ? AND at_account_group.grp_id = at_cub_details.grp_id " +
"AND at_cub_details.cd_id NOT IN ( " +
"SELECT IFNULL (at_cub_details.cd_id, 0) " +
"FROM at_group, at_account_group group1, at_account_group group2, at_accounts " +
"LEFT JOIN at_account_cub_association ON at_accounts.acc_id = at_account_cub_association.acc_id " +
"LEFT JOIN at_cub_details ON at_account_cub_association.cd_id = at_cub_details.cd_id " +
"WHERE (at_accounts.acc_id = ? " +
"AND (group1.acc_id = ? " +
"AND group1.grp_id = group2.grp_id " +
"AND group2.acc_id = at_accounts.acc_id)) " +
"AND (group2.grp_id = at_group.grp_id)) " +
"ORDER BY at_cub_details.cd_surname, at_cub_details.cd_first_name;");
Kind regards,
Glyn
My problem is: if I change the query string below to select * from table;, it works fine. but if I execute the query string below, it throws an error:
SQL error: Invalid column number. Cannot less than or equal zero.
This is the code:
columnHeader = " 'id', 'pat_name', 'pat_age' ";
var filename = $('serType') + $('serId') + ".csv";
var reporting_exportQuery = "select " + columnHeader +
" union all " +
"select * " +
"from " + $('serType') +
" into outfile 'C://mytest/reports/service reports/"+$('serType') + "/" + "" + filename + "' " +
"fields terminated by ',' " +
"lines terminated by '\\n';";
var test = "select * from Age;";
reporting_DBConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver',:D);
logger.info(reporting_exportQuery);
var result = reporting_DBConn.executeCachedQuery(reporting_exportQuery);
You are adding(union) one column columnHeader with all columns (*) from table. You can do like this
SELECT 'columnHeader',table_alias.* FROM TABLE table_alias...
var filename = $('serType') + $('serId') + ".csv";
var reporting_exportQuery =
"select 'columnHeader',table_alias.* " +
"from " + $('serType') table_alias+
" into outfile 'C://mytest/reports/service reports/"+$('serType') + "/" + "" + filename + "' " +
"fields terminated by ',' " +
"lines terminated by '\\n';";
var test = "select * from Age;";
reporting_DBConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver','jdbc:mysql://localhost:3310/cnwlreports', :D);
logger.info(reporting_exportQuery);
var result = reporting_DBConn.executeCachedQuery(reporting_exportQuery);
My guess is columnHeader have a diferent number of columns than select * in the union part.
Step to debug.
print the result string reporting_exportQuery
try running that string on the MySql.
Added:
First Select doesn't have FROM part
.
var reporting_exportQuery =
"select " + columnHeader +
"FROM SomeTable" <--- need FROM
" union all " +
"select " + columnHeader + <--- need same columns
my sql statments are as follows
the insert statement only works
update and delete statements don't work
Purchase_InvoiceNo is a primary key column of Purchase table
i get this value like this and insert its value to PurchaseProduct table
"SELECT IDENT_CURRENT ('Purchase') AS [Purchase_InvoiceNo]"
string deletecmd_PurchaseProduct =
#"DELETE FROM PurchaseProduct " +
"PurchaseProduct_No=#PurchaseProduct_No and "+
"Purchase_InvoiceNo=#Purchase_InvoiceNo ";
string updatcmd_PurchaseProduct =
"UPDATE PurchaseProduct "
+ " SET "
+ " PurchaseProduct_SerialNo =#PurchaseProduct_SerialNo"
+ ", Purchase_InvoiceNo =#Purchase_InvoiceNo"
+ ", ProductNo =#ProductNo"
+ " PurchaseProduct_Quantity =#PurchaseProduct_Quantity "
+ ", PurchaseProduct_Unit =#PurchaseProduct_Unit"
+ ", PurchaseProduct_Price =#PurchaseProduct_Price"
+ " Where "
+ " PurchaseProduct_No=#PurchaseProduct_No";
string insertcmd_PurchaseProduct = "INSERT INTO PurchaseProduct" +
"(" +
" PurchaseProduct_SerialNo"+
",Purchase_InvoiceNo" +
",ProductNo" +
",PurchaseProduct_Quantity " +
",PurchaseProduct_Price" +
",PurchaseProduct_Unit" + //6
")" +
"Values" +
"(" +
" #PurchaseProduct_SerialNo"+
",#Purchase_InvoiceNo" +
",#ProductNo " +
",#PurchaseProduct_Quantity " +
",#PurchaseProduct_Price" +
",#PurchaseProduct_Unit" + //6
");";
Your Delete command appears to be incomplete:
string deletecmd_PurchaseProduct =
#"DELETE FROM PurchaseProduct " +
"PurchaseProduct_No=#PurchaseProduct_No and "+
"Purchase_InvoiceNo=#Purchase_InvoiceNo ";
should be:
string deletecmd_PurchaseProduct =
#"DELETE FROM PurchaseProduct WHERE" +
"PurchaseProduct_No=#PurchaseProduct_No and "+
"Purchase_InvoiceNo=#Purchase_InvoiceNo ";
I can't see why the INSERT should fail unless you're inserting a duplicate primary key value or you're not inserting data into a non-nullable field which does not have a default.
string updatcmd_PurchaseProduct =
"UPDATE PurchaseProduct "
+ "SET PurchaseProduct_SerialNo = #PurchaseProduct_SerialNo, "
+ "Purchase_InvoiceNo = #Purchase_InvoiceNo, "
+ "ProductNo = #ProductNo, "
+ "PurchaseProduct_Quantity = #PurchaseProduct_Quantity, "
+ "PurchaseProduct_Unit = #PurchaseProduct_Unit, "
+ "PurchaseProduct_Price = #PurchaseProduct_Price "
+ "Where PurchaseProduct_No = #PurchaseProduct_No";
There was a missing comma above.