Exporting table in a specific SQL database to a new database - sql

So I have a table in SQL that's in a database. I've performed sort operations as well as catching a certain amount of records, and now my objective is to input these values into a new database. How can I do this, considering the sort operations? Would it be potentially easier to export the whole table then perform the sort in the new database? I am using SQL 2005.

It doesn't matter what order you insert the data into the new table. If you put a clustered index on the table, the data will be stored in the order of the index, regardless of the order it was inserted. (And note that storing it in this order is no guarantee that your query results will come out in this order)
The only instance where the order of inserts might matter is if you have an IDENTITY column on the table, and are allowing it to auto-populate. Then the IDENTITY will increment in the order that the rows are inserted.
That said, treating your question as academic, if you did want to export data in a certain order from one database to another, you could use an SSIS dataflow and specify an ORDER BY in the query in the Source component.

Related

SQL for ordering the actual data in the database table

Fairly new to SQL. You can order records returned with something like:
SELECT * FROM MyTable ORDER BY SalesPrice;
Without physical extracting all records, clearing the table, and then re-writing them back in some customised order, is there a way to ask the database driver to re-order the physical data in the database table by some important column or key (e.g. "SalesPrice")?
This would not be desirable for tables with many records or where the data changes regularly, but may make more sense as a maintainenace action where the table is relatively static.
Using DAO and ODBC, though I'm hoping for a general answer that might apply to a variety of databases.
SQL tables represent unordered sets. Period. Well, there is a nuance to that, which are clustered indexes. So you can arrange to have the data stored in a particular order. However that does not guarantee that select * from table will return the results in that order.
If you are concerned about the performance of such a query, you wnat an index on (salesprice). Then use order by salesprice in the query.

Oracle SQL - Order data stored in a database

I was wondering if anyone knows if there is a way to sort the data that already resides in a database. That is, I want to sort what is there but NOT retrieve it in in query.
I am asking because I have a list of things in this database, that I would like to add to in future and would like to order it once I've added them.
So what I mean is, I would like to not have to download all the data; sort it; then put it back onto the database.
Thanks in advance.
If there is only one column in your table then this is fine and you can simply sort the data in that table.
But if there are more than one columns in your table then it would be dependent on the other columns as well(ie, you need to specify which column you are looking to sort.) Also if there is a primary key attached to the table then its not possible as primary key would be in ascending order by default. In that case you can only have it while selecting the data from the table.
(My suggestion is to sort the data while selecting your records from the table as that would be easy and will have less risk)
EDIT:
To make my point straight and clear, the best way to achieve what you are trying to simply use ORDER BY ASC or ORDER BY DESC while you are selecting the data from your table.
If you are creating a new table then you can create index organized table to ensure that the data is stored ordered by index.
I was wondering if anyone knows if there is a way to sort the data that already resides in a database.
Normal relational tables, called as heap-organized tables, store rows in any order i.e. unsorted. You sort the rows if required only when you fetch them, not when you store them. And the only way to guarantee the sorting while retrieving the rows is to use an ORDER BY clause.

How can I change the order of the rows in an Oracle Database? (Order By not an option)

How can I reorder rows in oracle sql database? I am using a 3rd party database-driven software and unfortunately I cannot change the call (or I would just add an order by), but can I change the order of the rows in the database?
Disclaimer: I know I should never depend on raw database order, and if it changes I understand, but can this be done?
Thank you!
You can't. If a query doesn't specify an ORDER BY, the order in which rows are returned is undefined.
If you are willing to accept a less-than-100% solution, you could try moving the data to a temporary table, truncate the table, and then insert the data back in the order you want it to appear. If the query is doing a table scan or some type of index scans and not doing anything complicated (like a join), it's likely that the rows would be returned in the order they are physically stored in the table. No guarantees, of course, but it might work most of the time.

Computed column that uses row_number() or dense_rank() over a partition

This question is related to a previous question,
Grouping by or iterating through partitions in SQL
I need to support SQL Server 2005, 2008, 2008 R2, and cannot depend on the Enterprise or full version (must work in SQL Server Express).
What I am trying to do is create a user-defined computed column that is essentially either a row_number() or dense_rank() over a partition by clause. This needs to act like an index, in that whenever rows are added to the table, this user-defined column is automatically generated.
I looked over the following Microsoft link that explains how to create a column based on a function, http://msdn.microsoft.com/en-us/library/ms186755.aspx. It doesn't quite get there.
It may not be possible, especially without the full version of SQL Server. Any thoughts?
Main feature of partitioning is splitting single object into multiple relatively independent database objects. Partitioning allows:
switch partitions from one table to another immediately, because it becomes a meta data operation, rather than physical copy.
lock single partition instead of the whole table, thus giving several processes simultaneous independent access to each partition.
spread a single table over multiple db files, storing each partition in its own file
compress data and change some other properties for each partition individually.
It cannot be done by any other means.
So I think you are looking to a way to assign an ID to each combination of some key, which consists of multiple columns. There is a number of ways to do it, depending on the result you want to achieve:
Create an index on several columns. It may be clustered, it may be on primary key columns. It will aid searches and get you data sorted (in case of clustered index).
Create separate table with all your primary key columns and assign each combination a unique key (eg using IDENTITY property). Insert this key to your main table to use as "partition id". Insert values automatically using a trigger or a join.
Please note, that SQL Server can use only one column for partitioning.

SQL Server table is sorted by default

I have simple SSIS package where I import data from flat file into SQL Server table (SQL Server 005). File contains 70k rows and table has no primary key. Importing is sucessful but when I open SQL Server table the order of rows is different from the that of file. After observing closely I see that data in table is sorted by default by first column. Why this is happening? and how I can avoid default sort?
Thanks.
You cannot rely on ordering unless you specify order by in your SQL query. SQL is a relational algebra that works with sets. Those sets are unordered. Database tables do not have an intrinsic ordering.
It may well be that the sets are ordered due to the way the data is retrieved from the tables. This may be based on primary key, order of insertion, clustered key, seemingly random order based on the execution plan of the query or the actual data in the table or even the phase of the moon.
Bottom line, if you want a specific order, use order by. If you don't want a specific order, the DBMS is free to deliver your rows in any order, including one based on the first column.
If you really want them sorted depending on the position in the import file, you should add another column to the table to store an increasing number based on its position in that file. Then use order by using that column. But that's a pretty arbitrary sort order, you're generally better off choosing one that makes more sense to the data (transaction ID, date/time, customer number or whatever else you have).
If you want to avoid the default sort (however variable that may be), use a specific sort.
In general no order is applied if there is no ordering in the select query.
What I have noticed is that the table results might return in the order of the primary key, but this is not gaurenteed either.
So all in all, if you do not specify a ordering, no ordering can be assumed.