sqlserver存储过程 赋值
select top 1 [name],ver,count(*) from t_credit where [name] = @name group by [name],ver having count(*) > 1 order by ver desc
存储过程:如何判断是否有记录?如何取出变量 ver?
------解决方案--------------------试试这个:
create proc dbo.xxx
@ver int output --输出参数
as
select top 1 @ver = count(*) from t_credit where [name] = @name
group by [name],ver having count(*) > 1
order by ver desc
go
declare @ver int;
exec dbo.xxx @ver output
select @ver
------解决方案--------------------
-- 如何判断是否有记录?
select @@rowcount
-- 如何取出变量 ver?
declare @v [ver数据类型]
select top 1 @v=ver
from t_credit
where [name]=@name
group by [name],ver
having count(*)>1
order by ver desc
------解决方案--------------------一般用
if (select count(1) from xxx where )>0
有数据
else
没数据
------解决方案--------------------
你的把select top 1 中的字段就只能有一个ver,其他的字段都得去掉才行的
------解决方案--------------------像下面这样定义一个变量,然后你就可以在赋值后随便使用了
declare @ver int
select @ver=count(*) from t_credit where [name] = @name group by [name],ver having count(*) > 1
------解决方案--------------------select
set 都可以赋值..
declare @ver int
--第一种 select --
select @ver=count(*) from t_credit where [name] = @name group by [name],ver having count(*) > 1
--第二种 set --
set @ver=(select count(*) from t_credit where [name] = @name group by [name],ver having count(*) > 1 )