zoukankan      html  css  js  c++  java
  • sqlbishi

     begin
      rollback transaction
     end
    else
     begin
      commit transaction
     end
    2隐式事务

    3自动提交事务
    单独的SQL语句视为一个事务,如果执行自动提交,否则会滚

    ------------------------------------------
    索引
    1:主键索引
    唯一性  它允许快速访问数据库----定义一个主键将自动创建一个主键索引
    2:聚集索引
    按 a b c
    3:非聚集索引--一个表只能创建一个聚集索引,但能有多个非聚集索引
    比聚集索引查询慢

    use stuDB--创建索引
    go
    if exists(select * from sysindexes where name='IX_stuMarks_writtenExam')
     drop index IX_stuMarks_writtenExam
    create [unique][clustered][noncluster] IX_stuMarks_writtenExam--cluster[klQstE]成群
    on stuMarks(writtenExam)
    go
    查询
    select * from stuMarks
    (index=IX_stuMarks_writtenExam)
    where writtenExam betwenn 60 and 90
    ---------------------------------------------
    视图
    if exists(select * from sysobjects where name='view_stuInfo_stuMarks')
     drop view view_stuInfo_stuMarks
    create view view_stuInfo_stuMarks
    as
    select ```````.....
    go
    select * from view_stuInfo_stuMarks
    〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓

    ⑧存储过程


    create proc sp_

      @tmp varchar()
    as
    declare @tmp1

    1.ASC表示升序排序,DESC表示降序排序

    2.quotename print quotename('aabb','''')
    把字符加上[],还可以quotename('aabb','''')把[]变成''加引号

    3.创建变量表,多行插入
    declare @tmp table(id int primary key ,col decimal(10,2))
    insert @tem select 1,25.21
    union all select 2,26,23


    3.表的复制语法
    insert table1 from table2
    insert table select.....
    select * into table1 from table2

    4.cross join
    叫查表 两表交叉相乘

    5.exec (string)执行拼出来的sql语句

    6.创建临时表 只有临时表才能exec()
    create table #tmp()

    7.union all  union

    9.DECLARE CURSOR  游标定义

    10. distinct

    ==================================================

    declare @sqlstr varchar(2000)
    declare @i int
    set @i=2
    create table #tmp(tyear int,capital1 money,capital2 money,capital3 money,tdate datetime,tmonth int)
     set @sqlstr ='insert #tmp select 2009,1,2,3,'+quotename('2009-12-12','''')+','+convert(varchar(10),1)
    while @i<=12
    begin
     set @sqlstr=@sqlstr+' union all select 2009,1,2,3,'+quotename('2009-12-12','''')+','+convert(varchar(10),@i)
    set @i=@i+1
    end
    print @sqlstr
    exec(@sqlstr)
    select * from #tmp
    select student.stuid,t.* into teacher from student cross join #tmp as t
    /**
    insert @tmp select 2009,1,2,3,'2009-12-12',1
    union all select 2009,1,2,3,'2009-12-12',2
    union all select 2009,1,2,3,'2009-12-12',3
    union all select 2009,1,2,3,'2009-12-12',4
    union all select 2009,1,2,3,'2009-12-12',5
    union all select 2009,1,2,3,'2009-12-12',6
    union all select 2009,1,2,3,'2009-12-12',7
    union all select 2009,1,2,3,'2009-12-12',8
    union all select 2009,1,2,3,'2009-12-12',9
    union all select 2009,1,2,3,'2009-12-12',10
    union all select 2009,1,2,3,'2009-12-12',11
    union all select 2009,1,2,3,'2009-12-12',12
    drop table teacher

     */

    //题目二

    select s.stunum,sum(case when tmonth=1 then capital3 else 0 end) '一月',
    sum(case when tmonth=2 then capital3 else 0 end) '二月',
    sum(case when tmonth=3 then capital3 else 0 end) '三月',
    sum(case when tmonth=4 then capital3 else 0 end) '四月',
    sum(case when tmonth=5 then capital3 else 0 end) '五月',
    sum(case when tmonth=6 then capital3 else 0 end) '六月',
    sum(case when tmonth=7 then capital3 else 0 end) '七月',
    sum(case when tmonth=8 then capital3 else 0 end)'八月',
    sum(case when tmonth=9 then capital3 else 0 end) '九月',
    sum(case when tmonth=10 then capital3 else 0 end) '十月',
    sum(case when tmonth=11 then capital3 else 0 end) '十一月',
    sum(case when tmonth=12 then capital3 else 0 end) '十二月'
    from student as s inner join teacher as t on(s.stuid=t.stuid) group by stunum

    /**
    create table gb
    (
     gid int identity(1,1) primary key not null,
     gname varchar(20) not null
    )
    insert gb select 'zzk'
    union all select 'zzk'
    union all select 'zz'
    union all select 'zk'
    union all select 'k'
    union all select 'z'
    select * from gb

    select count(gname) from gb group by gname having count(gname)>1
    select count(gname) from gb

    drop table gb
    */

    ===============================================================================

    select itemName from dbo.table1 union all select * from tests

    create table a
    (
     id int null,
     names varchar(10),
    )
    insert into a select 1,'qwe'
    union all select 1,'qwe'
    union all select 2,'qwe'
    union all select 3,'qweasdaa'
    drop from a
    select * from a
    delete from a

    //=============
    select * from T1
    ////======列模块
    declare @s nvarchar(4000)
    set @s=''
    Select @s=@s+','+[column2]+'=max(case when [column2]='+quotename([column2],'''')+' then [value] else 0 end)'
    from t1 group by[column2]
    print @s
    /**,电话=max(case when [column2]='电话' then [value] else 0 end),宽带=max(case when [column2]='宽带' then [value] else 0 end)  -->*/
    exec('select [column1] as 地名'+@s+' from t1 group by [column1]')

    //总费用 sum要比group后执行
    declare @s nvarchar(4000)
    set @s=''
    Select @s=@s+','+quotename([column2])+'=max(case when [column2]='+quotename([column2],'''')+' then [value] else 0 end)'
    from t1 group by[column2]
    exec('select [column1] as 地名'+@s+',[总费用]=sum([value])  from t1 group by [column1]')

    //新建零时表 插入行模版
    declare @s nvarchar(4000)
    declare @tems nvarchar(2000)
    set @s=''
    Select @s=@s+','+quotename([column2])+'=max(case when [column2]='+quotename([column2],'''')+' then [value] else 0 end)'
    from t1 group by[column2]
    set @tems='select [column1] as 地名'+@s+',[总费用]=sum([value]) into t2  from t1 group by [column1]'
    print @tems
    exec('select [column1] as 地名'+@s+',[总费用]=sum([value]) into t2  from t1 group by [column1]')
    select * from t2
    drop table T2

    declare @s1 int ;
    declare @s2 int ;
    select @s1=sum(isnull(电话,0)),@s2=sum(isnull(宽带,0)) from t2 group by 电话,宽带
    print @s1
    print @s2
    insert into t2(t2.电话,t2.宽带) values(@s1,@s2)
    select * from t2

    =========================================================================

    use MyWeight
    select * from #tmp
    create table #tmp(rq varchar(10),shengfu nchar(1))

    insert into #tmp values('2005-05-09','胜')
    insert into #tmp values('2005-05-09','胜')
    insert into #tmp values('2005-05-09','负')
    insert into #tmp values('2005-05-09','负')
    insert into #tmp values('2005-05-10','胜')
    insert into #tmp values('2005-05-10','负')
    insert into #tmp values('2005-05-10','负')
    1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq
    select rq,sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq

    2) select N.rq,N.勝,M.負 from (
    select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join
    (select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq

    3)select a.col001,a.a1 胜,b.b1 负 from
    (select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a,
    (select col001,count(col001) b1 from temp1 where col002='负' group by col001) b
    where a.col001=b.col001


    2.请教一个面试中遇到的SQL语句的查询问题
    表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
    ------------------------------------------
    select (case when a>b then a else b end ),
    (case when b>c then b esle c end)
    from table_name


    3.面试题:一个日期判断的sql语句?
    请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)
    ------------------------------------------
    select * from tb where datediff(dd,SendTime,getdate())=0


    4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,
    请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 
       大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 
           显示格式: 
           语文              数学                英语 
           及格              优秀                不及格   
    ------------------------------------------
    select
    (case when 语文>=80 then '优秀'
            when 语文>=60 then '及格'
    else '不及格') as 语文,
    (case when 数学>=80 then '优秀'
            when 数学>=60 then '及格'
    else '不及格') as 数学,
    (case when 英语>=80 then '优秀'
            when 英语>=60 then '及格'
    else '不及格') as 英语,
    from table

    5.在sqlserver2000中请用sql创建一张用户临时表和系统临时表,里面包含两个字段ID和IDValues,类型都是int型,并解释下两者的区别?
    ------------------------------------------
    用户临时表:create table #xx(ID int, IDValues int)
    系统临时表:create table ##xx(ID int, IDValues int)

    区别:
    用户临时表只对创建这个表的用户的Session可见,对其他进程是不可见的.
    当创建它的进程消失时这个临时表就自动删除.

    全局临时表对整个SQL Server实例都可见,但是所有访问它的Session都消失的时候,它也自动删除.

    6.sqlserver2000是一种大型数据库,他的存储容量只受存储介质的限制,请问它是通过什么方式实现这种无限容量机制的。
    ------------------------------------------
    它的所有数据都存储在数据文件中(*.dbf),所以只要文件够大,SQL    Server的存储容量是可以扩大的.

    SQL Server 2000 数据库有三种类型的文件:

    主要数据文件
    主要数据文件是数据库的起点,指向数据库中文件的其它部分。每个数据库都有一个主要数据文件。主要数据文件的推荐文件扩展名是 .mdf。

    次要数据文件
    次要数据文件包含除主要数据文件外的所有数据文件。有些数据库可能没有次要数据文件,而有些数据库则有多个次要数据文件。次要数据文件的推荐文件扩展名是 .ndf。

    日志文件
    日志文件包含恢复数据库所需的所有日志信息。每个数据库必须至少有一个日志文件,但可以不止一个。日志文件的推荐文件扩展名是 .ldf。

    7.请用一个sql语句得出结果
    从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
    如使用存储过程也可以。

    table1

    月份mon 部门dep 业绩yj
    -------------------------------
    一月份      01      10
    一月份      02      10
    一月份      03      5
    二月份      02      8
    二月份      04      9
    三月份      03      8

    table2

    部门dep      部门名称dname
    --------------------------------
          01      国内业务一部
          02      国内业务二部
          03      国内业务三部
          04      国际业务部

    table3 (result)

    部门dep 一月份      二月份      三月份
    --------------------------------------
          01      10        null      null
          02      10         8        null
          03      null       5        8
          04      null      null      9

    ------------------------------------------
    1)
    select a.部门名称dname,b.业绩yj as '一月份',c.业绩yj as '二月份',d.业绩yj as '三月份'
    from table1 a,table2 b,table2 c,table2 d
    where a.部门dep = b.部门dep and b.月份mon = '一月份' and
    a.部门dep = c.部门dep and c.月份mon = '二月份' and
    a.部门dep = d.部门dep and d.月份mon = '三月份' and
    2)
    select a.dep,
    sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
    sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
    sum(case when b.mon=3 then b.yj else 0 end) as '三月份',
    sum(case when b.mon=4 then b.yj else 0 end) as '四月份',
    sum(case when b.mon=5 then b.yj else 0 end) as '五月份',
    sum(case when b.mon=6 then b.yj else 0 end) as '六月份',
    sum(case when b.mon=7 then b.yj else 0 end) as '七月份',
    sum(case when b.mon=8 then b.yj else 0 end) as '八月份',
    sum(case when b.mon=9 then b.yj else 0 end) as '九月份',
    sum(case when b.mon=10 then b.yj else 0 end) as '十月份',
    sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',
    sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',
    from table2 a left join table1 b on a.dep=b.dep

     ==================================================================

     XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("userinfo.xml"));
            XmlElement root=xmldoc.DocumentElement;
            XmlNodeList nodelist = root.SelectNodes("//user");
            DataSet ds = new DataSet("myds");
            DataTable tb = new DataTable("userinfo");
            ds.Tables.Add(tb);
            ds.Tables[0].Columns.Add("UserID", typeof(int));
            ds.Tables[0].Columns.Add("UserName", typeof(string));
            ds.Tables[0].Columns.Add("UserPwd", typeof(string));
            ds.Tables[0].Columns.Add("Email", typeof(string));
            ds.Tables[0].Columns.Add("DetailedID", typeof(string));

            foreach(XmlNode node in nodelist)
            {
                DataRow row=ds.Tables[0].NewRow();
                row["UserID"] = node.SelectSingleNode("UserID").InnerText;
                row["UserName"] = node.SelectSingleNode("UserName").InnerText;
                row["UserPwd"] = node.SelectSingleNode("UserPwd").InnerText;
                row["Email"] = node.SelectSingleNode("Email").InnerText;
                row["DetailedID"] = node.SelectSingleNode("DetailedID").InnerText;
                ds.Tables[0].Rows.Add(row);
            }
            GridView gv = new GridView();
            gv.DataSource = ds.Tables[0];
            gv.DataBind();
            from1.Controls.Add(gv);

  • 相关阅读:
    Arch 真好用
    Spring 自定义注解-字段注解
    Raft论文概述
    Raft成员变化(Membership Change)
    Reactor模式详解
    高性能IO之Reactor模式
    WinFrm中多线程操作窗体属性
    Reactor模式
    高并发中的线程与线程池
    二层交换机与三层交换机区别详解!
  • 原文地址:https://www.cnblogs.com/jazzka702/p/1503567.html
Copyright © 2011-2022 走看看