MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Anyone looking for a way to modernize legacy data or easily migrate to a more cost-effective database without sacrificing functionality will benefit from this seminar. View the Intro to Advantage Database Server now!
  #1  
Old August 28th, 2003, 06:03 AM
24712365DUDE 24712365DUDE is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: UAE
Posts: 3 24712365DUDE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Query Help

Hello ppl,
Let me get straight to the point. I have three tables: -
1.)Movies-contains fields Movie_Id(PK) and Movie_Title
2.)Actors-contains fields Actor_Id(PK) and Actor_Name
3.)MoviesActors-contains fields Movie_Id(FK) and Actor_Id(FK)

PK->Primary Key
FK->Foreign Key

An Actor entity and a Movie entity share a many-to-many relationship between each other. Hence the need for the association table, MoviesActors.

Now I need to get a result set in the following format:-
Movie Id,Movie Title,Cast......where CAST is a comma-delimited string comprising the names of actors associated with the movie.

An example of the result set would be..

Movie Id Movie title Cast
---------- ---------------------- -----------------------------------
01 About A Boy Hugh Grant, Rachel Weisz

Hugh Grant and Rachel Weisz would be two separate entries in the Actors table.
Please Help!!

24-7-365 DUDE

Reply With Quote
  #2  
Old August 28th, 2003, 01:24 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,329 Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level)Scorpions4ever User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 4 Weeks 11 h 18 sec
Reputation Power: 662
Create a stored procedure that does the following:

1. Create a CURSOR to go through the rows of the Movies table.
2. Create a temp table (say #movie_cast)
3. For each row fetched by the cursor, insert the id and name into #movie_cast.
4. Next, populate an actors temp table containing the actors for the movie.
5. Select all the rows from the actors temp table and concatenate them. Update the corresponding row in #movie_cast with the concatenated string.
6. Skip back to step 3, if there are more rows to be fetched by the cursor.
7. Select all rows from #movie_cast (to return them),
8. Drop all temp tables.

Hope this helps.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old August 29th, 2003, 01:58 AM
24712365DUDE 24712365DUDE is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: UAE
Posts: 3 24712365DUDE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Making the query a VIEW

Thank you ! Your solution workes Scorpion. however, I tried making a VIEW with it, but the effort failed because I cant use the technique of using Cursors in defining a VIEW. How do i pull out the rabbit out of the hat now? A VIEW is what I ideally need.

Please Help!Thanking you in advance,
24-7-12-365 DUDE

Reply With Quote
  #4  
Old August 29th, 2003, 08:33 AM
WineIsGood's Avatar
WineIsGood WineIsGood is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: New York
Posts: 49 WineIsGood User rank is Private First Class (20 - 50 Reputation Level)WineIsGood User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 m 51 sec
Reputation Power: 5
Make a stored procedure instead and make the SELECT as the last statement. It'll work just like a view and will automatically trash the temp table when the connection terminates.
-Dave

Reply With Quote
  #5  
Old August 29th, 2003, 08:41 AM
24712365DUDE 24712365DUDE is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: UAE
Posts: 3 24712365DUDE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanx Dave!!But I already thought of that.However, I am glad that I thought of what you said as it would mean that i am learning.Finally, the DUDE has begun his journey!!
Yours Sincerely,
24-7-12-365 DUDE

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Query Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway