How to use a user-defined variable in MySQL stored procedure? - mysql

I am trying to use a CASE statement in MySQL based on a variable. I tried the code below; however, I have found that "The SELECT statement cannot refer to system variables or user-defined variables". What I am trying to achieve is that I have an input date. I get its day of week so I know which day it is on (1 is Sunday). I then only want rows returned in the query that have the day set to "Y" (e.g., if day of week is 1 then stt_sunday is "Y"). The table has:
stt_id
stt_start_date
stt_end_date
stt_start_time
stt_end_time
stt_sunday
stt_monday
stt_tuesday
stt_wednesday
stt_thursday
stt_friday
stt_saturday
My code is:
String selectQry1 = (" SET #clickedDateDOW = clickedDateDOW; " +
" SELECT st_type, csm_session_timetable.stt_id, stt_start_date, stt_end_date, stt_start_time, stt_end_time " +
" FROM csm_session_type, csm_session_timetable " +
" WHERE csm_session_type.cli_id = ? " +
" AND csm_session_type.prm_id = ? " +
" AND csm_session_type.st_id = csm_session_timetable.st_id " +
" AND (csm_session_timetable.stt_end_date IS NULL || csm_session_timetable.stt_end_date > ?) " +
" AND csm_session_timetable.gym_id = ? " +
" AND CASE " +
" WHEN #clickedDateDOW = 1 AND stt_sunday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 2 AND stt_monday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 3 AND stt_tuesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 4 AND stt_wednesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 5 AND stt_thursday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 6 AND stt_friday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 7 AND stt_saturday LIKE 'Y' THEN 1 " +
" ORDER BY stt_start_date DESC, stt_start_time, stt_end_date DESC;");

Related

Why am I getting a "No value specified for parameter 2" error in Spring boot jdbc template

My sql query looks like:
select T.Col1
from (
select Past_Diagnoses_1 as Col1
from patienthistory
union
select Past_Diagnoses_2 as Col1
from patienthistory
union
select Past_Diagnoses_3 as Col1
from patienthistory
union
select Past_Diagnoses_4 as Col1
from patienthistory
) as T;
I keep getting an error on Spring which says No value specified for parameter 2
I am trying to union 4 columns together into 1 super long column.
This is a simplified code. I have 32 columns but for question I have shown 4
Java code :
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select T.Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_3 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_4 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_5 as Col1\n" +
" from patienthistory\n"
" where Patient_Number = ?\n" +
" ) as T";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("T.Col1")
));
}

SSRS - trying to get total rows by using CountRows function

I am trying to get the total rows returned by each tablix. Each row returned is an error and I need to get the total errors. Each tablix has it own data set name so I am trying to use CountRows(data set) and add them together to get the total rows returned or total errors.
Code:
="Number of Errors: " & CountRows("Reader_Check")'' + '' & CountRows("Access_Panel_Check")'' + '' & CountRows("Reader_Check")'' + '' & CountRows("Alarm_Panel_Check")'' + '' & CountRows("Alarm_Input_Check")'' + '' & CountRows("Alarm_Output_Check")'' + '' & CountRows("Segment_Check")'' + '' & CountRows("Access_Level_Check")'' + '' & CountRows("Area_Check")'' + '' & CountRows("Timezone_Check")'' + '' & CountRows("Alarm_Check")
Any help would be great. Thanks
It looks like you are appending the + as a string?
I think you just need something like
="Number of Errors: " &
(CountRows("Reader_Check")
+ CountRows("Access_Panel_Check")
+ CountRows("Reader_Check")
+ CountRows("Alarm_Panel_Check")
+ CountRows("Alarm_Input_Check")
+ CountRows("Alarm_Output_Check")
+ CountRows("Segment_Check")
+ CountRows("Access_Level_Check")
+ CountRows("Area_Check")
+ CountRows("Timezone_Check")
+ CountRows("Alarm_Check")
)
Note: you have Reader_Check specified twice which I assume is incorrect

How to add SELECT TOP (CASE WHEN user=#{username} THEN 100 ELSE 0 END) PERCENT * FROM (SELECT .... )?

My goal is to return either all of the row or just an empty array [] if "not one row" have column user with value #{username}.
This is the SQL SELECT statement
#Select("SELECT * FROM ( SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} )")
List<MemberModel> getChannelDetail(String username, String channelUuid);
My goal is to change the 100 PERCENT to 0 PERCENT if "not one row" have column user with value of #{username}
What I've tried
Add SELECT TOP (CASE WHEN user=#{username} THEN 100 ELSE 0 END) PERCENT ...
#Select("SELECT * FROM ( SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} ) " +
"LIMIT CASE WHEN user=#{username} THEN ALL ELSE 0 END")
List<MemberModel> getChannelDetail(String username, String channelUuid);
But it throws an error
Column "USER" not found; SQL statement:

MySQL - using a prepared statement to copy a row in the same table and include static values throws an error

I am trying to use a prepared statement to copy one row to a new row (INSERT)in the same table and include some static values. However, I get the following error:
SQLException in copyProgram: java.sql.SQLException: Operand should contain 1 column(s)
My input is:
prId: 4 accountID: 50 newFromDate: 2020-04-27 newToDate: 2020-04-28
My code is:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " (SELECT (?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " FROM at_program "
+ " WHERE pr_id = ?);");
ps = c.prepareStatement(insertQry, Statement.RETURN_GENERATED_KEYS);
// Create a statement and execute the query on it
ps.setString(1, accountID);
ps.setString(2, fromDate);
ps.setString(3, toDate);
ps.setString(4, prID);
ps.executeUpdate();
You have parens around the field list in your SELECT! Remove them.
The issue is that SELECT expects a list of (individual) columns, but you are passing a multi-column value.
SELECT x, y, z sees x, y and z as three values each consisting of one column, yet you are passing SELECT (x, y, z) which is interpreted as one value with three columns, hence the error.
By the way, the parens around the whole SELECT are superfluous as well, this time because INSERT INTO table (columns) SELECT ... is a recognized syntax construct in itself.
Corrected code:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " SELECT ?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes "
+ " FROM at_program "
+ " WHERE pr_id = ?;");

MySql Insert Into JDBC

I'm trying to execute a query using JDBC in mysql using INSERT INTO SELECT
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/new_db?&user=root&password=");
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
String sql
= "INSERT INTO `tr_tmp`(`id`, `root_group_id`, `IATANO`, `NRPB`, `PlatedCarrierCode`, `PlatedCarrierName`, "
+ "`DateOfIssue`, `DomInt`, `FlownCarrierCode`, `FlownCarrierName`, `FlightNo`, `OperatedCarriercode`, "
+ "`FlightDate`, `Class`, `Origin`, `Destination`, `CashAmt`, `ExCashAmt`, `CreditAmt`, `EXCreditAmt`, "
+ "`EffCommAmt`, `EXEffCommAmt`, `RefCashAmt`, `RefCreditAmt`, `RefEffCommAmt`, "
+ "`ACMCashAmt`, `ACMCreditAmt`, `ACMEffCommAmt`, `ADMCashAmt`, `ADMCreditAmt`, `ADMEffCommAmt`, "
+ "`Farebasis`, `TaxCashAmt`, `EXTaxCashAmt`, "
+ "`TaxCreditAmt`, `EXTaxCreditAmt`, `RefTaxCashAmt`, `RefTaxCreditAmt`, "
+ "`ACMTaxCashAmt`, `ACMTaxCreditAmt`, `ADMTaxCashAmt`, `ADMTaxCreditAmt`, "
+ " `YQTax`, `EXYQtax`, `YRTax`, `EXYRtax`, `Currency`, NFCode) "
+ "(SELECT null, 100 as groupId, a.agtn as `IATANO`, b.nrid as NRPB, m.CarrierCode as PlatedCarrierCode, m.CarrierName as PlatedCarrierName, "
+ "c.DAIS as DateOfIssue, c.STAT as DomInt,j.CARR as FlownCarrierCode , l.CarrierName as FlownCarrierName, TRIM(LEADING '0' FROM j.FTNR) as FlightNo, j.OP_CARR as OP_CARR, "
+ "j.FLIGHTDT as FlightDate, j.RBKD as Class, j.ORAC AS Origin, j.DSTC AS Destination, k.CashAmt, k.ExCashAmt, k.CreditAmt, k.EXCreditAmt, "
+ "k.EffCommAmt as EffCommAmt,k.EXEffCommAmt as EXEffCommAmt, "
+ "k.RefCashAmt as RefCashAmt, k.RefCreditAmt as RefCreditAmt, k.RefEffCommAmt as RefEffCommAmt, "
+ "k.ACMCashAmt as ACMCashAmt, k.ACMCreditAmt as ACMCreditAmt, k.ACMEffCommAmt as ACMEffCommAmt, "
+ "k.ADMCashAmt as ADMCashAmt, k.ADMCreditAmt as ADMCreditAmt, k.ADMEffCommAmt as ADMEffCommAmt, "
+ "j.FBTD as Farebasis,k.TaxCashAmt as TaxCashAmt, k.EXTaxCashAmt as EXTaxCashAmt, "
+ "k.TaxCreditAmt, k.EXTaxCreditAmt, k.RefTaxCashAmt, k.RefTaxCreditAmt, "
+ "k.ACMTaxCashAmt, k.ACMTaxCreditAmt, k.ADMTaxCashAmt, k.ADMTaxCreditAmt, "
+ "k.YQTax, k.EXYQtax, k.YRTax, k.EXYRtax, k.CUTP as Currency, c.NefCode as NFCode "
+ "FROM tp_boh03 a "
+ "INNER JOIN tp_bkt06 b ON a.offheadid= b.offheadid "
+ "INNER JOIN tp_tickets c ON b.transheadid= c.transheadid "
+ "LEFT JOIN tp_bks39 f ON c.TicketHeadId = f.TicketHeadId "
+ "INNER JOIN tp_bki63 j ON c.TicketHeadId = j.TicketHeadId "
+ "INNER JOIN tp_mileage_fare k ON j.ItinerarySegmentID = k.ItinerarySegmentID "
+ "INNER JOIN tp_iata ti ON a.AGTN = ti.iata_no "
+ "LEFT JOIN tp_carriers l ON j.CARR = l.CarrierCode "
+ "LEFT JOIN tp_carriers m ON b.TACN = m.TicketCode)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.execute();
System.out.println("TIME TAKE : " + (System.currentTimeMillis() - start));
} catch (Exception ex) {
ex.printStackTrace();
}
The data expected should insert rows amounting to 2 million, but it's not inserting all data, every run of the script the rows being inserted changes. I tried to run the script directly in mysql and it did inserted same amount of rows after few re-runs.