Previous Table of Contents Next


How to Insert Each Type of Data

Data types are inserted in different ways. Table 9.2 describeshow to insert each data type and what to watch out for.

Table 9.2. How to insert different datatypes into a table.

Data Type Example Use Quotes? Comments

varbinary values(0xa02d21a5) No Binary values accept hexadecimal notation.
binary values(0x1a) No Binary columns are used to store byte-pairs.
varchar values(“David”) Yes
char values(“John’s Book”) Yes Character values are specified in quotes. To use a quote in acharacter string, use the other quote type to contain the string.You can use single or double quotes interchangeably.
smalldatetime
datetime values(“01/01/96 3:45 PM”) Yes Specifying a date without a time defaults the time to midnight. Specifying a timewithout a date defaults the date to January 1, 1900. You may usemany different date formats when inserting dates; only one of themis shown here.
smallmoney
money values($355000.16) No Do not use quotes. Do not use commas to demarcate the thousandsposition, because the server will interpret your comma as a columndelimiter. To indicate that your value is money, and not a floatingpoint data type, always use the $ symbol.
Bit values(0) No The possible values for a bit field are 0 and1.
Int values(34038) No
smallint values(-320) No
tinyint values(25)МNo
numeric values(-23.002) No
decimal values(123.1) No
float values(123.1) No
real values(1121.22) No

SELECT INTO and INSERT… SELECT

There are two useful commands that will copy data from one tableto another. The first, SELECT INTO, creates a new tablebased on rows in an existing table.

select *
into CA_authors
from authors
where state  =  ‘CA’
(15 row(s) affected)

All authors in the authors table who live in Californiawere copied to a new table, CA_authors.

You may select specific columns using SELECT INTO tocreate a table with fewer fields than the original or join tablestogether. Virtually any query that is possible with SELECTis possible with SELECT INTO:

select a.au_lname, t.title, t.price
into   whowrotewhat
from   authors a join titleauthor ta on a.au_id = ta.au_id
       join titles t on t.title_id = ta.title_id


NOTE:  SELECT INTO is a nonlogged operation. Itrequires that a special option, SELECT INTO/BULKCOPY, beset on in the database.


WARNING:  After performing a SELECT INTO ora BULKCOPY operation (usually with the bcp utility in\mssql\binn) you will not be able to perform anincremental backup of the database until you first perform a fullbackup.

If you are using incremental backups on your database, youprobably should not enable the SELECT INTO/BULKCOPYoption in that database.


INSERT… SELECT is a type of INSERTstatement. You specify a regular INSERT statement, but donot specify a values clause. Instead, you provide a query thatreturns values to be inserted. INSERT... SELECT willselect rows from an existing table and insert those rows in anotherexisting table. If you had created the California authors table andwanted to also add in the authors from Maryland, this query woulddo the trick:

insert CA_authors
select * from  authors
where state =  ‘MD’
(2 row(s) affected)

This query copies rows between two existing tables.

You can select specific columns by restricting the column list(the SELECT list) and specific rows by restricting the WHERE list.


TIP:  If you want to copy all rows in your table back toitself, for instance, or if you wanted to create a lot of test datavery quickly, you could perform an INSERT… SELECT onthe same table:
insert testdata
select * from testdata


NOTE:  Differences between INSERT... SELECT and SELECT... INTO

INSERT... SELECT is a logged operation; SELECT...INTO is nonlogged. SELECT... INTO creates a new tableout of the selected data. INSERT... SELECT inserts datainto an existing table.



Previous Table of Contents Next
Используются технологии uCoz