USE AdventureWorks2008R2
--------------------
USE AdventureWorks2008R2
------------------------------------------------------------------------------------------------------------------------
SELECT TOP 5 *
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY RAND()) PAGE_ROW_NUMBER, *
    FROM (
        SELECT tc.* FROM TC_CarIdentify AS tc WHERE 1 = 1
    ) AS PAGE_TABLE_ALIAS
) AS PAGE_TABLE_ALIAS
WHERE PAGE_ROW_NUMBER > 0 ORDER BY PAGE_ROW_NUMBER
--------------------
SELECT TOP 5 *
FROM (
	SELECT ROW_NUMBER() OVER (ORDER BY RAND()) AS PAGE_ROW_NUMBER, *
	FROM (
		SELECT tc.*
		FROM TC_CarIdentify tc
		WHERE 1 = 1
	) PAGE_TABLE_ALIAS
) PAGE_TABLE_ALIAS
WHERE PAGE_ROW_NUMBER > 0
ORDER BY PAGE_ROW_NUMBER
------------------------------------------------------------------------------------------------------------------------
ALTER TABLE [dbo].[sys_config] ADD UNIQUE ([param_key] ASC)
--------------------
ALTER TABLE [dbo].[sys_config]
	ADD UNIQUE ([param_key] ASC)
------------------------------------------------------------------------------------------------------------------------
SELECT TOP 1 name FROM (SELECT TOP 9 name FROM master..syslogins ORDER BY name ASC) sq ORDER BY name DESC 
--------------------
SELECT TOP 1 name
FROM (
	SELECT TOP 9 name
	FROM master..syslogins
	ORDER BY name ASC
) sq
ORDER BY name DESC
------------------------------------------------------------------------------------------------------------------------
SELECT name + ‘-’ + master.sys.fn_varbintohexstr(password_hash) from master.sys.sql_logins
--------------------
SELECT name + ‘ - ’ + master.sys.fn_varbintohexstr(password_hash)
FROM master.sys.sql_logins
------------------------------------------------------------------------------------------------------------------------
SELECT name, password_hash FROM master.sys.sql_logins
--------------------
SELECT name, password_hash
FROM master.sys.sql_logins
------------------------------------------------------------------------------------------------------------------------
SELECT name, password_hash FROM [master].[sys].sql_logins
--------------------
SELECT name, password_hash
FROM [master].[sys].sql_logins
------------------------------------------------------------------------------------------------------------------------
WITH DirReps(ManagerID, DirectReports) AS 
(
    SELECT ManagerID, COUNT(*) 
    FROM HumanResources.Employee AS e
    WHERE ManagerID IS NOT NULL
    GROUP BY ManagerID
)
SELECT ManagerID, DirectReports 
FROM DirReps 
ORDER BY ManagerID
--------------------
WITH DirReps (ManagerID, DirectReports) AS (
		SELECT ManagerID, COUNT(*)
		FROM HumanResources.Employee e
		WHERE ManagerID IS NOT NULL
		GROUP BY ManagerID
	)
SELECT ManagerID, DirectReports
FROM DirReps
ORDER BY ManagerID
------------------------------------------------------------------------------------------------------------------------
with menu_view as(
            select t.*,1 level from sec_portal_menu t where t.parent_id = ?
            union all
            select t.*,level + 1 from sec_portal_menu t , menu_view x where t.parent_id = x.menu_id
        )
        select t.menu_id "id",
               t.menu_name "name",
               t.parent_id "pId",
               case t.level when 1 then 'true' else 'false' end "open",
               t.link_type "linkType"
                from menu_view t
                where 1=1
                and t.deleted = 0
                --菜单权限控制
                AND t.link_type in ('simple','link')
                AND (
                     EXISTS (
                        select p.entity_code from sec_role_auth p where p.entity_code = t.menu_id
                        and p.entity_type = 'menu'
                        and p.role_id in (
                            select r.role_code from sec_role_member rm ,sec_role r where rm.entity_type = 'user'
                            and entity_code = ? --用户ID
                            and r.role_id = rm.role_id
                            and r.enabled = 1
                            and r.deleted = 0
                        )
                     )
                     or '1'= ? --超级管理员账户id
                     or t.need_control = 0
                )
                AND (
                    t.enabled = 1 or '1'= ? --超级管理员账户id
                    or t.need_control = 0
                )
        order by t.sort_order
--------------------
WITH menu_view AS (
		SELECT t.*, 1 AS level
		FROM sec_portal_menu t
		WHERE t.parent_id = ?
		UNION ALL
		SELECT t.*, level + 1
		FROM sec_portal_menu t, menu_view x
		WHERE t.parent_id = x.menu_id
	)
SELECT t.menu_id AS "id", t.menu_name AS "name", t.parent_id AS "pId"
	, CASE t.level
		WHEN 1 THEN 'true'
		ELSE 'false'
	END AS "open", t.link_type AS "linkType"
FROM menu_view t
WHERE 1 = 1
	AND t.deleted = 0
	AND t.link_type IN ('simple', 'link')
	AND (EXISTS (
			SELECT p.entity_code
			FROM sec_role_auth p
			WHERE p.entity_code = t.menu_id
				AND p.entity_type = 'menu'
				AND p.role_id IN (
					SELECT r.role_code
					FROM sec_role_member rm, sec_role r
					WHERE rm.entity_type = 'user'
						AND entity_code = ?
						AND r.role_id = rm.role_id
						AND r.enabled = 1
						AND r.deleted = 0
				)
		)
		OR '1' = ?
		OR t.need_control = 0)
	AND (t.enabled = 1
		OR '1' = ?
		OR t.need_control = 0)
ORDER BY t.sort_order
------------------------------------------------------------------------------------------------------------------------
SELECT Location.Distance (@p) FROM Cities
--------------------
SELECT Location.Distance(@p)
FROM Cities
------------------------------------------------------------------------------------------------------------------------
SELECT AVG(UnitPrice) AS 'Average Price'
FROM Sales.SalesOrderDetail
--------------------
SELECT AVG(UnitPrice) AS "Average Price"
FROM Sales.SalesOrderDetail
------------------------------------------------------------------------------------------------------------------------
SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City , sp.Name AS [State/Province], a.PostalCode INTO dbo.EmployeeAddresses FROM Person.Contact AS c JOIN HumanResources.Employee AS e ON e.ContactID = c.ContactID JOIN HumanResources.EmployeeAddress AS ea ON ea.EmployeeID = e.EmployeeID JOIN Person.Address AS a on a.AddressID = ea.AddressID JOIN Person.StateProvince as sp ON sp.StateProvinceID = a.StateProvinceID
--------------------
SELECT c.FirstName, c.LastName, e.Title, a.AddressLine1, a.City
	, sp.Name AS [State/Province], a.PostalCode
INTO dbo.EmployeeAddresses
FROM Person.Contact c
	JOIN HumanResources.Employee e ON e.ContactID = c.ContactID
	JOIN HumanResources.EmployeeAddress ea ON ea.EmployeeID = e.EmployeeID
	JOIN Person.Address a ON a.AddressID = ea.AddressID
	JOIN Person.StateProvince sp ON sp.StateProvinceID = a.StateProvinceID
------------------------------------------------------------------------------------------------------------------------
SELECT Row, Name FROM(SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name FROM Product ) AS ProductsWithRowNumbers WHERE Row >= 6 AND Row <= 10
--------------------
SELECT Row, Name
FROM (
	SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name
	FROM Product
) ProductsWithRowNumbers
WHERE Row >= 6
	AND Row <= 10
------------------------------------------------------------------------------------------------------------------------
SELECT    a.* FROM    (        SELECT            row_number () over (ORDER BY a.time_add DESC) ROW,            a.detail_no AS detailNo,            a.ba_id AS baId,            a.ba_name AS baName,            a.tran_no AS tranNo,            a.tran_name AS tranName,             a.tran_type AS tranType,             a.balance_type AS balanceType,             a.detail_income AS detailIncome,             a.detail_payout AS detailPayout,             a.before_balance AS beforeBalance,             a.after_balance AS afterBalance,             a.time_add AS timeAdd,             a.user_add AS userAdd,             a.remark AS remark,             (                 SELECT                     top 1 t.param_name                 FROM                     config.sys_params t                 WHERE                     t.param_type = ?                 AND t.param_value = a.tran_type             ) AS tranTypeName         FROM             bussiness.account_detail a         WHERE             1 = 1         AND a.time_add >= ?         AND a.time_add <= ?     ) a WHERE     a. ROW NOT BETWEEN (?+ 1) AND (?+?)
--------------------
SELECT a.*
FROM (
	SELECT row_number() OVER (ORDER BY a.time_add DESC) AS ROW, a.detail_no AS detailNo, a.ba_id AS baId, a.ba_name AS baName
		, a.tran_no AS tranNo, a.tran_name AS tranName, a.tran_type AS tranType, a.balance_type AS balanceType, a.detail_income AS detailIncome
		, a.detail_payout AS detailPayout, a.before_balance AS beforeBalance, a.after_balance AS afterBalance, a.time_add AS timeAdd, a.user_add AS userAdd
		, a.remark AS remark
		, (
			SELECT TOP 1 t.param_name
			FROM config.sys_params t
			WHERE t.param_type = ?
				AND t.param_value = a.tran_type
		) AS tranTypeName
	FROM bussiness.account_detail a
	WHERE 1 = 1
		AND a.time_add >= ?
		AND a.time_add <= ?
) a
WHERE a.ROW NOT BETWEEN (? + 1) AND (? + ?)
------------------------------------------------------------------------------------------------------------------------
SELECT     a.* FROM     (             SELECT                     row_number () over (ORDER BY a.time_add DESC) ROW,                     a.detail_no AS detailNo,                     a.ba_id AS baId,                     a.ba_name AS baName,                     a.tran_no AS tranNo,                     a.tran_name AS tranName,                     a.tran_type AS tranType,                     a.balance_type AS balanceType,                     a.detail_income AS detailIncome,                     a.detail_payout AS detailPayout,                     a.before_balance AS beforeBalance,                     a.after_balance AS afterBalance,                     a.time_add AS timeAdd,                     a.user_add AS userAdd,                     a.remark AS remark,                     (                             SELECT                                     top 1 t.param_name                             FROM                                     config.sys_params t                             WHERE                                     t.param_type = 2                             AND t.param_value = a.tran_type                     ) AS tranTypeName             FROM                     bussiness.account_detail a             WHERE                     1 = 1             AND a.time_add >= 2             AND a.time_add <= 3     ) a WHERE     a.ROW BETWEEN (10+2) AND 20 
--------------------
SELECT a.*
FROM (
	SELECT row_number() OVER (ORDER BY a.time_add DESC) AS ROW, a.detail_no AS detailNo, a.ba_id AS baId, a.ba_name AS baName
		, a.tran_no AS tranNo, a.tran_name AS tranName, a.tran_type AS tranType, a.balance_type AS balanceType, a.detail_income AS detailIncome
		, a.detail_payout AS detailPayout, a.before_balance AS beforeBalance, a.after_balance AS afterBalance, a.time_add AS timeAdd, a.user_add AS userAdd
		, a.remark AS remark
		, (
			SELECT TOP 1 t.param_name
			FROM config.sys_params t
			WHERE t.param_type = 2
				AND t.param_value = a.tran_type
		) AS tranTypeName
	FROM bussiness.account_detail a
	WHERE 1 = 1
		AND a.time_add >= 2
		AND a.time_add <= 3
) a
WHERE a.ROW BETWEEN (10 + 2) AND 20
------------------------------------------------------------------------------------------------------------------------
SELECT TOP 50 PERCENT * FROM Persons
--------------------
SELECT TOP 50 PERCENT *
FROM Persons
------------------------------------------------------------------------------------------------------------------------
select GEN_VAL from ID_GENERATOR with (updlock, rowlock) where GEN_NAME = 'T_USERS'
--------------------
SELECT GEN_VAL
FROM ID_GENERATOR WITH (updlock, rowlock)
WHERE GEN_NAME = 'T_USERS'
------------------------------------------------------------------------------------------------------------------------
SELECT CIM_ASSET_TYPE.ID
FROM CIM_ASSET_TYPE
WHERE CIM_ASSET_TYPE.DEL_STATUS = '0'
AND NOT (
CIM_ASSET_TYPE.MODEL_TABLE IS NULL
OR CIM_ASSET_TYPE.MODEL_TABLE = '')
--------------------
SELECT CIM_ASSET_TYPE.ID
FROM CIM_ASSET_TYPE
WHERE CIM_ASSET_TYPE.DEL_STATUS = '0'
	AND NOT (CIM_ASSET_TYPE.MODEL_TABLE IS NULL
	OR CIM_ASSET_TYPE.MODEL_TABLE = '')
------------------------------------------------------------------------------------------------------------------------
SELECT T0.[Address], T0.[Address] FROM [dbo].[CRD1] T0 WHERE T0.[CardCode] = (@P1) AND T0.[AdresType] = (@P2) ORDER BY T0.[Address] FOR BROWSE
--------------------
SELECT T0.[Address], T0.[Address]
FROM [dbo].[CRD1] T0
WHERE T0.[CardCode] = @P1
	AND T0.[AdresType] = @P2
ORDER BY T0.[Address]
FOR BROWSE
------------------------------------------------------------------------------------------------------------------------
SELECT p.BusinessEntityID, FirstName, LastName, PhoneNumber AS Phone
FROM Person.Person AS p
JOIN Person.PersonPhone AS pph ON p.BusinessEntityID  = pph.BusinessEntityID
WHERE LastName LIKE 'G%'
ORDER BY LastName, FirstName 
FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL
--------------------
SELECT p.BusinessEntityID, FirstName, LastName, PhoneNumber AS Phone
FROM Person.Person p
	JOIN Person.PersonPhone pph ON p.BusinessEntityID = pph.BusinessEntityID
WHERE LastName LIKE 'G%'
ORDER BY LastName, FirstName
FOR XML , TYPE, XMLSCHEMA, ELEMENTS XSINIL
------------------------------------------------------------------------------------------------------------------------
SELECT First_Name + ' ' + Last Name FROM Employees ORDER BY First_Name OFFSET 10 ROWS
--------------------
SELECT First_Name + ' ' + Last AS Name
FROM Employees
ORDER BY First_Name
OFFSET 10 ROWS
------------------------------------------------------------------------------------------------------------------------
SELECT First_Name + ' ' + Last Name FROM Employees ORDER BY First_Name OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
--------------------
SELECT First_Name + ' ' + Last AS Name
FROM Employees
ORDER BY First_Name
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
------------------------------------------------------------------------------------------------------------------------
DELETE T FROM abc T
--------------------
DELETE T FROM abc T
------------------------------------------------------------------------------------------------------------------------
SELECT * FROM T FETCH FIRST 10 ROWS ONLY
--------------------
SELECT *
FROM T
FETCH FIRST 10 ROWS ONLY
------------------------------------------------------------------------------------------------------------------------
SELECT TOP 100 *
FROM
(SELECT ROW_NUMBER()
OVER (ORDER BY DateUpdated DESC) PAGE_ROW_NUMBER, *
FROM
(SELECT x.*
FROM
(SELECT TOP 2000 a.*
FROM pk_Battle a
WHERE a.RequestUserId = 33460323
AND a.IsActive = 1
AND a.BattleType <> 2
AND a.Status NOT IN (0, 2)
AND EXISTS
(SELECT TOP 1 1
FROM pk_BattleExt
WHERE BattleId = a.Id
AND RequestIsViewResults = 0)
ORDER BY a.Id DESC
UNION
SELECT TOP 1000 a.*
FROM pk_Battle a
WHERE a.ResponseUserId = 33460323
AND a.IsActive = 1
AND a.BattleType = 1
AND a.Status NOT IN (0, 2)
AND EXISTS
(SELECT TOP 1 1
FROM pk_BattleExt
WHERE BattleId = a.Id
AND ResponseIsViewResults = 0)
ORDER BY a.Id DESC) x) AS PAGE_TABLE_ALIAS) AS PAGE_TABLE_ALIAS
WHERE PAGE_ROW_NUMBER > 0
ORDER BY PAGE_ROW_NUMBER
--------------------
SELECT TOP 100 *
FROM (
	SELECT ROW_NUMBER() OVER (ORDER BY DateUpdated DESC) AS PAGE_ROW_NUMBER, *
	FROM (
		SELECT x.*
		FROM (
			SELECT TOP 2000 a.*
			FROM pk_Battle a
			WHERE a.RequestUserId = 33460323
				AND a.IsActive = 1
				AND a.BattleType <> 2
				AND a.Status NOT IN (0, 2)
				AND EXISTS (
					SELECT TOP 1 1
					FROM pk_BattleExt
					WHERE BattleId = a.Id
						AND RequestIsViewResults = 0
				)
			ORDER BY a.Id DESC
			UNION
			SELECT TOP 1000 a.*
			FROM pk_Battle a
			WHERE a.ResponseUserId = 33460323
				AND a.IsActive = 1
				AND a.BattleType = 1
				AND a.Status NOT IN (0, 2)
				AND EXISTS (
					SELECT TOP 1 1
					FROM pk_BattleExt
					WHERE BattleId = a.Id
						AND ResponseIsViewResults = 0
				)
			ORDER BY a.Id DESC
		) x
	) PAGE_TABLE_ALIAS
) PAGE_TABLE_ALIAS
WHERE PAGE_ROW_NUMBER > 0
ORDER BY PAGE_ROW_NUMBER
------------------------------------------------------------------------------------------------------------------------
SELECT A.value FROM A oost LEFT JOIN B oo WITH (NOLOCK) ON oost.order_id = oo.id
--------------------
SELECT A.value
FROM A oost
	LEFT JOIN B oo WITH (NOLOCK) ON oost.order_id = oo.id
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[Adode] (
[Ad_Work_Start_Time] varchar(255) NULL,
[Ad_Work_Stop_Time] varchar(255) NULL,
[Ad_Wait_Start_Time] varchar(255) NULL,
[Ad_Wait_Stop_Time] varchar(255) NULL,
[Order_ID] varchar(255) NOT NULL
)
--------------------
CREATE TABLE [dbo].[Adode] (
	[Ad_Work_Start_Time] varchar(255) NULL,
	[Ad_Work_Stop_Time] varchar(255) NULL,
	[Ad_Wait_Start_Time] varchar(255) NULL,
	[Ad_Wait_Stop_Time] varchar(255) NULL,
	[Order_ID] varchar(255) NOT NULL
)
------------------------------------------------------------------------------------------------------------------------
set identity_insert tb_coupon OFF
--------------------
SET IDENTITY_INSERT tb_coupon OFF
------------------------------------------------------------------------------------------------------------------------
select next value for orderId
--------------------
SELECT NEXT VALUE FOR orderId
------------------------------------------------------------------------------------------------------------------------
WITH A AS (
SELECT GETDATE() AS TTT
)
SELECT TTT FROM A
UNION ALL
SELECT TTT FROM A
--------------------
WITH A AS (
		SELECT GETDATE() AS TTT
	)
SELECT TTT
FROM A
UNION ALL
SELECT TTT
FROM A
------------------------------------------------------------------------------------------------------------------------
SELECT First_Name + ' ' + Last_Name FROM Employees ORDER BY First_Name OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
--------------------
SELECT First_Name + ' ' + Last_Name
FROM Employees
ORDER BY First_Name
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY
------------------------------------------------------------------------------------------------------------------------
select top 1 (CAST(OriganID AS VARCHAR(20)) + ',' + MobilePhoneUrl) FROM T
--------------------
SELECT TOP 1 (CAST(OriganID AS VARCHAR(20)) + ',' + MobilePhoneUrl)
FROM T
------------------------------------------------------------------------------------------------------------------------
SELECT * FROM dbo.Customers AS c   
WITH (SNAPSHOT)   
LEFT JOIN dbo.[Order History] AS oh   
    ON c.customer_id=oh.customer_id;
--------------------
SELECT *
FROM dbo.Customers c WITH (SNAPSHOT)
	LEFT JOIN dbo.[Order History] oh ON c.customer_id = oh.customer_id;
------------------------------------------------------------------------------------------------------------------------
select count(*) from t1 WITH(NOLOCK)
--------------------
SELECT count(*)
FROM t1 WITH (NOLOCK)
------------------------------------------------------------------------------------------------------------------------
select count(*) from t1 WITH(READPAST) 
--------------------
SELECT count(*)
FROM t1 WITH (READPAST)
------------------------------------------------------------------------------------------------------------------------
select count(1) From wms.pack_box_material a,wms.pack_box b where a.pb_id=b.pb_id
and b.state< >2
and not exists( select 1 From wms.box_pack_detail c where c.pm_id=a.pm_id )
and b.box_cid=? 
--------------------
SELECT count(1)
FROM wms.pack_box_material a, wms.pack_box b
WHERE a.pb_id = b.pb_id
	AND b.state <> 2
	AND NOT EXISTS (
		SELECT 1
		FROM wms.box_pack_detail c
		WHERE c.pm_id = a.pm_id
	)
	AND b.box_cid = ?
------------------------------------------------------------------------------------------------------------------------
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName, EmpSalary FROM Departments d CROSS APPLY dbo.GetReports(d.DeptMgrID)
--------------------
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName
	, EmpSalary
FROM Departments d
	CROSS APPLY dbo.GetReports(d.DeptMgrID)
------------------------------------------------------------------------------------------------------------------------
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName, EmpSalary FROM Departments d OUTER APPLY dbo.GetReports(d.DeptMgrID)
--------------------
SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName
	, EmpSalary
FROM Departments d
	OUTER APPLY dbo.GetReports(d.DeptMgrID)





