createfunction Concat_Name(@csvarchar(1000)) returnsvarchar(3000) as begin declare@Stringvarchar(8000)='' select@String=@String+name +';'from students where class=@cs return@String end select dbo.Concat_Name('B')
SELECT*INTO KITE FROM ( SELECT*FROM ITEMS WHERE1=1/* condition */ ) AS A
資料表較大時,可以先取出某一部分進行查詢數據,效能比較快
4.2.2. 以客戶資料為主table,查詢最後一筆交易產品線記錄
1 2 3 4
SELECT AA001 AS 客戶代號 ,(SELECT TOP 1 PRODUCT_DATE FROM ORDER_BOM WHERE OD_AA001=AA001 ORDERBY PRODUCT_DATE DESC) 最後一次交易日 FROM CRMAA WHERE AA013 IN('台北')
常見狀況以主表資料為主,再去撈取某一資訊,放在欄位上進行呈現
當然,如果有多個欄位,用一樣的語句結構,再多寫幾行子查詢
4.2.3. 子表完成後,再進行第二次篩選
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECTDISTINCT A.* ,HC050 訂單合約起日,HC051 訂單合約迄日 FROM ( SELECT AA001 AS 客戶代號,AA002 AS 客戶名稱,OD004 AS 出貨日期,OD005 AS 出貨單號 FROM ORDER_BOM JOIN CRMAA ON AA001=OD_AA001 WHERE 1=1 GROUPBY OD_AA001 ) AS A LEFTJOIN CRMHC ON HC002 = REPLACE(出貨單號,'DS2-','') ORDERBY 出貨日期,客戶代號
縮限資料後,再進行第二次的篩選
4.2.4. 子表完成後,進行第三次篩選
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT AR001,AR002,AR003,AR004,PE2.PE02,PE2.PE03,PE2.PE04 FROM ( SELECT*FROM SERAR_KITE2 WHERE RTRIM(AR001)+RTRIM(AR002)+RTRIM(AR003)+RTRIM(AR004) IN ( SELECT RTRIM(AR001)+RTRIM(AR002)+RTRIM(AR003)+RTRIM(AR004) FROM SERAR_KITE2 GROUPBY AR001,AR002,AR003,AR004 HAVINGCOUNT(PE02) >1 ) ) AS AR JOIN DSCPE PE2 ON AR.PE02=PE2.PE02 collate database_default ORDERBY AR001,AR002,AR003,AR004
同樣概念也可以包N層
4.2.5. 縱向轉直向
1 2 3 4
SELECT COUNT(CASE class WHEN'a'THEN class END) as A_CLASS_CNT ,COUNT(CASE class WHEN'b'THEN class END) as B_CLASS_CNT FROM students
同一個表統計時,如果有COUNT需求,要轉向,是可以這樣寫的
4.2.6. 相乘
1 2 3
SELECT student_name,subject_name FROM students,subjects ORDERBY student_name,subject_name
自行想像需求
4.3. 修改
4.3.1. 參考table更新至目的table
1 2 3 4
UPDATE AR1 SET AR1.AR005= AR2.PE02 --SELECT AR1.AR005,AR2.PE02 FROM SERAR_KITE AR1 JOIN SERAR_KITE2 AR2 ON AR1.AR001= AR2.AR001 AND AR1.AR002=AR2.AR002 AND AR1.AR003=AR2.AR003 AND AR1.AR004=AR2.AR004 WHERE AR1.AR005 !=AR2.PE02
4.4. 新增
4.4.1. 批次新增資料,參考既有table,更新目的table
1 2 3 4 5 6 7 8 9
--一般業務 DECLARE@JOB_IDASVARCHAR(10) DECLARE@UIDASVARCHAR(10) SET@JOB_ID='001'--一般業務 SET@UID='02944'-- 參考工號 INSERTINTO ADMML (ML001,ML002,ML003,ML004,ML005,ML006,ML007,ML008,ML009,ML010) SELECTDISTINCT@JOB_IDAS ML001,MG002 AS ML002, MG003 AS ML003,MG004 AS ML004,MG005 AS ML005,MG006 AS ML006,MG007 AS ML007,''AS ML008,MG009 AS ML009,MG010 AS ML010 FROM ADMMG_ACP WHERE MG001=@UID ORDERBY MG002
批次新增資料,再利用參數方式去當條件
4.4.2. 批次新增資眼,excel 來源匯入,更新目的table
1 2 3 4 5 6
INSERTINTO CRMHG(HG001,HG005,HG002,HG032,HG033,HG024,HG021,HG007,HG010,HG012,HG026,HG008,HG011) SELECT 客戶代號,姓名,'8888',職稱,職能別,郵區,公司地址,電話1,電話1_分機,行動電話,行動電話2,電話2,電話2_分機 FROM [dbo].[工作表_kite] WHERE 是否匯入='Y' AND (客戶代號 collate database_default + 姓名 collate database_default) NOTIN (SELECT HG001+HG005 FROM CRMHG)
IF EXISTS(SELECT*FROM master.dbo.sysprocesses WHERE spid in (SELECT blocked FROM master.dbo.sysprocesses)) BEGIN SELECT spid 處理序, status 狀態 , 登入帳號=SUBSTRING(SUSER_SNAME(sid), 1, 30) , 使用者機器名稱=SUBSTRING(hostname, 1, 12) , 是否鎖住=CONVERT(char(3), blocked) , 資料庫名稱=SUBSTRING(DB_NAME(dbid), 1, 10) , cmd 命令, waittype 等待類型 FROM master.dbo.sysprocesses --找出鎖住別(自己未被鎖住(blocked=0) 但在別的處理序中blocked欄位出現的值) WHERE spid IN (SELECT blocked FROM master.dbo.sysprocesses) AND blocked =0 END ELSE BEGIN SELECT'沒有處理序被鎖住' END
5.1.2. SQL SERVER 2008
5.1.2.1. 找出最耗用IO的語法
1 2 3 4 5 6 7 8 9 10 11 12
SELECT TOP 10 total_logical_reads, total_logical_writes, execution_count, total_logical_reads+total_logical_writes AS [IO_total], st.text AS query_text, db_name(st.dbid) AS database_name, st.objectid AS object_id FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(sql_handle) st WHERE total_logical_reads+total_logical_writes>0 ORDERBY [IO_total] DESC
5.1.2.2. 列出目前最耗用CPU的前50個查詢
1 2 3 4 5 6 7 8 9 10
SELECT TOP 50 qs.total_worker_time / qs.execution_count AS[Avg CPU Time], SUBSTRING(qt.text, qs.statement_start_offset /2, (CASEWHEN qs.statement_end_offset =-1THEN len(CONVERT (NVARCHAR (MAX), qt.text)) *2 ELSE qs.statement_end_offset END- qs.statement_start_offset) /2) AS query_text, qt.dbid, qt.objectid FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text (qs.sql_handle) AS qt ORDERBY [Avg CPU Time] DESC;
5.1.2.3. 列出目前最耗用Worker Time的前50個查詢
1 2 3 4 5 6 7 8 9 10 11
SELECT TOP 50sum(qs.total_worker_time) AS total_cpu_time, sum(qs.execution_count) AS total_execution_count, count(*) AS'#_statements', qt.dbid, qt.objectid, qs.sql_handle, qt.[text] FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text (qs.sql_handle) AS qt GROUPBY qt.dbid, qt.objectid, qs.sql_handle, qt.[text] ORDERBYsum(qs.total_worker_time) DESC, qs.sql_handle;
SELECT r.scheduler_id as 排程器識別碼, status as 要求的狀態, r.session_id as SPID, r.blocking_session_id as BlkBy, substring( ltrim(q.text), r.statement_start_offset/2+1, (CASE WHEN r.statement_end_offset =-1 THEN LEN(CONVERT(nvarchar(MAX), q.text)) *2 ELSE r.statement_end_offset END- r.statement_start_offset)/2) AS [正在執行的 T-SQL 命令], r.cpu_time as [CPU Time(ms)], r.start_time as [開始時間], r.total_elapsed_time as [執行總時間], r.reads as [讀取數], r.writes as [寫入數], r.logical_reads as [邏輯讀取數], -- q.text, /* 完整的 T-SQL 指令碼 */ d.name as [資料庫名稱] FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS q LEFTJOIN sys.databases d ON (r.database_id=d.database_id) WHERE r.session_id >50AND r.session_id <> @@SPID ORDERBY r.total_elapsed_time desc
USE DSC_CRM_TEST CHECKPOINT DBCC SHRINKFILE(2, 200)
BACKUP LOG [DSC_CRM_TEST] WITH TRUNCATE_ONLY; DBCC SHRINKFILE ([DSC_CRM_TEST_Log],500);
ALTER DATABASE DSC_CRM_SP SET RECOVERY SIMPLE WITH NO_WAIT DBCC SHRINKFILE(DSC_CRM_SP_Log, 1) ALTER DATABASE DSC_CRM_SP SET RECOVERY FULLWITH NO_WAIT GO
BACKUP LOG [Quiz] WITH TRUNCATE_ONLY; DBCC SHRINKFILE ([Quiz_Log],500);