Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi All,

I have a gridview that displays data about users and their mobile numbers. It can have 100 records in it, and I have a text box that takes mobile count. By that mobile count, I will display the grid according to its size.
Example: I have 100 records and I want to see 10 only then I will write 10 in the text box and the grid will display top 10 only.

I tried to make that:
create proc test
(@mobilecount bigint)
as
begin
select top @mobilecount from tbl_mobiles 

Any help?
Thanks

UPDATE:
Solved by OP herself.
Posted
Updated 19-Jan-11 20:36pm
v3

Use Exec()[^] to achieve dynamic execution.

SQL
create proc test
(@mobilecount bigint)
as
begin
Declare @SQL varchar(max)
SET @SQL = 'select top '+@mobilecount+' from tbl_mobiles'
EXEC(@SQL)
 
Share this answer
 
Comments
Hiren solanki 20-Jan-11 2:04am    
Wel come my downvoter friend come and downvote it using different account too.
Sandeep Mewara 20-Jan-11 2:35am    
Looks like you know your admirer pretty well! :)
How did you pissed him/her of so much?
Rajesh Anuhya 20-Jan-11 2:53am    
Can you know who is down voting??? (mentor). My answers are also down voting..
Sandeep Mewara 20-Jan-11 3:18am    
No yaar, no idea.
Hiren solanki 20-Jan-11 3:21am    
We can't know about who's downvoting. it's top secret for all of us.
alter proc test
(
@mobilecount bigint
)
as
begin
declare @n int
set @n=@mobilecount
select top (@n) mobile,ownerName  from 
tbl_mobiles
end
 
Share this answer
 
v2
Comments
Hiren solanki 20-Jan-11 2:16am    
Sorry But I didn't aware about variable inside top works. It's all correct
Sandeep Mewara 20-Jan-11 2:34am    
Why do you think it won't work? It surely will.

Have you executed it and seen?
Hiren solanki 20-Jan-11 3:24am    
Oh my bad!! I understood the query little differently and I don't have an idea of varible executing works in normal query too in TOP.
Sandeep Mewara 20-Jan-11 3:33am    
Well, this is one of the advantages here... we get to learn new things! :)
Hiren solanki 20-Jan-11 3:34am    
That's why I am not leaving CP after getting this much down vote :laugh:

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