📜  dual in db2 - SQL (1)

📅  最后修改于: 2023-12-03 15:30:34.801000             🧑  作者: Mango

Dual in DB2 - SQL

DUAL is a special table in DB2 that you can use in SQL queries when you don't have a real table to select from. This might happen when you want to generate a series of numbers, or retrieve the current date and time. In these cases, you can select from DUAL to get the result you need.

Syntax

The syntax of using DUAL in a query is simple. You just include the name of the table in the FROM clause of your query, like this:

SELECT expression FROM DUAL

Where expression is any valid SQL expression that you want to retrieve.

Examples

Here are a few examples of using DUAL in DB2 SQL:

Retrieving the current date/time

To retrieve the current date and time, you can use the CURRENT_TIMESTAMP function in combination with DUAL, like this:

SELECT CURRENT_TIMESTAMP FROM DUAL

This will return a single row containing the current date and time.

Generating a series of numbers

To generate a series of numbers, you can use the ROW_NUMBER() function in combination with DUAL, like this:

SELECT ROW_NUMBER() OVER() AS num FROM DUAL FETCH FIRST 10 ROWS ONLY

This will return a table containing the numbers 1 through 10 in the num column.

Retrieving a constant value

Finally, you can simply retrieve a constant value by selecting it from DUAL, like this:

SELECT 'Hello, world!' FROM DUAL

This will return a single row containing the string "Hello, world!".