DECLARE 
 @value1 varchar(10),
 @value2 varchar(20)

SET @value1 = ''
SET @value2 = 'value'

SELECT * FROM [TABLE] WITH(NOLOCK)
WHERE
     ([COL1] = '' OR [COL1] = @value1)
     AND ([COL2] = '' OR [COL2] = @value2)

위와 같이 사용하면 조건값이 '' 이면 참이 되어 OR 뒤의 조건이 실행되지 않는다.
때문에 [COL1] = @value1의 조건은 무시되고 [COL2] = @value2 조건만 검색하게 된다.

AND


현재 위치의 정보를 보여주고 다음 목적지의 이름을 가져와야 하는데 뭔가 간단한 방법이 있지 않을까 해서 알아보니 오라클에 LEAD() 라는 함수가 존재하더군요.

LEAD() 함수 덕분에 간단하게 다음 로우에 있는 데이터를 가져 올 수 있게 되었네요.

반대로 이전 로우의 데이터를 가져 올 때는 LAG() 라는 함수를 같은 방식으로 사용하면 됩니다.

아래는 오라클 사이트에서의 LEAD() 함수에 대한 설명 원문입니다.


Syntax

Description of lead.gif follows
Description of the illustration lead.gif

See Also:

"Analytic Functions" for information on syntax, semantics, and restrictions, including valid forms of value_expr

Purpose

LEAD is an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LEAD provides access to a row at a given physical offset beyond that position.

If you do not specify offset, then its default is 1. The optional default value is returned if the offset goes beyond the scope of the table. If you do not specify default, then its default value is null.

You cannot nest analytic functions by using LEAD or any other analytic function for value_expr. However, you can use other built-in function expressions for value_expr.

See Also:

"About SQL Expressions" for information on valid forms of expr and LAG

Examples

The following example provides, for each employee in the employees table, the hire date of the employee hired just after:

SELECT last_name, hire_date, 
LEAD(hire_date, 1) OVER (ORDER BY hire_date) AS "NextHired"
FROM employees WHERE department_id = 30
ORDER BY last_name, hire_date, "NextHired";

LAST_NAME HIRE_DATE NextHired
------------------------- --------- ---------
Baida 24-DEC-97 15-NOV-98
Colmenares 10-AUG-99
Himuro 15-NOV-98 10-AUG-99
Khoo 18-MAY-95 24-JUL-97
Raphaely 07-DEC-94 18-MAY-95
Tobias 24-JUL-97 24-DEC-97


출처 : http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions079.htm#i83834

'SQL > ORACLE' 카테고리의 다른 글

오라클에서 IF NOT EXISTS 구현  (3) 2009.05.07

AND


출처 : http://kr.forums.oracle.com/forums/thread.jspa?threadID=477102&tstart=120

제품 : JDBC

작성날짜 : 2003-08-06

JDBC PROGRAM 에서 REF CURSOR 를 사용하는 SAMPLE
==============================================


PURPOSE


--------------------------------------------------------------------------------
dynamic SQL 과 흡사하게 JDBC program 에서 SQL 을 실행할때
REF Cursors 를 사용하는 방법을 알아 봅니다.

Explanation

--------------------------------------------------------------------------------

PreparedStatement


--------------------------------------------------------------------------------

JDBC 는 PreparedStatement 를 이용해 dynamic SQL 을 실행하기 위한
APIs 를 제공합니다.

예제 :

PreparedStatement pstmt;
pstmt=conn.prepareStatement("select empno from emp where deptno=?");
pstmt.setInt(1,10);
ResultSet c1;
c1=pstmt.executeQuery();
pstmt.setInt(1,10)
while (c1.next ())
{System.out.println (c1.getInt(1));}


REF CURSOR


--------------------------------------------------------------------------------

JDBC 에서 dynamic SQL 를 실행하는 다른 방법이 있습니다.
이 방법은 Oracle8i 이상에서 실행가능합니다.

다음의 경우 REF CURSOR 를 resturn 하는 PL/SQL procedure 를 사용했습니다.

REF CURSOR 는 cursor 에 대한 pointer 입니다.
Stored procedures 는 REF CURSOR category 의
cursor 변수나 user-defined types 을 반환합니다.
이 결과는 database cursor 나 JDBC result set 과 같습니다.
REF CURSOR 는 근본적으로는 query 의 결과를 캡슐로 싸고 있습니다.

REF CURSOR 사용 예제


--------------------------------------------------------------------------------

create or replace package test_ref_cursor as
type gc is REF CURSOR;
procedure test_ref_cursor(v1 OUT gc, v2 in varchar2);
end;
/

create or replace package body test_ref_cursor as
procedure test_ref_cursor(v1 OUT gc, v2 in varchar2) as
begin
open v1 for v2;
end;
end;
/

장점
----

o Code 재사용
동일한 package procedure 를 java program 이나 그밖의
program 에서 사용할 수 있습니다.

o Load Balancing.


Example


--------------------------------------------------------------------------------

JDBC Program 에서 REF CURSOR 를 사용하는 방법


--------------------------------------------------------------------------------

1. stored procedure 를 호출하기 위해 JDBC callable statement 를 사용합니다.

CallableStatement ocstmt;
cstmt = conn.prepareCall("begin test_ref_cursor.test_ref_cursor(?,?); end;");

2. CallableStatement 를 위한 REF CURSOR output parameter 를 OracleTypes.CURSOR
로 등록합니다.

주의 : OracleTypes class 는 oracle.jdbc.driver 아래에 있습니다.
따라서 oracle.jdbc.driver.OracleTypes 를 import 하셔야 합니다.

ocstmt.registerOutParameter (1, OracleTypes.CURSOR);

3. CallableStatement 를 실행합니다.

ocstmt.execute ();

4. getCursor() method 를 사용하기 위해 CallableStatement 를
OracleCallableStatement object 로 바꿉니다.
getCursor() method 는 오라클에서 표준 JDBC API 를 확장해서 만든 것입니다.
이 method 는 REF CURSOR 를 ResultSet object 로 return 합니다.

OracleCallableStatement tstmt;
tstmt = (OracleCallableStatement)ocstmt;

5. OracleCallableStatement 의 getCursor() method 를 사용해서 REF CURSOR 를
JDBC ResultSet variable 에 저장합니다.

ResultSet cursor;
cursor = tstmt.getCursor (1);

6. 위에서 생성한 resultset을 사용합니다.


Example


--------------------------------------------------------------------------------

다음 예제에서는 위에서 언급한 package procedure 에서 data 를 추출합니다.
procedure 는 input argument 로 실행할 select 문장을 받습니다.
package procedure 는 실행할 select 문장을 위해 REF CUSROR 를 생성합니다.
이 REF CUSROR 는 JDBC program 안에서 ResultSet 에 담기게 됩니다.

import java.sql.*;
import oracle.jdbc.driver.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes;
/* Could also use import oracle.jdbc.driver.*; instead of the last two above imports */

public class REFtest {

public static void main(String[] args) {
REFtest vTest = new REFtest();
vTest.call_stmt();

}

void call_stmt()
{
try {


DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:@krint-5.kr.oracle.com:1525:ora920", "scott", "tiger");
CallableStatement ocstmt;
ocstmt = conn.prepareCall
("begin test_ref_cursor.test_ref_cursor(?,?); end;");
ocstmt.setString(2,"select ename from emp");
ocstmt.registerOutParameter (1, OracleTypes.CURSOR);
ocstmt.execute ();
OracleCallableStatement tstmt;
tstmt = (OracleCallableStatement)ocstmt;
ResultSet cursor;
cursor = tstmt.getCursor (1);
// Use the cursor like a normal ResultSet
while (cursor.next ())
{System.out.println (cursor.getString (1));}
}
catch(Exception e)
{
}

}
}


Reference Documents

AND