Copying data from one table to the other
| There are several ways to copy or move data from one table to another. Some methods are applicable when copying data from one table to another on the same database and other methods are applicable for copying data from one database to another and even or a different server. Bellow are several methods used to copy data from from one storage location to another:
- Using a select statement
- bulk copy in and out of a database
- backing up and reloading
Copying data using SQL select statement
To copy data from one table to another, we use the select statement. Bellow is the general syntax for copying data from one table to an already existing table:
insert into destination_table(field1,field2,field3,...)
select field1,field2,field3,...
from source_table;
However, if the destination table does not already exist, there is no need to first create it and then copy the data,you could simply run the following query:
create table new_table
select field1,field2,field3
from orriginal_table;
Moving data using bulk copy
The general syntax for using the bcp command is as follows:
bcp {[[database_name.][schema].]{table_name | view_name} | "query"}
{in | out | queryout | format} data_file
[-m max_errors] [-f format_file] [-x] [-e err_file]
[-F first_row] [-L last_row] [-b batch_size]
[-n] [-c] [-N] [-w] [-V (70 | 80 | 90 )]
[-q] [-C { ACP | OEM | RAW | code_page } ] [-t field_term]
[-r row_term] [-i input_file] [-o output_file] [-a packet_size]
[-S server_name[\instance_name]] [-U login_id] [-P password]
[-T] [-v] [-R] [-k] [-E] [-h"hint [,...n]"]
Arguments
database_name
Is the name of the database in which the specified table or view resides. If not specified, this is the default database for the user.
owner
Is the name of the owner of the table or view. owner is optional if the user performing the operation owns the specified table or view. If owner is not specified and the user performing the operation does not own the specified table or view, SQL Server returns an error message, and the operation is canceled.
table_name
Is the name of the destination table when importing data into SQL Server (in), and the source table when exporting data from SQL Server (out).
view_name
Is the name of the destination view when copying data into SQL Server (in), and the source view when copying data from SQL Server (out). Only views in which all columns refer to the same table can be used as destination views. For more information on the restrictions for copying data into views, see INSERT (Transact-SQL).
query
Is a Transact-SQL query that returns a result set. If the query returns multiple result sets, such as a SELECT statement that specifies a COMPUTE clause, only the first result set is copied to the data file; subsequent result sets are ignored. Use double quotation marks around the query and single quotation marks around anything embedded in the query. queryout must also be specified when bulk copying data from a query.
The query can reference a stored procedure as long as all tables referenced inside the stored procedure exist prior to executing the bcp statement. For example, if the stored procedure generates a temp table, the bcp statement fails because the temp table is available only at run time and not at statement execution time. In this case, consider inserting the results of the stored procedure into a table and then use bcp to copy the data from the table into a data file.
in | out| queryout | format
Specifies the direction of the bulk copy, as follows:
in copies from a file into the database table or view.
out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string.
queryout copies from a query and must be specified only when bulk copying data from a query.
format creates a format file based on the option specified (-n, -c, -w, or -N) and the table or view delimiters. When bulk copying data, the bcp command can refer to a format file, which saves you from re-entering format information interactively. The format option requires the -f option; creating an XML format file, also requires the -x option. For more information, see Creating a Format File.
data_file
Is the full path of the data file. When data is bulk imported into SQL Server, the data file contains the data to be copied into the specified table or view. When data is bulk exported from SQL Server, the data file contains the data copied from the table or view. The path can have from 1 through 255 characters. The data file can contain a maximum of 263 - 1 rows.
|
RELATED ARTICLES: Common SQL Data types Now that you have identified your data entities or tables, you have to define each attribute of the tables you have identified. These definitions include the name of the attribute and the type of data that can be stored in the field. Most relational database systems have a number of data types that
Selecting data from multiple tables So far we have covered methods of retrieving data from single table. In real life however, you hardly get to only retrieve data from one table. In other words, to retrieve and interpret information, you normally get such information from different tables each of which will contain only information a
Updating and deleting data with SQL In the posting “Introduction to SQL”, we covered the creation of data storage space or database and its entities. We then went a little further to cover a methods of entering data into those entities or tables and methods of retrieval.
There are however, times when the data stored in
The state of Periodic table elements - In addition to the group 8 or O elements, the following elements also naturally occur in gas state:
H, N, O, F, Cl and Br
-Among metals and non-metals, the following occurs in liquid state:
Na, K, Rb, Cs, Fr, Hg a
breaking code periodic table
What the cash flow statement tell us The cash flow statement is a financial document that shows how changes in balance sheet and income statements affect cash and cash equivalents. As an analytical tool, the cash flow statement is useful in determining the short-te
Java variables and data types A variable is defined as a named piece of memory that a java program uses to store data. Each of these storage pieces can only store data of a certain type. In other words, if you defined as variable of type integer, that variable can only hold inte
The Anatomy of an IF statement In programming, there are cases when you need the program to perform a certain function when a certain condition is met. If such conditions are met then a certain piece of code is executed, otherwise skip it and execute an alternative code. This is ac
Joining a table with itself (SQL self-join) Joining a table with itself is sometimes the only way to make sense of information. Consider a scenario where everybody's information in a company including managers and ordinary employees. That same table contains information on who reports to who?
Breaking out of loops with break statement div class='floatleft'> So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero
| |
|