T-SQL grammar learning (1)
Section 1 Uncommon Statements
Uncommon statements - refer to some uncommon query statements, not for business data queries
- SET STATISTICS IO ON (used to query the number of logical reads, the number of physical reads)
picture - select @@version (query the database version of the current instance)
Section 2 Precautions for Operating Database Management Tools
When we are using the database graphical management interface, if we forget to add conditions when performing update, insert, delete operations, then a very serious problem will occur, the data of the entire table, and even the entire database may be deleted by you So when doing these dangerous operations, I recommend adding transactions, as follows:
begin tran hcx --创建并命名事务
update MCSMCB
set MCB004=c.MYA013,MCB021=c.MYA003
from
MCSMCB b
left join MCSMCA a on b.MCB001=a.MCA001
left join EB_DuoMoCh.dbo.BASMYA c on c.MYA001=a.MCA002 and RTRIM(LTRIM(c.MYA004))=b.MCB003 AND b.MCB015=c.MYA002
--先使用select将要update的列,以及要update的值查询出来,做好对比,然后再执行update操作,并select出来,最后commit
select MCB001,MCA002,c.MYA004,b.MCB003,c.MYA013,b.MCB004,c.MYA003,b.MCB021,b.MCB015,c.MYA002 from
MCSMCB b
left join MCSMCA a on b.MCB001=a.MCA001
left join EB_DuoMoCh.dbo.BASMYA c on c.MYA001=a.MCA002 and RTRIM(LTRIM(c.MYA004))=b.MCB003 AND b.MCB015=c.MYA002
rollback tran hcx --回滚事务
COMMIT TRAN hcx