I'm passing a single string column table parameter (no other option, must be one column). I've read that there's no guarantee of order on a TVP, but all the articles I've read are referring to a select statement. If I run a cursor on a TVP, will it always read from first record to last?
Table-valued parameter is a table, a temporary table.
Any table, including temporary tables in SQL Server is an unordered set of rows.
When you write
SELECT Column1 FROM Table1
the server can return rows in any order it chooses. It will return all rows of a table, of course, you just can't predict in which order.
If you need some specific order, then you have to specify ORDER BY clause, like this:
SELECT Column1 FROM Table1 ORDER BY Column2
All this applies to TVP and cursors.
If you define a cursor without ORDER BY, it will read through all rows, but in some unspecified order, which may vary with each run. If you need the cursor to process rows of TVP in some specific order, then specify the ORDER BY clause in the SELECT statement of the cursor definition.
In other words, the cursor will always read from the "first" record to the "last". If it is important to the logic of the cursor to have defined what is "first" and what is "last" you need to add ORDER BY clause.
Related
When I insert a row in a Firebird database, it doesn't show at the end of the table and it inserted before other row. Why is this?
Firebird doesn't use clustered tables, nor does it order data by insert. When you insert a row, Firebird adds the row to the first datapage of the table it can find that has sufficient space available. As a result, it may end up anywhere relative to other rows of the table.
If you want to enforce a specific order to the data you view, you need to add an order by clause to your select statement, specifying by which columns you want to order the data. Without an order by, the order of the result of a select is not deterministic, and may be the result of physical order in data pages, order of data in an index used to find rows, and other factors.
Posting two questions:
1.
Let's say there is a query:
SELECT C1, C2, C3 from TABLE;
When this query is fired for the first time,it retrieves all the values in a certain order.
Next time, when the same query is fired, will the previous order be retained?
There are 2 tables, TABLE1 and TABLE2, both of them have identical data.
Will (SELECT * from TABLE1) and (SELECT * from TABLE1) retrieve the same order of rows?
SQL tables represent unordered sets. Period. There is no ordering in a result set unless you explicitly include an ORDER BY.
It is that simple. If you want data in a particular order, then you need to use ORDER BY. That is how relational databases work.
The same query can return results in different orders each time the query is executed. There are no guarantees about the order -- unless the query has an ORDER BY for the outermost SELECT.
No, unless you are fetching data from result cache!
No, unless they are very small tables and your query runs with low parallelism.
Sorry for extra answer, but I see Tim claims that the query will return same result as long as the underlying table(s) is not modified, and the query has same execution plan.
Snowflake executes the queries in parallel, therefore the order of data is not predictable unless ORDER BY is used.
Let's create a table (big enough to be processed in parallel), and run a simple test case:
-- running on medium warehouse
create or replace table my_test_table ( id number, name varchar ) as
select seq4(), 'gokhan' || seq4() from table(generator(rowcount=>1000000000));
alter session set USE_CACHED_RESULT = false;
select * from my_test_table limit 10;
You will see that it will return different rows every time you run.
To answer both questions short: No.
If your query has no ORDER BY-clause, the SELECT statement always returns an unordered set. This means: Even if you query the same table twice and the data didnt change, SELECT without ORDER BY can retrieve different row-orders.
https://docs.snowflake.com/en/sql-reference/sql/select.html
Does the sequence of records in SELECT QUERY in result always the same?
I mean that if the first result of operation returns the following sequence:
first record
second record
third record
All other select * from t queries always return records in the same sequence.
A SQL query -- like a SQL table -- represents an unordered set. There is no ordering, unless an ORDER BY is present for the outermost SELECT.
As an unordered set, the same query can return results in a different order each time it is run.
So, if you want results in a particular order, use ORDER BY.
I should add that if multiple rows have the same key, then these rows can appear in any order, even with an ORDER BY. In general, you should ensure that the keys in the ORDER BY uniquely define each row (say by including the primary key as the final key).
I have a table and I want to update this table using SQL query statement.
How can i write a statement to update the 3rd row for example?
What I mean is that I want to make update to a certain row according to its appearance sequence in the table.
3rd row
When ordered by what? SQL databases make no guarantee to the order of records in any given query unless explicitly given an ORDER BY clause.
Identify the row you want to update using a WHERE clause. For example:
UPDATE SomeTable SET SomeColumn = 'Some Value' WHERE AnotherColumn = 'Another Value'
You can chain together lots of boolean logic into that WHERE clause to create more complex ways of identifying the record(s) you want to update. But the point is that you have to identify the record. "The 3rd row" doesn't mean anything to SQL.
Once you do have that ORDER BY clause, you can perhaps do something a little more complex. For example, sort the records in a sub-query and use an identifier from that query. So you might get "the 3rd row" like this:
SELECT ID FROM SomeTable ORDER BY ID OFFSET 2 LIMIT 1
Then use that in the UPDATE:
UPDATE SomeTable SET SomeColumn = 'Some Value'
WHERE ID IN (SELECT ID FROM SomeTable ORDER BY ID OFFSET 2 LIMIT 1)
(assuming MySQL, since you didn't specify, but other RDBMS engines have similar capabilities)
I don't know which DBMS you're using. If you're using Oracle DB there are pseudocolumns ROWID and ROWNUM you could use for this purpose.
I have a table called points. I executed the following query and expected a list of lexicographicaly sorted list of ROWIDs but that did not happen. How does Order by rowid sorts the row?
select rowid from points order by rowid
I had rows like following
AAAE6MAAFAAABiSAAA
AAAE6MAAFAAABi+AAA
2nd row is lexicographicaly smaller than first row. So what is sorting criteria if it is not lecxicographical sorting?
Why you see is only a representation used for display purposes.
The actual rowid contains binary information about the data block, the row in the block, the file where the block is located and the internal object id of the table (See the manual for details)
When you use order by rowid Oracle sorts the rows based on that (internal) information, not based on the "string representation".
If you change your query to:
select rowid,
dbms_rowid.rowid_relative_fno(rowid) as rel_fno,
dbms_rowid.rowid_row_number(rowid) as row_num,
dbms_rowid.rowid_block_number(rowid) as block_num,
dbms_rowid.rowid_object(rowid)
from points
order by rowid
You will most probably see the logic behind the ordering of the rownumber.
Note that the value for dbms_rowid.rowid_object() will always be the same. And if you only have two rows in your table, both will most probably also have the same value for rowid_block_number()
The sequence of rowid is not guranteed. It depends on how you have set the NLS settings. Also rowid represents the physical allocation of the row in the database. A rowid is considered immutable(does not change) but if you delete a row and insert it again then it changes.
If you delete a row, then Oracle may reassign its rowid to a new row
inserted later.