讀取資料表的資料 (教學課程)
使用 SELECT 陳述式來讀取資料表的資料。Transact-SQL 陳述式中最重要的其中一個陳述式就是 SELECT 陳述式,而其中有很多的語法變化。在本教學課程中,您將使用五種簡單的變化樣式。
讀取資料表的資料
輸入並執行下列陳述式,以讀取 Products 資料表的資料。
-- The basic syntax for reading data from a single table SELECT ProductID, ProductName, Price, ProductDescription FROM dbo.Products GO
您可以使用星號,選取資料表中的所有資料行。這個方法常在特定查詢中使用。您應該在固定程式碼中提供資料行清單,使陳述式會傳回預期的資料行,即使以後加入新資料行還是一樣。
-- Returns all columns in the table -- Does not use the optional schema, dbo SELECT * FROM Products GO
您可以省略不要傳回的資料行。而且會以資料行所列出的順序來傳回資料行。
-- Returns only two of the columns from the table SELECT ProductName, Price FROM dbo.Products GO
使用 WHERE 子句,限制要傳回給使用者的資料列。
-- Returns only two of the records in the table SELECT ProductID, ProductName, Price, ProductDescription FROM dbo.Products WHERE ProductID < 60 GO
您可以處理資料行中所傳回的值。下列範例會在 Price 資料行上進行數學運算。除非使用 AS 關鍵字提供名稱,否則以這種方式變更的資料行將不會有名稱。
-- Returns ProductName and the Price including a 7% tax -- Provides the name CustomerPays for the calculated column SELECT ProductName, Price * 1.07 AS CustomerPays FROM dbo.Products GO
函數對 SELECT 陳述式相當有用
如需有關某些可在 SELECT 陳述式中用來處理資料之函數的詳細資訊,請參閱下列主題: