然后再向表里插入数据
insert into u_tab(u_no,name, age)
select 'jd_001','a01','25' union
select 'jd_002','a02','22' union
select 'jd_003','a03','26' union
select 'jd_004','a04','23' union
select 'jd_005','a05','24' union
select 'lm_001','b01','28' union
select 'lm_002','b02','65' union
select 'lm_003','b03','76'
定义table类型的变量和创建表、变量的许多地方都是相同的。
declare @utab table (uid int IDENTITY , u_no varchar(100) not null unique, name varchar(100) not null, age varchar(50) not null)
insert into @utab
select * from u_tab
select * from @utab
update @utab set age = 43 where name = 'a05'
select * from @utab where u_no like 'jd_%' order by age desc
delete from @utab where age > 75
select * from @utab where age > 75
go
用到是定义变量时的declare但是后面却和创建表用的格式是相同的。但是在调用table类型变量时又体现出它具有表的某些性质。在我个人看来table类型的变量是具有表和一般变量的交集的特性。
通过编写函数来实现学习table类型的使用方法
create function select_utab (@u_no varchar(100))
returns table
as
return (
select U_no, name ,age from u_tab
where u_no = @u_no
)
编写了一个简单的内嵌表值函数,函数select_utab返回的类型便是table类型。
调用函数 select * from select_utab('jd_005')
得到结果: jd_005 a05 24
在成功调用select_utab函数后,又一次体会到了。table类型具有表的性质的一面。