規則:
1、如果可以,要加條件判斷,為了不必要的錯誤
use hferpdb-- 選擇資料庫
select * from hr_person-- 查資料的時候最好寫明欄位,這樣會提高效率
select departname,nam,sex,age,identityno,workdate,duty,educationlevel,tel,telbak from hr_person
select * into hr_personbackup from hr_person -- 複製表,只用於測試
update hr_person set tel=b.phone,telbak=b.duan from hr_person ,phone b
where hr_person.nam=b.nam-- 從另一個表提取資料來更新當前表
drop table hr_personbackup --刪除整個表
select count(nam) '該列顯示的名稱' from phone--統計某個欄位的數量
delete from hr_person where tel is null--刪除,最好加條件
select top 0 * from people--用於看有什麼欄位名稱
delete from people where xingming in (select nam from hr_personbackup group by nam)
--條件可以是資料集,不一定是表
insert into phone (nam) select xingming from people p where p.xingming is not null
--把某個欄位插入目標表的欄位
--把一個表的資料插入到另一個表指定的列中
insert into hr_person(departname,nam,sex,identityno,workdate,duty,educationlevel,LastOpDate,CreateDate,isSocialSec,id_person)
select bumen,xingming,sex1,sfz,worktime,job2,xueli,getdate(),getdate(),0,id2 from people where id2 is not null
--截取身份證的出生年月
select substring(identityno,7,8) from hr_personbackup
--把身份證轉換為出生日期
select cast(substring(identityno,7,8) as datetime) from hr_personbackup
--得到身份證的到現在時間的年齡,不過一般是設計年齡欄位 = 目前時間減去現在的時間
select datediff(year,substring(identityno,7,8),getdate()) from hr_personbackup
--cast 用於轉化欄位格式,不過必須與目標欄位的格式不一樣
update hr_person set birthday = cast(substring(identityno,7,8) as datetime) from hr_person
--尋找相同的記錄
select nam from hr_person group by nam having count(nam) >1
未完待續。。。