SQL server變數的定義
阿新 • • 發佈:2020-09-09
在SQL server中定義變數
create table stduser( id int, name varchar, age int, gender varchar ); insert into stduser(id,name,age,gender) values(1, '唐玄宗',125,'man'); insert into stduser(id,name,age,gender) values(2, '李高宗',453,'woman'); insert into stduser(id,name,age,gender) values(3, '唐太宗',412,'man'); insert into stduser(id,name,age,gender) values(4, '宋高宗',652,'man'); insert into stduser(id,name,age,gender) values(5, '慈溪',534,'man'); insert into stduser(id,name,age,gender) values(6, '賈寶玉',234,'man'); insert into stduser(id,name,age,gender) values(7, '林黛玉',564,'woman'); insert into stduser(id,name,age,gender) values(8, '薛寶釵',143,'man'); insert into stduser(id,name,age,gender) values(9, '王羲之',112,'woman'); insert into stduser(id,name,age,gender) values(10,'吳廣',129,'woman'); insert into stduser(id,name,age,gender) values(11,'韓非',123,'man'); insert into stduser(id,name,age,gender) values(12,'荀子',121,'man'); insert into stduser(id,name,age,gender) values(13,'孟子',120,'man'); insert into stduser(id,name,age,gender) values(14,'老子',122,'man'); insert into stduser(id,name,age,gender) values(15,'衛莊',125,'man'); insert into stduser(id,name,age,gender) values(16,'農夫山泉',128,'man'); select * from stduser where gender='woman' order by age asc; --定義變數 使用關鍵字declare declare @num int; --變數賦值的兩種方式 --1 使用select 給變數賦值,將從表中查出來的值賦給變數,變數型別和表中對應的欄位型別必須可以進行轉換。 select @num = age from stduser where id=1; print(@num); select @num = 11; print(@num); --2 使用set 給變數賦值 set @num = 10; print(@num);