下面将为您介绍sql变量从select,insert获取值的单值情况及记录集情况的操作,供您参考,希望对您学习sql变量会有所帮助。
use Northwind
--整形sql变量的赋值
declare @addr int
set @addr = (select sum(power(2,cast(EmployeeID as integer))) as addr from employees )
print '员工id的平方和为:'+cast(@addr as varchar(10))
--浮点数sql变量的赋值
declare @total float
set @total = (select sum(freight) as freight from Orders)
print '所有商品的总运费为:'+cast(@total as varchar(10))
--记录集sql变量的赋值
declare @tb table(id int,name varchar(20),country varchar(20))
insert into @tb select employeeid,firstname+' '+lastname,country from employees
select * from @tb
select a.name,b.customerId,b.freight from @tb a,orders b where a.id=b.employeeid
关键点:单值情况下,select一定要用括号
【编辑推荐】