Click here to Skip to main content
15,889,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a query that select data from multiple tables.
first i select 100 records from table and next i want to select next 100 records based on particular column like RegNo in my case;
i use subquery approach but subquery did not allow mulitple columns.

my query is this
SQL
Select Top 100
RegNo,
PatientName,
FCNo,
RelationName,
G.Description Gender,
DATEDIFF(YYYY,DOB,GETDATE())'Age',
R.Deleted

FROM PRS.dbo.tblRegistration R
LEFT OUTER JOIN TKC.dbo.tblGender G ON R.GenderID=G.GenderID

WHERE
Regno NOT IN
(
  SELECT TOP 0
  Regno RegNo,
  PatientName,
  FCNo,
  RelationName,
  G.Description Gender,
  DATEDIFF(YYYY,DOB,GETDATE())'Age',
  R.Deleted

  FROM PRS.dbo.tblRegistration R
  LEFT OUTER JOIN TKC.dbo.tblGender G ON R.GenderID=G.GenderID
  ORDER BY Regno
)

ORDER BY Regno


in subquery where there is 0, i use a variable that change with every iteration.
like 0 then 99 than 199 and so on....
Thanks in advance
Posted
Updated 13-Jan-12 21:13pm
v2
Comments
manognya kota 14-Jan-12 4:17am    
Here, in the sub query WHERE
Regno NOT IN
(
SELECT TOP 0
Regno RegNo,
PatientName,
FCNo,
RelationName,
G.Description Gender,
DATEDIFF(YYYY,DOB,GETDATE())'Age',
R.Deleted

FROM PRS.dbo.tblRegistration R
LEFT OUTER JOIN TKC.dbo.tblGender G ON R.GenderID=G.GenderID
ORDER BY Regno
)
The sub query select should have only the regno field.As you are comparing with re no not in ().

hope its clear.

1 solution

Hi,


Try sequencing your select query result.

Check this code,

SQL
WITH NumberedMyTable AS
(
    SELECT
        Id,
        Value,
        ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
    FROM
        MyTable
)
SELECT
    Id,
    Value
FROM
    NumberedMyTable
WHERE
    RowNumber BETWEEN @From AND @To



Also, Your condition checks..
SQL
WHERE
Regno NOT IN
(
  SELECT TOP 0
  Regno RegNo,
  PatientName,


The select here should have only Regno in the query.


Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900