在每一门的编译程序中,都离不开数据,现在我就来和大家分享一下我对SQL基础知识的小结吧,有不足,总结不到位之处,还请多多指教!

首先SQL语句不区分大小写。

一、变量的声明和赋值

通过delcare关键字来声明变量

declare 变量 数据类型

变量由变量符号(@)和变量名组成。

如: delcare @name varchar(20);

注意:在声明变量时,不能为其赋初值。可以通过set或select给变量赋值。

1,set @name = '你好';                     2, select @name = 'Hello';

值的打印-------print或select

1, print @name                                                 2,select @name

不同之处:print是在消息中打印结果,select是在网格中打印结果

例子:

  declare @name varchar(20)
      set @name  = '中国'
      print @name

二、数据类型的转换

字符串和其他类型数据进行拼接元算的时候,需要显示的把其他类型转换为字符串类型。
    1,通过 cast(expression as datatype)
    2,通过 convert(datatype ,expression)   

如:
    declare @name varchar(20)
    declare @age int
    set @name =  '啊Q'
    set @age = 34
    print '你得名字是'+@name+'你得年龄是'+cast(@age as varchar)

三、运算符

  =,+,-,*,/,%,>=,<=,!=,<>,>,<,and,or
    =:如果是在条件中代表是否相等,如果在赋值语句中则是赋值运算符
    没有++,--,==
      declare @a int
      declare @b int
      set @a=1;
      set @b =1;
      print @a+@b  
    **** 如果需要++,--, 则必须使用set,set @i=@i+1;

四、语句

1,if语句

   if ()
   begin
  
   end
   else
   begin
  
   end

2,while语句

   while()
   begin
     --循环变量的自加
   end

如:

  请输出100个数字
     declare @i int;
     set @i =1;
     while(@i<=100)
     begin
     print @i
    set @i=@i+1
      end

3,case语句

 ①与定值比较

declare @i int
set @i = 7;
declare @result varchar(20)
set @result = case @i
              when 8 then 'A'
              when 7 then 'B'
              else 'C'
              end
 print @result

②与变量比较

declare @i int
set @i = 67
declare @result varchar(20)
set @result = case
              when 56<@i and @i<67 then 'A'
              when @i>67 then  'b'
              else  'c'
              end

print @result

 

 这是学习SQL最基础的知识,好了,今天就先到这里吧,接下来我会和大家分享一下对数据库的操作。

 

 

 

posted on 2012-10-24 20:20  午后の時間  阅读(128)  评论(0编辑  收藏  举报