SQLSERVER格納過程基本文法(上)

2535 ワード

一、変数を定義する
--単純な割り当て
declare @a int
set @a =5
print @a  
 
  --  select      
   
  

declare @user1 nvarchar( 50)
select @user1 = '   '
print @user1
declare @user2 nvarchar( 50)
select @user2 = Name from ST_User where ID =1
print @user2  
--udate で を ける
declare @user3 nvarchar( 50)
update ST_User set @user3 = Name where ID =1 --        , set    
print @user3
、 、
-- Select INTO                  。          ,        Select   。
--        Select         (  )        。
select * into NewTable from Uname

--  ABC     ,   Uname     Username     ABC
Insert INTO ABC Select Username FROM Uname

--     (##  )        ,                       。
--              ,         ,     drop table #Tmp(   drop table ##Tmp)        。
 Create TABLE #temp (
     UID int identity( 1, 1 ) PRIMARY KEY,
     UserName varchar (16),
     Pwd varchar (50),
     Age smallint ,
     Sex varchar (6)
 )
サイクル
--while       
print @ sum
declare @a int
declare @sum int
set @a =1
set @sum =0
while @a <10
begin
/*set @sum=@sum+@a
set @a=@a+1*/
set @sum +=@a  --         ,            ;        
set @a +=1
end
print @sum
--if,else    
if(1 +1= 2)
begin
     print '  '
end
else
begin
     print '  '
end
--case when then else end     
declare @today int
declare @week nvarchar( 3)
set @today =3
set @week = case
     when @today =1 then '    '
     when @today =2 then '    '
     when @today =3 then '    '
     when @today =4 then '    '
     when @today =5 then '    '
     when @today =6 then '    '
     when @today =7 then '    '
     else '    '
end
print @week