就业数据资源平台
当前位置:首页 > PC技术
2012年计算机三级考试PC技术基础知识(46)

数据开发-经典

  1.按姓氏笔画排序:


  Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多


  2.数据库加密:


  select encrypt('原始密码')


  select pwdencrypt('原始密码')


  select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')


  select pwdencrypt('原始密码')


  select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同


  3.取回表中字段:


  declare @list varchar(1000),


  @sql nvarchar(1000)


  select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'


  set @sql='select '+right(@list,len(@list)-1)+' from 表A'


  exec (@sql)


  4.查看硬盘分区:


  EXEC master..xp_fixeddrives


  5.比较A,B表是否相等:


  if (select checksum_agg(binary_checksum(*)) from A)


  =


  (select checksum_agg(binary_checksum(*)) from B)


  print '相等'


  else


  print '不相等'
  6.杀掉所有的事件探察器进程:


  DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocesses


  WHERE program_name IN('SQL profiler',N'SQL 事件探查器')


  EXEC sp_msforeach_worker '?'


  7.记录搜索:


  开头到N条记录


  Select Top N * From 表


  -------------------------------


  N到M条记录(要有主索引ID)


  Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc


  ----------------------------------


  N到结尾记录


  Select Top N * From 表 Order by ID Desc


  案例


  例如1:一张表有一万多条记录,表的第一个字段 RecID 是自增长字段, 写一个SQL语句, 找出表的第31到第40个记录。


  select top 10 recid from A where recid not in(select top 30 recid from A)


  分析:如果这样写会产生某些问题,如果recid在表中存在逻辑索引。


  select top 10 recid from A where……是从索引中查找,而后面的select top 30 recid from A则在数据表中查找,这样由于索引中的顺序有可能和数据表中的不一致,这样就导致查询到的不是本来的欲得到的数据。


  解决方案


  1, 用order by select top 30 recid from A order by ricid 如果该字段不是自增长,就会出现问题


  2, 在那个子查询中也加条件:select top 30 recid from A where recid>-1


  例2:查询表中的最后以条记录,并不知道这个表共有多少数据,以及表结构。


  set @s = 'select top 1 * from T where pid not in (select top ' + str(@count-1) + ' pid from T)'


  print @s exec sp_executesql @s


  9:获取当前数据库中的所有用户表


  select Name from sysobjects where xtype='u' and status>=0 

就业数据资源平台