Sunday, February 19, 2012

Help me in this Query

hello all,
I have a table with the Following structure. .. . .

Catid CategoryName Desc
_________________________________________
1 A Hello all Welcome
2 A Thanks For all
3 B Did u come yesterday ?
4 B Shall we got out.. ?

now i need a procedure which will accept a Categoryname as Arguement and will return the "desc " First time First record, second time second record,and so on depending on the N number of entries in each and evry category..
Please help me how do i go about..
I guess some how i am not getting any ideas..
Help me..

saiIS this what you are looking for?

create table #Category(CatID int,CategoryName varchar(10),[Desc] varchar(30))
go
insert into #Category values(1, 'A', 'Hello all Welcome')
insert into #Category values(2, 'A', 'Thanks For all')
insert into #Category values(3, 'B', 'Did u come yesterday ?')
insert into #Category values(4, 'B', 'Shall we got out.. ?')
go
select * from #Category
go
create procedure #GetCat(
@.CategoryName varchar(10),
@.Desc varchar(30) = Null)
as
select min([Desc])
from #Category
where CategoryName = @.CategoryName
and ([Desc] > @.Desc or @.Desc is null)
return 0
go
exec #GetCat 'A'
exec #GetCat 'A','Hello all Welcome'
exec #GetCat 'A','Thanks For all'
exec #GetCat 'B'
exec #GetCat 'B','Did u come yesterday ?'
exec #GetCat 'B','Shall we got out.. ?'
go|||hi Paul,
Thanks for the reply.

yeah i am looking for something similar but not the same..

I need like below..

exec #getdat 'A'
should give me 'Hello all WElcome'
and again if i execute the same (i.e.) exec #getdat 'A'
then it should give me 'Thanks .. '
and again if i execute 'hello all welcome '
like this...
i am looking for something like Rand function do we have any ?

Thanks
sai|||what's a RAND function?|||What about

select top 1 *
from #Category
where CategoryName=@.CategoryName
order by NEWID()

You also can order by CHECKSUM(NEWID()) for more random distribution of results.|||Thanks a lot Ispaleny... It solved my purpose...

thanks and regards
SAI

No comments:

Post a Comment