mssql:t-sql;创建表;给表添加约束;使用变量;事务,索引,视图;存储过程;触发器trigger;播放器http://www.smartgz.com/blog/Article/956.asp

1 t-sql;
   select * from table1;
   insert into table1(name,pwd) values('csj','csj');
   update table1 set  pwd='css'  where name='csj';
   delete from table1  where name='csj';
   select top 3 * from  stu order by stuno desc;   --desc降序排列,asc为升序;

   定义别名:select name as names from users
  输出语句:print 'csj' + @@rowcount
  将数值转换成字符串:convert(varchar(5),@@error);
  最后插入的标识值:@@identity
 批处理语句:GO是批处理的结束;
   条件:in; not in;=;<>;
   exists;not exists
   declare  @age int
 select  @age=stuage  from stuinfo where stuname='csj'
   select  *  from stuinfo where stuname=@age

   逻辑控制语句:
  1 条件:
    if(@age>90)
               语句块
           else
                语句块

   if(条件)
    begin
                语句1
                语句2
                 .......
           end
        else
            begin
                语句1
                语句2
                 .......
              end
       2  循环语句
      while(@age > 90)
               begin
                    语句1
                    语句2
                     .......
                    end
         3 case 多分支语句
                   select stuno, case
                                                  when  age1=90  then 结果1
                                                  when  age1=80  then 结果2
                                       end
                    from  stum
       

数据库文件的组成:
  主数据库文件:*.mdf
  次要数据文件:*.ndf
  日志文件:*.ldf
      if(select * from sysdatabase where name=db1
      create database db1
      drop database db1
2 创建表
 use master    --设置当前数据库为master
 if  exists (select * from sysobjects where name='table1')  --检查要创建的表是否存在,若存在删除;
      drop table table1
   create table table1
    (
     user char(7)   not null,
     pwd char(6)  not null
    )

   drop table table1
  
给表添加约束
  约束类型:目的是确保表中数据的完整性;
  主健约束:要求主健列唯一且不为空;
  唯一约束:要求列唯一可为空,只有一个空值;
  检查约束:某列取值范围限制,格式限制等;
  默认约束:某列默认值,如男性较多,默认为男;

  添加约束:alter table table1
                    add constraint  pk_name   primary key (name)  --添加主健约束,名称作为主健;
                    alter table table1
                    add constraint  pk_name  unique (name)  --添加唯一约束,名称不能重复;
                   alter table table1
                   add constraint df_address default('地点不详') for saddress --添加默认约束,地点不详;
                    alter table table1
                   add constraint  ck_age  check (ages between 20 and 60)--添加检查约束,要求年龄在20-60间;
                    alter table table1
                   add constraint  fk_stuno foreing  key(stuno)  references table2(stuno) --添加外健约束,主表table2和从表table1建立关系,通过stuno;

  删除约束: alter table table1
                      drop constraint   pk_name;

4 使用变量
   局部变量的名称必须用@作前缀;
  全局变量的名称必须用@@作前缀;
  declare  @username varchar(8)
     set @username='csj'
     select * from table1 where username=@username
 

  全局变量的名称必须用@@作前缀;
   @@ROWCOUNT受一个SQL语句景响的行数;
  @@ERROR最后一个T-SQL   错误的错误号;

事务,索引,视图;
    开始事务:begin transaction
  提交事务:commit transaction
  回滚事务:rollback transaction
 
      begin transaction
          declare  @errorsum int
          语句块
          set @errorsum=@errorsum + @@error
    if @errorsum <>0
                begin
                   rollback  transaction
                end
        else
               begin
                   commit transaction
               end
           ---------------------------------------
       视图
       create view  view_name
       as
            select * from   sql语句
 --------------------
  存储过程
       exec proc_name  60,'csj'

         if exists (select * from sysobjects where
   create proc  proc_name
         @no  int,
         @name
         as
            语句块
  ---------------------------------------------
   触发器:是在对表执行插入,更新,删除操作时自动执行的存储过程; 
    if exists (select  name from sysobjects where name='trigger_name')
        drop trigger  trigger_name

    create trigger  trigger_name
    on table_name
    for insert / delete/update
    as
      语句块
-----------------------------------------------------------------
create trigger  trigger_name
    on table_name
    for insert 
    as
   if(@type='zhiqu')
     update bank set currentmoney=currentmoney -@outmoney  where cordid=@id
   else
      update bank set currentmoney=currentmoney +@outmoney  where cordid=@id
go
-- 当向这个表插入记录时,有自己动启动触发器;

创建删除触发器;
创建更新触发器;

========================================
游标:
使用游标有四种基本的步骤:声明游标、打开游标、提取数据、关闭游标。
 1 声明游标:
DECLARE CustomerCursor CURSOR FOR
SELECT acct_no,name,balance
FROM customer
WHERE province="北京";
 2 打开游标
OPEN CustomerCursor;
3  提取数据
 当用OPEN语句打开了游标并在数据库中执行了查询后,您不能立即利用在查询结果集中的数据。您必须用FETCH语句来取得数据。一条FETCH语句一次可以将一条记录放入程序员指定的变量中。事实上,FETCH语句是游标使用的核心
4 关闭游标 :CLOSE CustomerCursor

 


 原理:游标就是把数据按照指定要求提取出相应的数据集,然后逐条进行数据处理。
      1.1游标的概念
       游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。
       1.2 游标的优点
      从游标定义可以得到游标的如下优点,这些优点使游标在实际应用中发挥了重要作用:
        1)允许程序对由查询语句select返回的行集合中的每一行执行相同或不同的操作,而不是对整个行集合执行同一个操作。
        2)提供对基于游标位置的表中的行进行删除和更新的能力。
        3)游标实际上作为面向集合的数据库管理系统(RDBMS)和面向行的程序设计之间的桥梁,使这两种处理方式通过游标沟通起来。
       1.3 游标的使用
 讲了这个多游标的优点,现在我们就亲自来揭开游标的神秘的面纱。
 使用游标的顺序: 声名游标、打开游标、读取数据、关闭游标、删除游标。
       1.3.1声明游标
       最简单游标声明:DECLARE <游标名>CURSOR FOR<SELECT语句>;
       其中select语句可以是简单查询,也可以是复杂的接连查询和嵌套查询

       1.3.2 打开游标
       非常简单,我们就打开刚才我们声明的游标mycursor
       OPEN mycursor      
       1.3.3读取数据
        FETCH [ NEXT | PRIOR | FIRST | LAST] FROM { 游标名  | @游标变量名 } [ INTO @变量名 [,…] ]
        参数说明:
        NEXT   取下一行的数据,并把下一行作为当前行(递增)。由于打开游标后,行指针是指向该游标第1行之前,所以第一次执行FETCH NEXT操作将取得游标集中的第1行数据。NEXT为默认的游标提取选项。
        INTO @变量名[,…]  把提取操作的列数据放到局部变量中。
        列表中的各个变量从左到右与游标结果集中的相应列相关联。
        各变量的数据类型必须与相应的结果列的数据类型匹配或是结果列数据类型所支持的隐性转换。变量的数目必须与游标选择列表中的列的数目一致。       
        1.3.4关闭游标
        CLOSE mycursor             
        1.3.5删除游标
        DEALLOCATE mycursor 
 

本文来源于Woody的鸟窝(Woody's Blog) http://www.smartgz.com, 原文地址:http://www.smartgz.com/blog/Article/976.asp


 给出具体的例子:
declare @id nvarchar(20)  --定义变量来保存ID号
declare @A float                  --定义变量来保存值
declare mycursor cursor for select * from tb_c   --为所获得的数据集指定游标
open mycursor                   --打开游标
fetch next from mycursor  into @id,@A   --开始抓第一条数据
while(@@fetch_status=0)     --如果数据集里一直有数据
begin
 select tb_b.name,(tb_b.gz + @A) from tb_b where tb_b.id = @id   --开始做想做的事(什么更新呀,删除呀)
        fetch next from mycursor into @id,@A     --跳到下一条数据
end
close mycursor        --关闭游标
deallocate mycursor  --删除游标

实例:
 use database1
declare my_cursor cursor scroll dynamic
 /**//*scroll表示可随意移动游标指        针(否则只能向前),dynamic表示可以读写游标(否则游标只读)*/
for
select productname from  product

open my_cursor
declare @pname sysname
fetch next from my_cursor into @pname
while(@@fetch_status=0)
  begin
    print 'Product Name: ' + @pname
    fetch next from my_cursor into @pname
  end
fetch first from my_cursor into @pname
print @pname
/**//*update product set productname='zzg' where current of my_cursor */
/**//*delete from product where current of my_cursor */
close my_cursor
deallocate my_cursor

posted @ 2008-06-01 00:24  大树2  阅读(1577)  评论(0编辑  收藏  举报