T-SQL编程基本语法

一、while 的使用  

declare @i int

  set @i=0
  while(@i<(select count(*) from test))
  begin
  print @i
  set @i=@i+1

  end

begin end用来设置执行块,里面的语句是一个执行单元

二、if  else和类型转换函数convert的使用

  declare @a int,@b int
  set @a=1 set @b=2
  if(@a>@b)
     print 'MAX:'+convert(varchar,@a)
  else
     print 'MIN:'+convert(varchar,@b)

三、临时表的使用

1、

declare @table table
(
num int,
country varchar(200),
area varchar(200)
)
begin
insert into @table values(100,'中国','亚洲')
insert into @table values(1,'小日本','亚洲')
insert into @table values(50,'俄罗斯','欧洲')
insert into @table values(40,'美国','美洲')
end
 select * from @table

2、

创建临时表    #my_table当前用户的连接(有效), ##my_table所有用户的连接(有效),

create  table  #my_table
(
   id int,
   strData varchar(20)
)

给临时表插入数据

insert into #my_table 

select classid,className from class

3、

select classid,className into #my_table1  from class

##my_table同上

4、删除临时表

drop table #table或##table

四、waitfor的使用

waitfor  delay '00:00:05'执行完五秒后再执行print ‘good!‘

waitfor  delay '00:00:05'

print ‘good!’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章