Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

sql - Saved View with a timestamp expression

Is there a standard for how data is represented in a Saved View where a timestamp/voltatile expression is used? For example, let's say I have the SQL query:

SELECT person_id, name, NOW() as now FROM table

The NOW() function will have a different value every time it is run. When saving the above to a view, is the data stored as:

person_id          name             now
1                  Bob              2020-01-01T01:01:01
2                  Billy            2020-01-01T01:01:01
3                  Barbara          2020-01-01T01:01:01

Or is it stored as:

person_id          name             now
1                  Bob              =NOW()
2                  Billy            =NOW()
3                  Barbara          =NOW()

Or, is it dependent on the database implementation and there is no standard?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can see how this would be misleading. However, a view does not "store" data (except for materialized views, which are a different store).

A view is a query which is substituted into the query where it is used. Admittedly, it could be parsed in advance, saving a small amount of effort.

However, a function such as now() is called when a query is executed. So, the value you will see is the value when the query is run.

This is actually a little confusing, because the function now() is not called for every row. Instead, it is evaluated once when the query starts execution. However, the query in question is the query that uses the view, not the command that creates the view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...