如何将 COUNT(*) 值传出去?
ALTER PROCEDURE [dbo].[up_Project_GetProjCountByUserID]
@UserID int,
@ProjCount int OUTPUT
AS
SELECT COUNT(*) FROM Project_CProjects WHERE Project_CProjects.UserID = @UserID
SET @ProjCount = COUNT(*)
这样好像不行啊
------解决方案--------------------
CREATE TABLE t
(
ID INT ,
val VARCHAR(32)
)
INSERT INTO t VALUES(1,'aa')
INSERT INTO t VALUES(1,'bb')
INSERT INTO t VALUES(2,'aa')
INSERT INTO t VALUES(2,'bb')
INSERT INTO t VALUES(2,'cc')
INSERT INTO t VALUES(3,'dd')
GO
CREATE PROC sp_t
@id INT,
@Counter INT OUTPUT
AS
SELECT @Counter = COUNT(*) FROM t WHERE id = @id
GO
DECLARE @c INT
DECLARE @id INT
SET @id = 1
EXEC sp_t @id, @c OUTPUT --这里接受SP的返回值
SELECT @id 'ID', @c 'Counter'
------解决方案--------------------
你做的应该是创建一个存储过程实行 接受userid 返回该用户在表中的行数,
在SQL 2008的adventureworks 数据库里面的sales.salesorderdetail表测试了下,
--存储过程代码
create procedure project_getcountbyuserid
@userid int,
@procount int output
as
begin
select @procount=COUNT(*) from
Sales.SalesOrderDetail where SalesOrderID=@userid
end
--调用代码
declare @procount int
exec project_getcountbyuserid 43659,@procount output
select @procount
没问题!
------解决方案--------------------
create proc returncount
@intcount int output
as
select @intcount=COUNT(*) from sys.objects
declare @intcount int
exec dbo.returncount @intcount output
select @intcount