SQL總結(三)其餘查詢

SQL總結(三)其餘查詢

 其餘經常使用的SQL,在這裏集合。數據庫

一、SELECT INTO

從一個表中選取數據,而後把數據插入另外一個表中。經常使用於建立表的備份或者用於對記錄進行存檔。express

語法:架構

SELECT column_name(s)
INTO new_table_name [IN externaldatabase] 
FROM old_tablename

IN 子句可用於向另外一個數據庫中拷貝表。函數

1)備份表信息spa

SELECT ID,Name 
INTO Students_Backup
FROM Students

2)複製到備份庫code

SELECT *
INTO Students IN 'Backup.mdb'
FROM Students

二、IDENTITY 

做用:建立惟一的,遞增的列orm

注意:一張表中只能有一列爲IDENTITYxml

1)建立學生信息表時,指定其ID爲自增列,從1開始,每次遞增1對象

IF OBJECT_ID (N'Students', N'U') IS NOT NULL
    DROP TABLE Students;
GO
--學生信息表
CREATE TABLE Students(
ID int primary key IDENTITY(1,1) not null,
Name nvarchar(50),
Age int,
Sex bit,
City nvarchar(50),
MajorID int
)

 

2)若是指定了自增列,又須要插入指定ID的值,須要中止INDENTIY,執行後再開啓。blog

SET IDENTITY_Insert Students ON 
insert Students(ID,Name,Age,City) values(10,'Jim',18,'NewYank') 
SET IDENTITY_Insert Students OFF 

3)與SELECT INTO 合用,插入行號

這個經常用於臨時表分頁時使用。

注意:若是查詢的列中有自增列,須要將其刪除,或者屏蔽,由於一張表中只有一個IDENTITY字段。

SELECT IDENTITY(int,1,1) AS RowNumber,Name,Age,Sex,City
INTO Students_Backup
FROM Students

SCOPE_IDENTITY() 

還有一個經常使用的函數,與此一塊兒使用 SCOPE_IDENTITY()

經常在有IDENTITY列的插入時,須要返回當前的行的IDENTITY的列值。

如: 

IF OBJECT_ID (N'Students', N'U') IS NOT NULL
    DROP TABLE Students;
GO
--學生信息表
CREATE TABLE Students(
    ID int IDENTITY(1,1) PRIMARY KEY not null,
    Name nvarchar(50),
    Age int,
    Sex bit,
    City nvarchar(50),
    MajorID int
)

INSERT INTO Students(Name,Age,Sex,City,MajorID) VALUES('Jim',18,0,'ShangHai',12)
SELECT SCOPE_IDENTITY()

 

這個例子,每次返回插入記錄的ID的值。若是有其餘關聯表用到此ID,這時就不用再從數據庫查一遍了。 

三、OBJECT_ID 

返回架構範圍內對象的數據庫對象標識號。

1)查詢表是否存在

SELECT OBJECT_ID(N'Students',N'U')

與如下語句等價:

SELECT id FROM sysobjects WHERE name=N'Students' and type=N'U'

 

2)經常用於建立表、視圖時,作斷定。保證腳本的重複執行

建立學生信息表時,須要斷定該表是否存在,若是存在則刪除

IF OBJECT_ID (N'Students', N'U') IS NOT NULL
    DROP TABLE Students;
GO
--學生信息表
CREATE TABLE Students(
ID int primary key IDENTITY(1,1) not null,
Name nvarchar(50),
Age int,
Sex bit,
City nvarchar(50),
MajorID int
)

 

 四、跨庫執行

 

若是系統須要多個數據庫,當執行跨庫腳本,無需再次進行鏈接,能夠執行以下腳本

 

實例:當前在master庫,查詢TestDB庫的Students表信息:

 

USE master

SELECT * FROM TestDB..Students

 

 

經常使用函數

一、LEN函數

計算字段值的長度

SELECT LEN(Name) AS NameLength FROM Students

二、FORMAT函數

FORMAT 函數用於對字段的顯示進行格式化。

語法:

SELECT FORMAT(column_name,format) FROM table_name

1)時間格式化

SELECT FORMAT(GETDATE(),'yyyy-MM-dd') 

結果:2014-05-13

三、CAST函數

一、實例:將價格轉爲整型

SELECT CAST(Price AS smallint) AS CPrice FROM Orders

結果:10

二、將字段解析爲XML

SELECT CAST(Scheme AS xml) AS CPrice FROM Orders

結果:

<xml>
  <ProductID>101</ProductID>
  <ProductName>Card</ProductName>
</xml>

 四、CONVERT

語法:

 CONVERT(data_type,expression[,style])

說明:
此樣式通常在時間類型(datetime,smalldatetime)與字符串類型(nchar,nvarchar,char,varchar)
相互轉換的時候纔用到.

tyle數字在轉換時間時的含義以下:

------------------------------------------------------------------------------------------------------------
Style(2位表示年份)         |  Style(4位表示年份)   |   輸入輸出格式                                    
------------------------------------------------------------------------------------------------------------
0                               | 100                           |   mon dd yyyy hh:miAM(或PM)              
------------------------------------------------------------------------------------------------------------
1                               |  101   美國                |   mm/dd/yy                                       
------------------------------------------------------------------------------------------------------------
2                               |  102    ANSI               |   yy-mm-dd                                        
------------------------------------------------------------------------------------------------------------
3                               |  103    英法                |   dd/mm/yy                                       
------------------------------------------------------------------------------------------------------------
4                               |  104    德國                |   dd.mm.yy                                        
------------------------------------------------------------------------------------------------------------
5                               |  105    意大利             |   dd-mm-yy                                        
------------------------------------------------------------------------------------------------------------
6                               |  106                            |   dd mon yy                                        
------------------------------------------------------------------------------------------------------------
7                               |  107                            |   mon dd,yy                                        
------------------------------------------------------------------------------------------------------------
8                               |  108                            |   hh:mm:ss                                         
------------------------------------------------------------------------------------------------------------
9                               |  109                            |   mon dd yyyy hh:mi:ss:mmmmAM(或PM)
------------------------------------------------------------------------------------------------------------
10                             |  110    美國                 |   mm-dd-yy                                         
------------------------------------------------------------------------------------------------------------
11                             |  111    日本                 |   yy/mm/dd                                        
------------------------------------------------------------------------------------------------------------
12                             |  112    ISO                  |   yymmdd                                           
------------------------------------------------------------------------------------------------------------
13                             |  113     歐洲默認值     |   dd mon yyyy hh:mi:ss:mmm(24小時制)  
------------------------------------------------------------------------------------------------------------
14                             |  114                            |   hh:mi:ss:mmm(24小時制)                    
------------------------------------------------------------------------------------------------------------
20                             |  120     ODBC 規範     |    yyyy-mm-dd hh:mi:ss(24小時制)         
------------------------------------------------------------------------------------------------------------
21                             |   121                           |    yyyy-mm-dd hh:mi:ss:mmm(24小時制) 
------------------------------------------------------------------------------------------------------------

1)實例:時間轉換爲指定形式

SELECT CONVERT(NVARCHAR(20),GETDATE(),120) 

結果:2014-05-13 23:49:34

2)實例轉爲XML格式

SELECT CONVERT(xml,Scheme) FROM Orders

結果:

<xml>
  <ProductID>101</ProductID>
  <ProductName>Card</ProductName>
</xml>

   

所有腳本

SELECT UCASE(Name) FROM Students
SELECT UCASE(LastName) as LastName,FirstName FROM Persons





SELECT GETDATE() --2014-05-13 23:15:36.130

SELECT FORMAT(GETDATE(),'yyyy-MM-dd')  --2014-05-13

IF OBJECT_ID('Orders','U') IS NOT NULL
DROP TABLE Orders
CREATE TABLE Orders
(
    ID bigint primary key not null,
    ProductID int,
    ProductName nvarchar(50),
    Price float,
    Scheme text,
    Created datetime default(getdate())
)

INSERT INTO Orders(ID,ProductID,ProductName,Price,Scheme) 
VALUES(201405130001,101,'Card',10.899,'<xml><ProductID>101</ProductID><ProductName>Card</ProductName></xml>')


SELECT LEN(ProductName) AS NameLength FROM Orders    --4

SELECT FORMAT(Created,'yyyy-MM-dd') AS FormatDate FROM Orders 

--2014-05-13

SELECT ROUND(Price,2) FROM Orders   --10.9

SELECT CAST(Price AS smallint) AS CPrice FROM Orders     
--10

SELECT CAST(Scheme AS xml) AS CPrice FROM Orders

--CONVERT
SELECT CONVERT(NVARCHAR(20),GETDATE(),120) --2014-05-13 23:49:34
SELECT CONVERT(xml,Scheme) FROM Orders
/* 結果:
<xml>
  <ProductID>101</ProductID>
  <ProductName>Card</ProductName>
</xml>
*/
相關文章
相關標籤/搜索