在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);