I have some Oracle SQL statements in my java code which is often split in singular parts which makes in difficult for me to find an equivalent statement in SQL Server 2017. Here is an example:
if (typeId >= 1 && typeId <= 5)
{
sql = "SELECT t.run_id, t.tran_id, t.tran_id sort_id, t.tran_type, t.prod_id, t.type_id, t.value, " +
getExtractStatement() + " (year from t.tran_datetime) y, " +
getExtractStatement() + " (month from t.tran_datetime) mo, " +
getExtractStatement() + " (day from t.tran_datetime) d, " +
" to_number(to_char (t.tran_datetime, 'HH24')) h, " +
" to_number(to_char (t.tran_datetime, 'MI')) mi, " +
" to_number(to_char (t.tran_datetime, 'SS')) s, " +
" to_number(to_char (t.tran_datetime, 'FF')) ms " +
" FROM tran_calc_group t, mai_group_log m " +
" WHERE t.run_id = m.run_id and m.group_id = 1 " +
" AND m.mai_class_id = %d " +
" AND t.result_group_id = m.level_1 " +
" AND t.result_group_id_2 = m.level_2 " +
" AND t.result_group_id_3 = m.level_3 " +
" AND t.result_group_id_4 = m.level_4 " +
" AND t.prod_id = " + query.getProdId() +
" AND t.run_id IN (" + runLogIds + ")" +
" AND t.type_id = " + typeId;
}
If it were only a usual to_char statement with a YYYY-MM-DD argument for example I could just use GETDATE(), 20 in SQL Server but I don't know how to do this for such a split statement
" to_number(to_char (t.tran_datetime, 'HH24')) h, " +
" to_number(to_char (t.tran_datetime, 'MI')) mi, " +
" to_number(to_char (t.tran_datetime, 'SS')) s, " +
" to_number(to_char (t.tran_datetime, 'FF')) ms " +
I have tried to convert it with a tool (SQLines) but it did not work.
You can check the DATEPART function in SQL Server. It allows you to extract particular parts of given date and time value. For example:
SELECT GETUTCDATE()
,DATEPART(HOUR, GETUTCDATE())
,DATEPART(MINUTE, GETUTCDATE())
,DATEPART(SECOND, GETUTCDATE())
,DATEPART(MILLISECOND, GETUTCDATE());
Also, as stated in the documentation, it returns integer, so there is no need for additional converting.
Related
For administration needs, I need to create and replace stored procedures from Spring Repository. Has anyone already done this?
I tried to use following code(unfinished):
#Component
public class JdbcRepository {
#Autowired
private JdbcTemplate jdbc;
public void checkConn(){
jdbc.execute("create or replace package Z$CLIENT_INTERFACE_API as \n" +
" function CL_ORG_SEARCH_CREATE(p_request in clob) return clob;\n" +
"end Z$CLIENT_INTERFACE_API;\n" +
"/\n" +
"create or replace package body Z$CLIENT_INTERFACE_API as\n" +
" function CL_ORG_SEARCH_CREATE(p_request in clob) return clob\n" +
" is\n" +
" content_xml VARCHAR2(4000);\n" +
" p Dbms_Xmlparser.Parser;\n" +
" v_Doc Dbms_Xmldom.Domdocument;\n" +
" v_Root_Element Dbms_Xmldom.Domelement;\n" +
" v_Child_Nodes Dbms_Xmldom.Domnodelist;\n" +
" v_Child_Node Dbms_Xmldom.Domnode;\n" +
" v_Message_Id VARCHAR2(36);\n" +
" v_First_Char VARCHAR2(1);\n" +
" begin\n" +
" content_xml:= CAST(p_request as VARCHAR2);\n" +
" p := Dbms_Xmlparser.Newparser;\n" +
" dbms_xmlparser.setvalidationmode(p,False);\n" +
" dbms_xmlparser.parsebuffer(p,content_xml);\n" +
" v_Doc := dbms_xmlparser.getdocument(p);\n" +
" v_Root_Element := Dbms_Xmldom.getdocumentelement(v_Doc);\n" +
" return 'aaaaaaaaaaaaaaaaaaaaaaaaaa1';\n" +
" end;\n" +
"end Z$CLIENT_INTERFACE_API;\n" +
"/");
}
}
But whan I execute it, i take broken package in db. In same time, whan I run this from SQLDeveloper - all works perfect.
You are separating statements by both semicolon ; and slash /.
Try using only one of those, but not both. If that doesn't work either, try executing each statement separately with execute( )
This works perfect:
jdbc.execute("create or replace package Z$CLIENT_INTERFACE_API as \n" +
" function CL_ORG_SEARCH_CREATE(p_request in clob) return clob;\n" +
"end Z$CLIENT_INTERFACE_API;\n");
jdbc.execute("create or replace package body Z$CLIENT_INTERFACE_API as\n" +
" function CL_ORG_SEARCH_CREATE(p_request in clob) return clob\n" +
" is\n" +
" content_xml VARCHAR2(4000);\n" +
" p Dbms_Xmlparser.Parser;\n" +
" v_Doc Dbms_Xmldom.Domdocument;\n" +
" v_Root_Element Dbms_Xmldom.Domelement;\n" +
" v_Child_Nodes Dbms_Xmldom.Domnodelist;\n" +
" v_Child_Node Dbms_Xmldom.Domnode;\n" +
" v_Message_Id VARCHAR2(36);\n" +
" v_First_Char VARCHAR2(1);\n" +
" begin\n" +
" content_xml:= CAST(p_request as VARCHAR2);\n" +
" p := Dbms_Xmlparser.Newparser;\n" +
" dbms_xmlparser.setvalidationmode(p,False);\n" +
" dbms_xmlparser.parsebuffer(p,content_xml);\n" +
" v_Doc := dbms_xmlparser.getdocument(p);\n" +
" v_Root_Element := Dbms_Xmldom.getdocumentelement(v_Doc);\n" +
" return 'aaaaaaaaaaaaaaaaaaaaaaaaaa1';\n" +
" end;\n" +
"end Z$CLIENT_INTERFACE_API;");
I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"
I am busy using a SQLite database with a java application and after updating the database successfully I get the following error
org.sqlite.jdbc4.JDBC4PreparedStatement#4e1c6f
Below is my update code
PreparedStatement update = con.prepareStatement("UPDATE highLeaker SET " + cyc + " = "
+ dataArray[3] + " , "
+ "gasSurveyOperator" + " = "
+ dataArray[1] + " , "
+ "gasSurveySerial" + " = "
+ dataArray[2] + " , "
+ "loss" + " = "
+ dataArray[4] + " , "
+ "comment" + " = "
+ dataArray[5] + " , "
+ "cycle" + " = '"
+ cycleT + "' , "
+ "date" + " = "
+ dataArray[6]
+ " WHERE leakerID = " + dataArray[0]+";");
System.out.println(update);
update.executeUpdate();
dataRow = CSVFile.readLine(); // Read next line of data.
You printed out the object. Also, I would highly recommend using place holders instead when executing SQL queries.
I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...
You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.
You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...
okay i have source like this
public List<SearchRecord> getResult() {
List<SearchRecord> searchResult = new ArrayList<SearchRecord>();
String query = "SELECT " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B + " UNION ALL SELECT " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
Cursor cursor = mDatabase.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
do {
SearchRecord sr = new SearchRecord();
sr.setRecordA(cursor.getString(0));
sr.setRecordB(cursor.getString(1));
sr.setRecordC(cursor.getString(2));
searchResult.add(sr);
} while (cursor.moveToNext());
return searchResult;
}
return null;
}
with this code i can get some record from multiple table who have same column name, and store the result in a List object. but how to i find out which table is this record belongs ? is it from TABLE_A, TABLE_B, TABLE_C, or the other
As per SQL, no, you are joining 3 sets of rows and operating them the same way, each record cannot be distinguished by its origin.
Of course, you could do something like
String query = "SELECT 'TABLE_A', " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT 'TABLE_B' " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B
+ " UNION ALL SELECT 'TABLE_C', " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT 'TABLE_D', "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
:-)