¡Caminando hacia el éxito!

Aprende en Comunidad

Avalados por :

Comparación entre el uso de cursores abiertos y SELECT en Open SQL

  • Creado 01/03/2024
  • Modificado 01/03/2024
  • 103 Vistas
0
Cargando...

Entre "OPEN CURSOR" y Open SQL (SELECT)

Si uso un cursor abierto con una declaración de selección paralela y normal, ¿cuál es mejor?

Pedro Pascal
Se unió el 07/03/2018
Pinterest
Telegram
Linkedin
Whatsapp

4 Respuestas

0
Cargando...

hi,

Una instrucción OPEN CURSOR genera la tabla de resultados definida bajo el nombre especificado con una instrucción DECLARE CURSOR.

<open_cursor_statement> ::= OPEN <result_table_name>

La instrucción DECLARE CURSOR define una tabla de resultados nombrada (ver tabla de resultados nombrada/sin nombre) con el nombre result_table_name.

<declare_cursor_statement> ::= DECLARE <result_table_name> CURSOR FOR <select_statement>

Una instrucción SELECT (select_statement) define y crea una tabla de resultados sin nombre (ver tabla de resultados nombrada/sin nombre).

Sintaxis

<select_statement> ::= <query_expression> [<order_clause>] [<update_clause>] [<lock_option>] [FOR REUSE]

expresión de consulta, cláusula de orden, cláusula de actualización, opción de bloqueo

No se permite una instrucción OPEN CURSOR para tablas de resultados creadas con esta instrucción SELECT.

La instrucción SELECT (select_statement) está sujeta a las reglas especificadas para la instrucción DECLARE CURSOR y aquellas especificadas para la instrucción OPEN CURSOR.

Dependiendo de la estrategia de búsqueda, se buscan todas las filas en la tabla de resultados cuando se ejecuta la instrucción SELECT (select_statement) y se genera físicamente la tabla de resultados, o se busca cada siguiente fila de la tabla de resultados cuando se ejecuta una instrucción FETCH, sin almacenarse físicamente. Esto debe tenerse en cuenta para el comportamiento temporal de las instrucciones FETCH.

Open SQL consiste en un conjunto de instrucciones ABAP que realizan operaciones en la base de datos central en SAP Web AS ABAP. Los resultados de las operaciones y cualquier mensaje de error son independientes del sistema de base de datos en uso. Por lo tanto, Open SQL proporciona una sintaxis y semántica uniforme para todos los sistemas de base de datos admitidos por SAP. Los programas ABAP que solo utilizan instrucciones Open SQL funcionarán en cualquier sistema SAP, independientemente del sistema de base de datos en uso. Las instrucciones Open SQL solo pueden trabajar con tablas de base de datos que se han creado en el Diccionario ABAP.

open sql

En el Diccionario ABAP, puedes combinar columnas de diferentes tablas de base de datos en una vista de base de datos (o vista abreviada). En las instrucciones Open SQL, las vistas se manejan exactamente de la misma manera que las tablas de base de datos. Cualquier referencia a tablas de base de datos en las secciones siguientes puede aplicarse igualmente a vistas.

La instrucción Open SQL para leer datos de tablas de base de datos es:

SELECT resultado

INTO objetivo

FROM fuente

[WHERE condición]

[GROUP BY campos]

[HAVING cond]

[ORDER BY campos].

La instrucción SELECT se divide en una serie de cláusulas simples, cada una con una función diferente en la selección, colocación y disposición de los datos de la base de datos.

Por favor, recompensa puntos si es útil,

shylaja

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Hola Veera, revisa esta declaración SQL dinámica también.

A continuación se muestra el código que utilicé para una consulta SQL dinámica y funcionó para mí.

DATA where_tab(25) OCURRE 5 CON CABECERA DE LÍNEA.

DATA p_kunnar like vbak-kunnr.

CONDENSE p_kunnar NO-GAPS.

IF p_kunnar NE ''.

APPEND 'vbak~kunnr = p_kunnar' A where_tab.

ENDIF.

SELECT DISTINCT vbeln

FROM vbak

INTO CAMPOS CORRESPONDIENTES DE LA TABLA gi_vtab

WHERE vbak-vkorg = p_vkorg Y

vbak-vtweg = p_vtweg Y

vbak-auart = 'LP' Y

(where_tab).

Saludos,

Hema.

Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Hi Veera,

OPEN CURSOR

OPEN CURSOR WITH HOLD dbcur FOR

SELECT result

FROM source

[FOR ALL ENTRIES IN itab] WHERE sql_cond

GROUP BY group HAVING group_cond

ORDER BY sort_key.

Addition: ... WITH HOLD

This statement opens a database cursor for the selection defined after FOR, and links a cursor variable dbcur with this database cursor. For dbcur, a declared variable with the specific predefined data type cursor must be entered. A database cursor dbcur that has already been opened cannot be opened again. A line of the resulting set is always assigned to an opened database cursor as a cursor position. After the OPEN CURSOR statement, the database cursor is positioned in front of the first line of the resulting set.

After FOR, the syntax of a SELECT statement can be entered, which contains all the additions of the normal SELECT statement, except for INTO and APPENDING. In the addition result, the addition SINGLE can also not be used after SELECT.

Only a limited number of database cursors can be open at the same time. An open database cursor can be closed using the statement CLOSE CURSOR . In addition, an open database cursor is closed for a database commit or a database rollback.

If a cursor variable dbcur of an open database cursor is assigned to another cursor variable, the latter is linked to the same database cursor at the same position. A cursor variable of an open database cursor can also be transferred to procedures that have been called externally, to enable the database cursor to be accessed from there.

It is not recommended to assign cursor variables to each other, but rather to set them exclusively using the statements OPEN CURSOR and CLOSE CURSOR.

Addition ... WITH HOLD

:

If the addition WITH HOLD is specified, the database cursor is not closed in an explicitly triggered database commit or database rollback, for example Native SQL. The addition WITH HOLD cannot be specified if the cursor is to be opened for a secondary database connection.

*****************************************************

Using a Cursor to Read Data

In the normal SELECT statement, the data from the selection is always read directly into the target area specified in the INTO clause during the SELECT statement. When you use a cursor to read data, you decouple the process from the SELECT statement. To do this, you must open a cursor for a SELECT statement. Afterwards, you can place the lines from the selection into a flat target area.

Opening and Closing Cursors

To open a cursor for a SELECT statement, use the following:

OPEN CURSOR WITH HOLD <c> FOR SELECT <result>

FROM <source>

WHERE <condition>

GROUP BY <fields>

HAVING <cond>

ORDER BY <fields>.

You must first have declared the cursor <c> using the DATA statement and the special data type CURSOR. You can use all clauses of the SELECT statement apart from the INTO clause. Furthermore, you can only formulate the SELECT clause so that the selection consists of more than one line. This means that you may not use the SINGLE addition, and that the column selection may not contain only aggregate expressions.

An open cursor points to an internal handler, similarly to a reference variable pointing to an object. You can reassign cursors so that more than one points to the same handler. In a MOVE statement, the target cursor adopts all of the attributes of the source cursor, namely its position, and all of the clauses in the OPEN CURSOR statement.

You can also open more than one cursor in parallel for a single database table. If a cursor is already open, you cannot reopen it. To close a cursor explicitly, use the following statement:

CLOSE CURSOR <c>.

You should use this statement to close all cursors that you no longer require, since only a limited number of cursors may be open simultaneously. With one exception, a database LUW is concluded when you close a cursor either explicitly or implicitly. The WITH HOLD addition in the OPEN CURSOR statement allows you to prevent a cursor from being closed when a database commit occurs in Native SQL.

Reading Data

An open cursor is linked to a multiple-line selection in the database table. To read the data into a target area in the ABAP program, use the following:

FETCH NEXT CURSOR <c> INTO <target>.

This writes one line of the selection into the target area <target>, and the cursor moves one line further in the selection set. The fetch statement decouples the INTO clause from the other clauses in the SELECT statement. All the INTO clauses of the SELECT statement can be used. The statement reads the lines that are needed to fill the target area of the INTO clause and moves the cursor to the next line to be read.

SY-SUBRC is set to 0 until all the lines of the selection have been read; otherwise it is 4. After a FETCH statement, system field SY-DBCNT contains the number of all the lines read so far for the corresponding cursor.

REPORT demo_select_cursor_1.

<
Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019
0
Cargando...

Hi Veera,

OPEN CURSOR

OPEN CURSOR WITH HOLD dbcur FOR

SELECT result

FROM source

[FOR ALL ENTRIES IN itab] WHERE sql_cond

GROUP BY group HAVING group_cond

ORDER BY sort_key.

Addition: ... WITH HOLD

This statement opens a database cursor for the selection defined after FOR, and links a cursor variable dbcur with this database cursor. For dbcur, a declared variable with the specific predefined data type cursor must be entered. A database cursor dbcur that has already been opened cannot be opened again. A line of the resulting set is always assigned to an opened database cursor as a cursor position. After the OPEN CURSOR statement, the database cursor is positioned in front of the first line of the resulting set.

After FOR, the syntax of a SELECT statement can be entered, which contains all the additions of the normal SELECT statement, except for INTO and APPENDING. In the addition result, the addition SINGLE can also not be used after SELECT.

Only a limited number of database cursors can be open at the same time. An open database cursor can be closed using the statement CLOSE CURSOR . In addition, an open database cursor is closed for a database commit or a database rollback.

If a cursor variable dbcur of an open database cursor is assigned to another cursor variable, the latter is linked to the same database cursor at the same position. A cursor variable of an open database cursor can also be transferred to procedures that have been called externally, to enable the database cursor to be accessed from there.

It is not recommended to assign cursor variables to each other, but rather to set them exclusively using the statements OPEN CURSOR and CLOSE CURSOR.

Addition ... WITH HOLD

:

If the addition WITH HOLD is specified, the database cursor is not closed in an explicitly triggered database commit or database rollback, for example Native SQL. The addition WITH HOLD cannot be specified if the cursor is to be opened for a secondary database connection.

*****************************************************

Using a Cursor to Read Data

In the normal SELECT statement, the data from the selection is always read directly into the target area specified in the INTO clause during the SELECT statement. When you use a cursor to read data, you decouple the process from the SELECT statement. To do this, you must open a cursor for a SELECT statement. Afterwards, you can place the lines from the selection into a flat target area.

Opening and Closing Cursors

To open a cursor for a SELECT statement, use the following:

OPEN CURSOR WITH HOLD <c> FOR SELECT <result>

FROM <source>

WHERE <condition>

GROUP BY <fields>

HAVING <cond>

ORDER BY <fields>.

You must first have declared the cursor <c> using the DATA statement and the special data type CURSOR. You can use all clauses of the SELECT statement apart from the INTO clause. Furthermore, you can only formulate the SELECT clause so that the selection consists of more than one line. This means that you may not use the SINGLE addition, and that the column selection may not contain only aggregate expressions.

An open cursor points to an internal handler, similarly to a reference variable pointing to an object. You can reassign cursors so that more than one points to the same handler. In a MOVE statement, the target cursor adopts all of the attributes of the source cursor, namely its position, and all of the clauses in the OPEN CURSOR statement.

You can also open more than one cursor in parallel for a single database table. If a cursor is already open, you cannot reopen it. To close a cursor explicitly, use the following statement:

CLOSE CURSOR <c>.

You should use this statement to close all cursors that you no longer require, since only a limited number of cursors may be open simultaneously. With one exception, a database LUW is concluded when you close a cursor either explicitly or implicitly. The WITH HOLD addition in the OPEN CURSOR statement allows you to prevent a cursor from being closed when a database commit occurs in Native SQL.

Reading Data

An open cursor is linked to a multiple-line selection in the database table. To read the data into a target area in the ABAP program, use the following:

FETCH NEXT CURSOR <c> INTO <target>.

This writes one line of the selection into the target area <target>, and the cursor moves one line further in the selection set. The fetch statement decouples the INTO clause from the other clauses in the SELECT statement. All the INTO clauses of the SELECT statement can be used. The statement reads the lines that are needed to fill the target area of the INTO clause and moves the cursor to the next line to be read.

SY-SUBRC is set to 0 until all the lines of the selection have been read; otherwise it is 4. After a FETCH statement, system field SY-DBCNT contains the number of all the lines read so far for the corresponding cursor.

REPORT demo_select_cursor_1.

<
Respondido el 15/04/2024
LUCIANO RIOJA GHIOTTO
Se unió el 13/07/2019

contacto@primeinstitute.com

(+51) 1641 9379
(+57) 1489 6964

© 2024 Copyright. Todos los derechos reservados.

Desarrollado por Prime Institute

¡Hola! Soy Diana, asesora académica de Prime Institute, indícame en que curso estas interesado, saludos!
Hola ¿Puedo ayudarte?