$(db)sql;…

Read(5352) Label: dbsql,

Here’s how to use $(db)sql;… function.

$(db)sql;…

Description:

Execute the specified SQL statement on the data source and return the result.

Syntax:

$(db)sql;…

Note:

On the data source db, execute the specified SQL statement sql and return the execution result. db is the data source name. If omitting (db), then use the data source specified by the previous statement. If no data source is specified in the previous statement, then use any of the currently connected data sources.

 

When data source name is not specified, the statement is regarded as a simple SQL statement.

Parameter:

db

Data source name.

sql

A sql statement, which can be select, insert, delete, update, with, cursor for select, or cursor for with. A cursor for select statement and a cursor for with statement return a cursor.

sql allows using ?x to reference an external cell or variable x.

…

Argument value passed to the SQL statement.

Return value:

Table sequence/Cursor

Example:

SELECT statement:

 

A

 

1

$select * from EMPLOYEE

No data source is specified in the previous statement since parameter (db) is absent, and error report appears if there isn’t a currently connected data source.

2

$(demo)select * from EMPLOYEE where EID=?;1

Find the employees whose EID is 1 from the demo data source.

3

$select * from EMPLOYEE where EID in (?) or GENDER=?;[1,3,5,7],"M"

Since parameter (db) is absent, the program uses the demo data source defined in the previous statement to find the employees whose EID is [1,3,5,7] and gender is M.

 

INSERT statement:

 

A

B

 

1

$(demo)insert into EMPLOYEE (EID, NAME) values(?,?);100,"test"

 

Insert statement.

2

[51,52,53,54]

 

 

3

for A2

 

 

4

 

$insert into STATECAPITAL (STATEID) values(?);A3

Since parameter (db) is absent, the program uses the demo data source defined in the previous statement; pass members of A2’s sequence to B4’s statement as parameters by loop.

 

DELETE statement:

 

A

 

1

$(demo)delete from EMPLOYEE where EID =? or EID=?;100,101

 

2

$delete from EMPLOYEE where EID in(?);[1,5,7,9]

 

3

$delete from EMPLOYEE where NAME ='Rebecca'

Since parameter (db) is absent and no data source is specified in the previous statement, the program uses one of the currently connected data sources.

 

UPDATE statement:

 

A

 

1

$(demo)update EMPLOYEE set NAME =?, GENDER=?

where EID =?;"testnew","M",100

 

2

$update EMPLOYEE set NAME ='Peter' where EID =10

Since parameter (db) is absent, the program uses the data source defined in the previous statement to modify the NAME of the employee whose EID is 10 to Peter.

3

$(sql)update Family set Name='Rose' where Eid=?;2

Update the name of a record of Family whose Eid is 2 by connecting to a data source named sql.

 

Ø  External cell and variable reference

 

A

 

1

>dept="HR"

Define a variable: its name is dept, and its value is HR.

2

F

A2’s value is F.

3

$('demo')select EID,NAME,GENDER,DEPT  from employee where DEPT=?dept and GENDER=?A2

Use ? to reference variable dept and A2’s value, and return result as follows?

 

Ø  with statement

 

A

 

1

$('demo')with HR_dept as(select *  from employee where DEPT='HR') select * from HR_dept where GENDER = 'M'

Query data of male employees in HR department.

2

$('duckdb')with  t1 AS ( SELECT * FROM employee WHERE DEPT='HR' and SALARY< 5000)  update  employee set SALARY = SALARY*1.2  where  EID in (select EID from  t1)

Increase salaries of employees in HR department who earn less than 5,000 to 1.2 times of their original salaries.

 

Ø  cursor statement

 

A

 

1

$('demo')cursor for select * from employee

Retrieve data from employee table and return a cursor.

2

$('demo')cursor for with HR_dept as(select *  from employee where DEPT='HR') select * from HR_dept where GENDER = 'M'

Use with to define a temporary result for querying employee data in HR department, filter the data, and return a cursor.

Related function:

db.query()

$(db)sql;…

Description:

Through esProc JDBC, execute a SQL statement in the data source and return the result set.

Syntax:

$(db)sql;…

Note:

In the specified database db, the function executes the specified SQL statement, and returns the result set once executed. Use st.executeQuery() for the execution and return the result set. Make sure the database db must be connected.

 

In JDBC, a string starting with $select or $with will be interpreted as a simple SQL statement for execution.

Parameter:

sql

A SQL statement in the form of select * from table; it should be one of select/insert/delete/update statements.

(db)

Data source name; will be interpreted as a simple SQL statement when omitted.

…

SQL parameter’s value.

Return value:

Set

Example:

Execute SQL statement in the database:

public void testDataServer() {

  Connection con = null;

  java.sql.Statement st;

  try{

  Class.forName("com.esproc.jdbc.InternalDriver");

  con= DriverManager.getConnection("jdbc:esproc:local://"); 

  st=con.createStatement();

  // Query demo database student table to find the data of students who are older than 16

  ResultSet set = st.executeQuery("$(demo)select * from STUDENTS where AGE>?;16");

  printRs(set);

  }

  catch(Exception e){

  System.out.println(e);

  }

  finally{

  // Close the connection

  if (con!=null) {

  try {

  con.close();

  }

  catch(Exception e) {

  System.out.println(e);

  }

  }

  }

  }

 

Execute a simple SQL statement to query a data file:

public void testDataServer(){

   Connection con = null;

    java.sql.Statement st;

   try{

    // Establish data source connection

    Class.forName("com.esproc.jdbc.InternalDriver");

    con= DriverManager.getConnection("jdbc:esproc:local://");

      st =con.createStatement();

    //Use executeQuery method to execute the simple SQL statement and query data in cities.txt

    ResultSet set = st.executeQuery ("$select * from cities.txt ");

    ResultSetMetaData meta = set.getMetaData();

    while (set.next()) {

    for(int i=0; i<meta.getColumnCount(); i++){ 

  System.out.print(set.getObject(i+1) + "\t");

  }

  System.out.println();

  }

    }

    catch(Exception e){

    System.out.println(e);

    }

    finally{

    // Close data source connection

    if (con!=null) {

    try {

    con.close();

    }

    catch(Exception e) {

    System.out.println(e);

    }

    }

    }