SQL基礎1
declare @city char(11) --聲明局部變量
select @city = 'Welcome to changsha' --一次隻能給一個變量賦值
set @city = 'hehe' --一次可以給多個變量賦值
select @city --顯示變量
select @@version --顯示版本信息
select @@rowcount --顯示受最近一條語句影響的行數
select ceiling(13.4), --返回14--不小於13.4的最小整數
floor(13.4), --返回13--不大於13.4的最大整數
round(13.45678, 2)--結果四舍五入,保留2位小數,輸出為4位,其餘補0
declare @strInfo varchar(40)
set @strInfo = 'Welcome to China, I love my Country.'
select lower(substring(@strInfo, 1, 6)) as lower,
upper(substring(@strInfo, 9, 5)) as upper
--LTRIM刪除字符串左邊的空格
declare @strInfo varchar(33)
set @strInfo = ' I love you '
select ltrim(@strInfo)
--RTRIM刪除字符串右邊的空格
select rtrim(@strInfo)
select left('thank you', 5)--返回字符串左邊的5個字符
select right('thank you', 5)--返回字符串右邊的5個字符
--刪除第一個字符串從第一個位置開始的5個字符,
--然後將第二個字符串插入第一個字符串刪除的起始位置
select stuff('thank you', 1, 5, 'thank')
select reverse('1sads')--反轉字符串
declare @i smallint, @sum int, @count int
set @i = 1
set @sum = 0
set @count = 0
while (@i <= 100)
begin
if (@i % 3 = 0)
begin
set @count = @count + 1
set @sum = @sum + @i
end
set @i = @i + 1
end
print str(@count) + ',' + str(@sum)
use blog
go
waitfor delay '00:00:03' --指定的等待時間格式必須是'hh:mm:ss'
select * from userinfo
--求1~100的和
declare @i smallint, @sum smallint
set @i = 1
set @sum = 0
Label:
if (@i <= 100)
begin
set @sum = @sum + @i
set @i = @i + 1
goto Label
end
print @sum
--計算20!
declare @sum bigint, @count int
set @sum = 1
set @count = 1
Label:
set @sum = @sum * @count
set @count = @count + 1
if (@count <= 20)
goto Label
print @sum
最後更新:2017-04-02 00:06:52