请问如何在存储过程返回的结果集上进行查询

金汐 2010-03-18 11:25:10
我在做一个项目,里面80%的界面都要分页,我写了个通用的分页存储过程,返回联接后的结果.我在其他的存储过程中要对分页存储过程的结果进行查询.该如何做呢?
我这样做语法不对:

USE JJDJ
GO
IF object_id('p_GetAllCard_Users1') is not null
DROP PROC dbo.p_GetAllCard_Users1;
GO
CREATE PROC dbo.p_GetAllCard_Users1
@RecordsCount int output,
@PageCount int output,
@MaxTime datetime='9999/1/1'
AS
SELECT [CardId]
,[CardLiveId]
,[CardLiveName]
,[CardPwd]
,[Balance]
,[Score]
,[CardStateId]
,[CardStateName]
,[StartTime]
,[EndTime]
,[UpdateTime]
,[Creator]
,[CreatorName]
,[CreateTime]
,[CardValue]
,[BatchId]
,[CommendCardId]=isnull([CommendCardId],'')
,[UserId]=isnull([UserId],'')
,[UserName]=isnull([UserName],'')
,[MobilePhone]=isnull([MobilePhone],'')
,[Sex]=isnull([Sex],'')
,[Birth]=isnull([Birth],@MaxTime)
,[Address]=isnull([Address],'')
,[Postcode]=isnull([Postcode],'')
,[Email]=isnull([Email],'')
,[TelPhone]=isnull([TelPhone],'')
FROM (exec dbo.P_GetByPage 'v_card','cardId',1,10,null,@RecordsCount,@PageCount) a
;
GO
...全文
406 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
金汐 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sql77 的回复:]
引用 4 楼 jyfgyksm 的回复:
引用 2 楼 fredrickhu 的回复:
储存过程调用存储过程的3种方法




这个用临时表的话可以直接insert into #tempTable exec GetUserName
吗?是不是一定要先创建一个列和存储过程结果集一样的列才可以这样呢?

是的,要不然只能是OPENROWSET
[/Quote]

...明白了.
金汐 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 guguda2008 的回复:]
不能这么写,如果存储过程中没有INSERT,DELETE之类的语句,可以把存储过程改成表函数
[/Quote]

有道理,郁闷的是我的分页存储过程需要输出总记录数和总页数的,需要输出参数.而且表名和列名及查询条件全是通过参数动态拼接的.
SQL77 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jyfgyksm 的回复:]
引用 2 楼 fredrickhu 的回复:
储存过程调用存储过程的3种方法




这个用临时表的话可以直接insert into #tempTable exec GetUserName
吗?是不是一定要先创建一个列和存储过程结果集一样的列才可以这样呢?
[/Quote]
是的,要不然只能是OPENROWSET
金汐 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
储存过程调用存储过程的3种方法


[/Quote]

这个用临时表的话可以直接insert into #tempTable exec GetUserName
吗?是不是一定要先创建一个列和存储过程结果集一样的列才可以这样呢?
guguda2008 2010-03-18
  • 打赏
  • 举报
回复
不能这么写,如果存储过程中没有INSERT,DELETE之类的语句,可以把存储过程改成表函数
--小F-- 2010-03-18
  • 打赏
  • 举报
回复
储存过程调用存储过程的3种方法
--小F-- 2010-03-18
  • 打赏
  • 举报
回复
第一种方法: 使用output参数

USE AdventureWorks;
GO
IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL
DROP PROCEDURE Production.usp_GetList;
GO
CREATE PROCEDURE Production.usp_GetList @product varchar(40)
, @maxprice money
, @compareprice money OUTPUT
, @listprice money OUT
AS
SELECT p.name AS Product, p.ListPrice AS 'List Price'
FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice;
-- Populate the output variable @listprice.
SET @listprice = (SELECT MAX(p.ListPrice)
FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice);
-- Populate the output variable @compareprice.
SET @compareprice = @maxprice;
GO


另一个存储过程调用的时候:

Create Proc Test
as
DECLARE @compareprice money, @cost money
EXECUTE Production.usp_GetList '%Bikes%', 700,
@compareprice OUT,
@cost OUTPUT
IF @cost <= @compareprice
BEGIN
PRINT 'These products can be purchased for less than
$'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
PRINT 'The prices for all products in this category exceed
$'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二种方法:创建一个临时表

create proc GetUserName
as
begin
select 'UserName'
end

Create table #tempTable (userName nvarchar(50))
insert into #tempTable(userName)
exec GetUserName

select #tempTable

--用完之后要把临时表清空
drop table #tempTable--需要注意的是,这种方法不能嵌套。例如:

procedure a
begin
...
insert #table exec b
end

procedure b
begin
...
insert #table exec c
select * from #table
end

procedure c
begin
...
select * from sometable
end

--这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。


第三种方法:声明一个变量,用exec(@sql)执行:

1);EXEC 执行SQL语句

declare @rsql varchar(250)
declare @csql varchar(300)
declare @rc nvarchar(500)
declare @cstucount int
declare @ccount int
set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
--exec(@rsql)
set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
set @rc=@csql+@rsql
exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法
--select @csql+@rsql
--select @cstucount



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧