2016年7月28日 星期四

[SQL] 暫存表 Temporary Tables

最近遇到一些情況

都是可以用到暫存表去解決的

偏偏以前從沒機會使用過(也沒聽過 XD)

感謝同事幫忙  >_<


狀況 1. 搜尋出來的資料量太大,EXCEL沒辦法全部貼上

---> 把資料撈進暫存表 再從暫存表下條件慢慢撈~~

狀況2. 依照EXCEL上的資料順序 去撈取資料, 再把撈到的資料貼到EXCEL上

  ---> 所以先把EXCEL的資料建表 order by 一些欄位 就可以了)

  1. //狀況1
  2. select email
  3. into #tempMail
  4. from member with(nolock)
  5. where ..some conditions
  6.  
  7. SELECT * FROM #tempMail WHERE email LIKE 'A%' ORDER BY email
  8.  
  9. //狀況2
  10. //暫存表的建法 是在table名稱前加上 #
  11. create table #tmp_table (return_id nvarchar(30) , pid nvarchar(20))
  12.  
  13. //建完後insert資料
  14. insert into #tmp_table(return_id ,pid) values('XXXXXX','YYYY')




沒有留言:

張貼留言