sql server t-sql语句总结

1.声明变量
declare @name nvarchar(10)–声明
set @name=‘武大’–赋值
print @name–输出
2.查看全局变量
print @@servername
select @@version–查看当前数据库版本

select * from ClassInfo
insert into ClassInfo values(‘四不像’);
select @@IDENTITY–最近的insert语句的标识
print @@rowcount–返回上一执行受影响的行数
3.选择语句
declare @id int
set @id=10
if @id>5
begin
–满足条件时,执行如下代码
print ‘ok’
end
else
begin
–不满足条件时,执行如下代码
print ‘no’
end
4.循环语句
declare @id int
set @id=1
while @id<10
begin
print @id
set @id=@id+1
end
–输出1-10之间的所有偶数
declare @num int
set @num=1
while @num<11
begin
if @num%2=0
begin
print @num
end
set @num=@num+1
end
5.异常处理
begin try
delete from ClassInfo
end try
begin catch
print @@error
end catch

猜你喜欢

转载自blog.csdn.net/huipingx/article/details/85013439