- 利用 T-SQL 語法
-- 授予
GRANT ALTER TRACE TO UserName;
-- 拒絕
DENY ALTER TRACE TO UserName;
- SSMS => 安全性 => 登入 => 使用者 => 右鍵屬性 => 安全性實體 => 改變追蹤 => 勾選拒絕 checkbox。
- 利用 SQL Profile 連進不是 sysadmin 或沒有 ALTER TRACE 權限的 SQL Server 會收到此錯誤訊息。
-- 授予
GRANT ALTER TRACE TO UserName;
-- 拒絕
DENY ALTER TRACE TO UserName;
Categories
---------------------------
food/biscuits/britania
food/milk
food/baby/icecream
food/baby/milk powder
detergents/liquids/handwash
id name parent
--- ----------- -------
1 detergents null
2 food null
3 liquids 1
4 baby 2
5 biscuits 2
6 milk 2
7 handwash 3
8 icecream 4
9 milk powder 4
10 britania 5
Jacob
Jess
Steve
John
Bob
Michael
Paul
Rui
Smith
Lana
Johnson
Bobbey
Mic
Stev
Jacobson
Steffi
Paulson
Bracha
Jessica
he challenge is to write a query that can take a Manager Name as a parameter and list all the employees reporting to that manager, directly or indirectly. If the parameter contains “Smith” the query should return:Smith
Lana
Johnson
If the parameter passed is ‘Jacob’, the query should return:Jacob
Jess
Steve
John
Bob
Michael
Paul
Your query should ideally look like the following:DECLARE @manager VARCHAR(20)
SELECT @manager = 'Smith'
-- Your query here:
This query is pretty easy to write using a recursive CTE. To make the query complex, so that it will meet the complexity level expected for the ‘TSQL Challenges’ series, I am adding the following restriction.{ database_name| @database_name_var }
這是要備份交易記錄、部分資料庫或完整資料庫的來源資料庫。 如果這個名稱是以變數 (@database_name_var) 的形式提供,則除了 ntext 或 text 資料類型之外,您可以將這個名稱指定為字串常數 (@database_name_var=database name),或指定為字元字串資料類型的變數。
TO backup_device [ ,...n]
指出隨附的備份裝置集合是未鏡像的媒體集,或是鏡像媒體集內的第一個鏡像 (如果宣告了一個或多個 MIRROR TO 子句)。
- backup_device:指定備份作業要使用的邏輯或實體備份裝置。
- { logical_device_name | @logical_device_name_var }
這是用來備份資料庫之備份裝置的邏輯名稱。邏輯名稱必須遵照識別碼的規則。如果備份裝置名稱是以變數 (@logical_device_name_var) 的方式來提供,則除了 ntext 或 text 資料類型之外,您可以將這個名稱指定為字串常數 (@logical_device_name_var = 邏輯備份裝置名稱),或指定為字元字串資料類型的變數。- { DISK | TAPE } = { 'physical_device_name' | @physical_device_name_var }
指定磁碟檔案或磁帶裝置。在 BACKUP 陳述式內指定磁碟裝置之前,該裝置不需要存在。 如果實體裝置存在,且 BACKUP 陳述式並未指定 INIT 選項,就會將備份附加至裝置中。
- n:這是一個預留位置,表示可以在逗號分隔清單中指定最多達 64 個備份裝置。
DECLARE @DBName AS nvarchar(100)
DECLARE @bakPath AS nvarchar(4000)
SET @DBName = N'AdventureWorks'
SET @bakPath = N'C:\BackupVariable' + @DBName + '.bak'
BACKUP DATABASE @DBName TO DISK = @bakPath
ID HtmlText -- ------------------------------------------------------------------------ 1 <html><body><font>This is challenge #18</Font><font></Font></Body></Html>
ID TagNamesOccurance -- -------------------------------------------------------------- 1 Body(Found: 1 time), Font(Found: 2 times), Html(Found: 1 time)
DECLARE @STD TABLE (STDNO char(3),STDName nchar(10))
INSERT INTO @STD VALUES('001',N'張三')
INSERT INTO @STD VALUES('002',N'李四')
INSERT INTO @STD VALUES('003',N'王五')
DECLARE @Books TABLE (ID int,STDNO char(3) , Kind char(10) , BookName char(30))
INSERT INTO @Books VALUES(1,'001','SQL','SQL Server T-SQL')
INSERT INTO @Books VALUES(2,'001','SQL','SQL Server Turning')
INSERT INTO @Books VALUES(3,'001','SQL','SQL Server Statistics')
INSERT INTO @Books VALUES(4,'003','Win','Windows Server')
INSERT INTO @Books VALUES(5,'003','SQL','SQL Server T-SQL')
INSERT INTO @Books VALUES(6,'003','Win','HyperV')
The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. The actual physical execution of the statement is determined by the query processor and the order might vary from this list.
- FROM
- ON
- JOIN
- WHERE
- GROUP BY
- WITH CUBE 或 WITH ROLLUP
- HAVING
- SELECT
- DISTINCT
- ORDER BY
- TOP
The preceding sequence is usually true. However, there are uncommon cases where the sequence might differ.For example, suppose you have a clustered index on a view, and the view excludes some table rows, and the view's SELECT column list uses a CONVERT that changes a data type from varchar to integer. In this situation, the CONVERT can execute before the WHERE clause executes. Uncommon indeed. Often there is a way to modify your view to avoid the different sequence, if it matters in your case.
-- 簡易範例 1
SELECT STDNO AS 學生編號 , STDName AS 學生姓名
FROM @STD
ORDER BY 學生編號 DESC -- 利用欄位別名來排序
ORDER BY 的順序在 SELECT 之後,因此 ORDER BY 可以指定 SELECT 的欄位別名,當成排序的欄位選擇。-- 簡易範例 2
SELECT STDNO AS 學生編號 , STDName AS 學生姓名
FROM @STD
WHERE 學生編號 = '001' -- 會出現此錯誤訊息"無效的資料行名稱 '學生編號'"。
WHERE 的邏輯順序,因為在 SELECT 之前,所以 WHERE 條件內不可以指定欄位別名來當成條件There can be predicates that involve only one of the joined tables in the ON clause. Such predicates also can be in the WHERE clause in the query. Although the placement of such predicates doesn't make a difference for INNER joins, they might cause a different result when OUTER joins are involved. This is because the predicates in the ON clause are applied to the table before the join, whereas the WHERE clause is semantically applied to the result of the join.
-- WHERE 內條件判斷
SELECT
A.STDNO AS 學生編號 ,
COUNT(B.BookName) AS 書籍數量
FROM @STD AS A
LEFT JOIN @Books AS B ON A.STDNO = B.STDNO
WHERE B.Kind = 'SQL'
GROUP BY A.STDNO
-- ON 內條件判斷
SELECT
A.STDNO AS 學生編號 ,
COUNT(B.BookName) AS 書籍數量
FROM @STD AS A
LEFT JOIN @Books AS B ON A.STDNO = B.STDNO AND B.Kind = 'SQL'
GROUP BY A.STDNO
在 WHERE 內判斷,是在 LEFT JOIN 後產生結果後(下圖),再對其結果(下圖)進行條件判斷
Guid ------------------------------------ 7FC61235-F252-4E32-BD31-99FB1FC8A574 D031BC00-ADD3-4C40-BE24-A613586FD42C 81B10B95-5A22-4D3A-949A-FF268103A554 ABCDE12345E
ValidGuid 1 2 3 4 5 A B C D E Sum ------------------------------------ - - - - - - - - - - ----- 7FC61235-F252-4E32-BD31-99FB1FC8A574 3 4 3 2 3 1 2 2 1 1 22 D031BC00-ADD3-4C40-BE24-A613586FD42C 2 2 3 4 1 2 2 3 4 1 24 Vertical Sum 5 6 6 6 4 3 4 5 5 2 46