go与变量

这里写图片描述
第二章
create table t
(
id int primary key identity(1,1)
)

create table t1--出错
(
    id int primary key identity(1,1),
    kid int references t(kid)
)
go
create table t2--正常
(
    id int primary key identity(1,1),
    kid int references t(id)
)

/——————————————–变 量 分 类—————————————————–/
/–局部变量–/
–声明变量
–DECLARE @变量名 数据类型
DECLARE @id char (10) –声明一个长度为个字符的变量id
DECLARE @age int –声明一个存放职员年龄的整型变量

    --变量赋值 
        --SET @变量名 = 值 :用于普通的赋值
        SET @age = 20
        --SELECT  @变量名 = 值:用于从表中查询数据并赋值
        SELECT @id = '11111'

    --使用变量          
        --找王五学号前后的同学
        declare @sid int 
        select @sid = stuid  from StuInfo where stuname='王五'
        print '王五的学号为:' + convert(varchar(20),@sid)
        select * from StuInfo where stuid=@sid-1 or stuid=@sid+1

        --注意:使用select 进行赋值时如果查询到的是一个结果集 那么变量得到的值是最后一条记录         
        --查询表中学号最小的学生姓名。
        declare @stuname  varchar(20)
        select @stuname = stuname from StuInfo order by stuid desc
        print @@error --错误代号
        print @stuname

/*--全局变量--*/
    --是以@@全局变量名   全局变量只能使用,由系统定义。我们不能更改和定义   @@ERROR
    --@@ERROR   最后一个T-SQL错误的错误号
    --@@IDENTITY    最后一次插入的标识值
    --@@ROWCOUNT    受上一个SQL语句影响的行数
    PRINT @@IDENTITY

/——————————————–输 出 语 句—————————————————–/
–print 变量或表达式:以消息形式进行显示
PRINT ‘数据库服务器名:’ + @@SERVICENAME
print 15 * 8

--select 变量或表达式:以表格形式进行显示
    SELECT 15 * 8 
    select '数据库服务器名:' + @@SERVICENAME

--强制类型转换 convert(要转成的数据类型,要转换的值)

/——————————————–逻 辑 控 制—————————————————–/
/–分支结构–/
–1.IF-ELSE语句
–if(条件)
– begin
– T-SQL语句
– end
–else if (条件)
– begin
– T-SQL语句
– end
–else
– begin
– T-SQL语句
– end

        ---统计男生的平均成绩和女生的平均成绩
        declare @avgman float
        declare @avggirl float
        select @avgman=avg(score) from StuMarks,StuInfo  where   StuInfo.stuid=StuMarks.stuid   and stusex='男'
        select  @avggirl=avg(score) from StuMarks,StuInfo where   StuInfo.stuid=StuMarks.stuid  and stusex='女'
        if (@avgman>@avggirl)
            begin
                print '男生优于女生'
                --获取男生第一名的成绩
                select top 1 sum(score) as '总分',StuInfo.stuid,stuname  from StuMarks ,StuInfo where StuInfo.stuid=StuMarks.stuid and stusex='男'            
                group by StuInfo.stuid,stuname  order by '总分' desc
            end
        else if(@avgman<@avggirl)
            begin
                print '女生优于男生'
                --获取男生第一名的成绩
                select top 1 sum(score) as '总分',StuInfo.stuid  from StuMarks ,StuInfo where StuInfo.stuid=StuMarks.stuid and stusex='女'            
                group by StuInfo.stuid  order by '总分' desc
            end
        else
            begin
                print '男女平等'
            end

    --2.CASE-END语句
        --CASE    
        --  WHEN   条件  then   结果
        --  WHEN   条件  then   结果
        --  [ELSE 结果]
        --END

        --成绩分等级
        select stuname as 姓名,成绩 = case
                when score >= 90 then 'A'
                when score >= 80 then 'B'
                when score >= 70 then 'C'
                when score >= 60 then 'D'
                else 'E'
            end
        from StuInfo,StuMarks where StuInfo.stuid = StuMarks.stuid and  [subject] = 'SQL'

/*--循环控制语句--*/
    --while(循环控制条件)
    --  begin 
    --      T-SQL语句
    --  end

    declare @mark int,@markid int
    select @mark = score,@markid = StuMarksno  from StuMarks where subject = 'html'
    while @mark < 90
        begin
            update StuMarks set score = @mark+1 WHERE StuMarksno = @markid
            select @mark = score,@markid = StuMarksno  from StuMarks where subject = 'html'
        end
    print @score --90
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值