小小飞鹰

     中国人缺少的是步骤,太急。练太极!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sql 语句的一此心得/小技巧 (持续更新中....)

Posted on 2006-04-11 14:08  小小飞鹰  阅读(332)  评论(0编辑  收藏  举报


1 说明:复制表(只复制结构,源表名:a 新表名:b)  

  sql: select * into b from a where 1<>1

  2 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)  

  sql: insert into b(a, b, c) select d,e,f from b; 
              select * into b from a

  3 说明:显示文章、提交人和最后回复时间  

  sql: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

  4 说明:外连接查询(表名1:a 表名2:b)  

  sql: select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a = b.c

  5 说明:日程安排提前五分钟提醒  

  sql: select * from 日程安排 where datediff(''minute'',f开始时间,getdate())>5  

  6 说明:两张关联表,删除主表中已经在副表中没有的信息

  sql:   

  delete from info where not exists ( select * from infobz where info.infid=infobz.infid

  7 说明:四表联查问题:  

  sql: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

  8 说明:得到表中最小的未使用的id号

  sql: 

  select (case when exists(select * from handle b where b.handleid = 1) then min(handleid) + 1 else 1 end) as handleid

   from handle

   where not handleid in (select a.handleid - 1 from handle a) 


        9 sql 2000 中修改字段的默认值:
            declare @name varchar(100)
            select @name=b.name  from syscolumns a,sysobjects b where a.id=object_id(N'表名')   
            and   b.id=a.cdefault   and   a.name='字段'   and   b.name   like   'DF%'
            exec('Alter   table   表名   DROP CONSTRAINT ' + @name )  
            只需修改表名和字段名即可;

            10 判断表T_Inform 是否存在字段InStoreDeptClassID ,如果不存在加入该字段:
                if (NOT exists ( select * from dbo.syscolumns where name = 'InStoreDeptClassID' and id in 
                (select id from dbo.sysobjects where id = object_id(N'[dbo].[T_Inform]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)))
ALTER TABLE dbo.T_Inform ADD InStoreDeptClassID varchar(50) NULL
 GO 

            11 设表T_ReceiveInStore 中有字段ID和InStorePerson,客户要求他们只能看到自己分公司的资料,因此在表中加入一个字段instoreDeptClassID,用于表示分公司的ID;前面客户已经输入相关的资料,因此根据InStorePerson到公司类别表T_DeptClass中找deptClassID (小弟做了张视图V_PersonToDeptClass )更新instoreDeptClassID,如下:

update T_ReceiveInStore set instoreDeptClassID=(select deptClassID from V_PersonToDeptClass where personname = InStorePerson)
            12 查看当前用户每个表占用空间的大小:
    Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name
            13 oracle创建用户
   create user JZX identified by JZX345 default tablespace users Temporary TABLESPACE Temp;
  
grant connect,resource,dba to JZX;
   commit;


 


很多的技巧是我不知道的,小弟还望各位大哥多多帮忙,有好的发表上来我一起加入.....

Oracle:
exp jzx/jzx345@jzx201 file=d:jzx201_0520.dmp full=y

ALTER USER system ACCOUNT UNLOCK;