declare @c1 char(10)='山东'
declare @c2 varchar(10)='山东'
print @c1+'!'
print @c2+'!'
set @c1='山东财经大学'
declare @c3 nchar(10)=@c1
print @c3+'!'
set @c3='山东财经大学'
print @c3+'!'
declare @c4 nvarchar(10)='山东财经大学'
print @c4+'!'
set @c4=@c3
print @c4+'!'
山东 !
山东!
山东财经大 !
山东财经大学 !
山东财经大学!
山东财经大学 !
declare @x1 int=100
print @x1/3
declare @x2 decimal(6,2)=100
print @x2/3
set @x2=@x2/3
print @x2
33
33.333333
33.33
--if语句
declare @x1 int=99,@x2 int=88
if @x1>@x2
print '第一个大'
else
begin
print'第二个大'
print'可也能是未知'
end
--没有赋值,系统无法判断哪一个大,所以选择else,输出第二个大
--实际上最后一句print不是跟着else,而是单独的一句print,不管输出的是if,还是else
--运用begin和end来把另一个print执行在else里
go
--变量M和N,要求M大,N小,应当设置一个中间变量
declare @m int=50,@n int=70
if @m<@n --需要交换
begin
declare @x int
set @x=@m
set @m=@n
set @n=@x
end
select @m,@n
go
--日期
print getdate()--出的是电脑上的时间
--如果日期是今年12月31日之前,显示今年,如果是12月31日之后,显示的是明年
if GETDATE()<='2014.12.31'
print '今年'
else
print '明年'
declare @d1 smalldatetime=getdate()
print @d1+100--日期的加减(天)
print month(@d1)
--日期是1-6月,显示上半年,7-12月,显示下半年
if month(@d1)<7
print '上半年'
else
print'下半年'
--case功能应用,显示季度
declare @d1 smalldatetime=getdate()
print case
when month(@d1) between 1 and 3 then '一季度'
when month(@d1) between 4 and 6 then '二季度'
when month(@d1) between 7 and 9 then '三季度'
when month(@d1) between 10 and 12 then '四季度'
else '月份有错'
end
go
declare @d1 smalldatetime=getdate()
print case
when month(@d1) between 3 and 5 then '春'
when month(@d1) between 6 and 8 then '夏'
when month(@d1) between 9 and 11 then '秋'
else '冬'--between 1 and 2 or month(@d1)=12 then '冬'
end
declare @d1 smalldatetime=getdate()
print case month(@d1)
when 1 then '冬'
when 2 then '冬'
when 3 then '春'
when 4 then '春'
when 5 then '春'
when 6 then '夏'
when 7 then '夏'
when 8 then '夏'
when 9 then '秋'
when 10 then '秋'
when 11 then '秋'
when 12then '冬'
else '月份有错'
end
print year(getdate())--取年
print day(getdate())--取日
--循环语句:显示1-10的数字
declare@iint
set@i=1--要记住赋予初值
while(@i<=10)--循环条件跟在while之后
begin--begin。。end循环体
print@i
set@i=@i+1--循环变量的控制,不断加
end--要记住加end
--求1到10的和
declare@iint=1--初值
declare@sumint=0
while (@i<=10)--循环条件
begin
set@sum=@sum+@i
set@i=@i+1
end
print@sum