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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-27 : 09:28:01
|
| Jeremy writes "HI, I have read many posts on the newsgroups, forums, FAQs,etc and for the life of me I can't figure this one out so I apologize if this has been answered before somwhere.Currently I'm using asp to cycle thru a list of staff members on my www site (in a table called STAFF). Then while looping I call a stored procedure to get the top 10 albums last listened to (order by date desc).here is my sp:CREATE PROCEDURE spPlayLists(@name as varchar(15))ASSELECT TOP 10 Artist, Album, [Date]FROM dbo.PlaylistsWHERE (Staff = @name)ORDER BY [Date] DESCGOis there any way to make a stored procedure to loop through my staff members list and call this stored procedure above? or maybe make into one sp?I have tried: (using a cursor and loop- which returns only one staff name)CREATE PROCEDURE spAllPlayListsNew ASdeclare curse cursor for SELECT NameFROM dbo.Staffopen cursedeclare @ppl varchar(30)fetch next from curse into @pplwhile (@@fetch_status=0)beginexec ('spPlayLists ' + @ppl)fetch next from curse into @pplendclose cursedeallocate curseGOand even some other funky code (subquery? ):CREATE PROCEDURE sptest ASbegindeclare @str varchar(255)declare @ppl varchar(50)select @str='exec (''spPlayLists ' + @ppl + ')'exec (@str)endGOthanks for the help in advance." |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2002-04-27 : 10:19:50
|
Try this one:SELECT Staff.Name, Playlists.Artist, Playlists.Album, Playlists.DateFROM Staff INNER JOIN Playlists ON Staff.Name = Playlists.Staff AND Playlist.ID IN (SELECT TOP 10 ID FROM Playlists WHERE Staff=Staff.Name ORDER BY Date DESC) I'm assuming your Playlists table has a primary key.... I haven't tested the query, but this is basic structure should work fine.btw - the reason you're SP isn't working is that it exits the procedure as soon as the first SELECT statement has run.Tim |
 |
|
|
Nazim
A custom title
1408 Posts |
|
|
jrb2971
Starting Member
5 Posts |
Posted - 2002-04-29 : 13:28:55
|
| thanks that did the try with some modifications:(also would it be more efficient to use a select distinct playlists.staff instead of the other staff table?)SELECT TOP 100 PERCENT dbo.Staff.Name, dbo.Playlists.Artist, dbo.Playlists.Album, dbo.Playlists.[Date]FROM dbo.Staff INNER JOIN dbo.Playlists ON dbo.Staff.Name = dbo.Playlists.Staff AND dbo.Playlists.id IN (SELECT TOP 10 ID FROM Playlists WHERE Staff = Staff.Name ORDER BY Date DESC)ORDER BY dbo.Staff.Name, dbo.Playlists.[Date] DESC |
 |
|
|
|
|
|
|
|