有用的sql语句

  随机读取若干条记录,测试过
Access语法:SELECT top 10 * From 表名 ORDER BY Rnd(id)
Sql server:select top n * from 表名 order by newid()
mysql select * From 表名 Order By rand() Limit n

 

  说明:选择从10到15的记录
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc

 

  根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only

 

1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
  法一:select * into b from a where 1<>1
  法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
      insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
      insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
  例子:..from b in '"&Server.MapPath(".")&"/data.mdb" &"' where..

 

说明:几个高级查询运算词
  A: UNION 运算符
  UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
  B: EXCEPT 运算符
  EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
  C: INTERSECT 运算符
  INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
  注:使用运算词的几个查询结果行必须是一致的。

 

20、说明:列出数据库里所有的表名
select name from sysobjects where type='U'
21、说明:列出表里的所有的列名
select name from syscolumns where id=object_id('TableName')

 

-----------------------------存储过程-----------------------------------

-(1)无参数的存储过程
    -- 创建一个存储过程,存储各部门的总工资与平均工资及部门编号。
    create proc deptsalary
    as
    select avg(salary) avgsalary, sum(salary) sumsalary,dept_id
    from employee
    group by dept_id

    -- 调用
    exec deptsalary
 
 --(2) 带有输入参数的存储过程
   -- 创建一个存储过程,以员工姓名为参数,输入姓名后显示某人信息(模糊查询).
    create proc empname
    @name varchar(20)
    as
    select *
    from employee
    where emp_lname+emp_fname
            like '%'+@name+'%'
    -- 调用
    exec empname  'A'


-- (3)带有输出参数的存储过程
  -- 创建一个存储过程,能够输出工资最高员工的工资。
     create proc empsalary
     @empsal decimal(20,3) output
     as
     select @empsal=max(salary)
     from employee
   
     --调用带输出参数的存储过程:
     declare @empsal decimal(20,3)
     exec empsalary @empsal output
     print @empsal

  --(4)查看 存储过程
    -- sp_helptext  存储过程名
     sp_helptext  deptsalary
drop table #tb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值