Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Paging in SQL Server - Inner Join

Author  Topic 

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-10-16 : 09:06:17
Hi everybody!

I´m working in a web application, and in my project i have two tables that can be many rows.

I´trying customize a stored procedure to get only a fix number of records for page, say 20 records at every request.

I try to use ROW_NUMBER, but I get a error from SQL that says that is not permitited nested queries.

How can i do to paging the result query, using two tables, somebody could help me, send me a example or indicated a book that help me understand this problem?

Thanks

Marcos Cruz

SELECT SetorAtividade.SetorAtividadeId
,SetorAtividade.SetorAtividade
,RamoAtividade.RamoAtividadeId
,RamoAtividade.RamoAtividade
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY SetorAtividade.SetorAtividade) As row, *
FROM (
SELECT SetorAtividade.SetorAtividadeId
,SetorAtividade.SetorAtividade
,RamoAtividade.RamoAtividadeId
,RamoAtividade.RamoAtividade
FROM
SetorAtividade
LEFT OUTER JOIN
RamoAtividade
ON SetorAtividade.SetorAtividadeId = RamoAtividade.SetorAtividadeId
WHERE
(SetorAtividade.SetorAtividadeId = @SetorAtividadeId)
ORDER BY
SetorAtividade.SetorAtividade
,RamoAtividade.RamoAtividade
)) AS tbl
WHERE row >= 1 AND row < 5

shakthiA
Starting Member

12 Posts

Posted - 2009-10-16 : 09:26:52
Hi,

Try this, should work now

SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY SetorAtividade.SetorAtividade, RamoAtividade.RamoAtividade) As row, SetorAtividade.SetorAtividadeId
,SetorAtividade.SetorAtividade
,RamoAtividade.RamoAtividadeId
,RamoAtividade.RamoAtividade
FROM
SetorAtividade LEFT OUTER JOIN RamoAtividade
ON SetorAtividade.SetorAtividadeId = RamoAtividade.SetorAtividadeId
AND SetorAtividade.SetorAtividadeId = @SetorAtividadeId
) AS tbl
WHERE row >= 1 AND row < 5
Go to Top of Page

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-10-16 : 11:03:46
Hi shakthiA,

Thanks for your answer. I made the teste and the procedure run, but the result is not correct. When i pass the SetorAtividadeId = 5, for example, the query not return only the records of SetorAtividadeId 5. Show the results - column SetorAtividadeId bring others id´s:


Running [dbo].[GetRamosBySetor] ( @SetorAtividadeId = 1 ).

row SetorAtividadeId SetorAtividade RamoAtividadeId RamoAtividade
--------------------- ---------------- ---------------------------------------- --------------- ----------------------------------------
1 3 Business <NULL> <NULL>
2 2 Financial Institution <NULL> <NULL>
3 1 Industry 4 Aliment
4 1 Industry 9 E-commerce
No rows affected.
(4 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[GetRamosBySetor].


But I´m find the error and fix it only change the AND for WHERE, see:


Running [dbo].[GetRamosBySetor] ( @SetorAtividadeId = 1 ).

row SetorAtividadeId SetorAtividade RamoAtividadeId RamoAtividade
--------------------- ---------------- ---------------------------------------- --------------- ----------------------------------------
1 1 Industry 4 Aliment
2 1 Industry 9 E-commerce
3 1 Industry 15 Eletronic Commerce
4 1 Industry 3 Gas
No rows affected.
(4 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[GetRamosBySetor].


Again, thanks for your help.

Marcos Cruz
Go to Top of Page
   

- Advertisement -