473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

T-SQL, Cursors, Best Practices of pulling from multiple tables

76
Good afternoon, here I am again with a new bugger of a problem. I'm working on an update for an existing program that uses classic ASP with a good SQL Server backend. The application server is kinda dink and my task lead wants most of the data to be handled on the database end of things. My problem? While I can write the queries of doom I'm kinda new to T-SQL.

Here's the issue. I've got projects, tasks, departments, point of contacts, and managers. I'm working on a report that for each project selected by (we'll say the site manager) to display for every project selected a list of departments associated with that project and their task status, the point of contact for that department, their 2nd tier supervisory department, the 2nd tier point of contact, and the projects due date/information, and overall status.

What I need is for each project I pass in to loop through the departments listed associated with that project and based on the COC pull their 2nd tier (already got a query for this from another thread), loop through the tasks associated with each project/department as they are related, then from a completely unrelated source pull in the POC's as associated with the department.

From what I've gathered talking to my collegues the best course of action is most likely setting up a temporary table within a stored procedure and using a cursor. My question is, can you nest cursors and how so?

-------
Basically, I have severe doubt anyone here could help me. Any book recommendations that talks about cursors and nesting? Thanks.
Jan 30 '07 #1
3 11043
iburyak
1,017 Expert 512MB
See example from T-SQL help.

[PHP] Use nested cursors to produce report output
This example shows how cursors can be nested to produce complex reports. The inner cursor is declared for each author.

SET NOCOUNT ON

DECLARE @au_id varchar(11), @au_fname varchar(20), @au_lname varchar(40),
@message varchar(80), @title varchar(80)

PRINT "-------- Utah Authors report --------"

DECLARE authors_cursor CURSOR FOR
SELECT au_id, au_fname, au_lname
FROM authors
WHERE state = "UT"
ORDER BY au_id

OPEN authors_cursor

FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT " "
SELECT @message = "----- Books by Author: " +
@au_fname + " " + @au_lname

PRINT @message

-- Declare an inner cursor based
-- on au_id from the outer cursor.

DECLARE titles_cursor CURSOR FOR
SELECT t.title
FROM titleauthor ta, titles t
WHERE ta.title_id = t.title_id AND
ta.au_id = @au_id -- Variable value from the outer cursor

OPEN titles_cursor
FETCH NEXT FROM titles_cursor INTO @title

IF @@FETCH_STATUS <> 0
PRINT " <<No Books>>"

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @message = " " + @title
PRINT @message
FETCH NEXT FROM titles_cursor INTO @title

END

CLOSE titles_cursor
DEALLOCATE titles_cursor

-- Get the next author.
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO[/PHP]
Jan 30 '07 #2
Arielle
76
Thank you very much.

I've managed to make the stored procedure neccessary. Tre fabulous! Now that my brain has been sufficiently picked for a few days...
Jan 31 '07 #3
iburyak
1,017 Expert 512MB
I remember you had difficult case where data has comma separated values.

I hate cursors and would not recommend them to anyone but in your case it is easier for you I guess.... :)

Good job.

Good luck.
Jan 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
1
by: louis nguyen | last post by:
Hi All, My question is what are the best practices for administering large DBs. (My coworker is the DB administrator. I'm more of the developer. But slowly being sucked in.) My main concern...
2
by: byrocat | last post by:
I'm chasing after a documetn that was available on one of the Microsoft websites that was titled somethign like "MS SQL Server Best Practices" and detailed a nyumber of best practices about...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
4
by: Collin Peters | last post by:
I have searched the Internet... but haven't found much relating to this. I am wondering on what the best practices are for migrating a developmemnt database to a release database. Here is the...
4
by: dreaken667 | last post by:
I have a MySQL database containing 16 tables of data. Each table has a different number of columns and there are few common field names accross tables. I do have one master table with which I connect...
1
desklamp
by: desklamp | last post by:
Access 2003 on Win2K The tables involved: an IP address table, tblIP, where basic IP address information is stored: IP, WHOIS, reverse DNS name, etc. an event IP table, tblIPEvent, where...
2
by: mvsmsh | last post by:
Hi , all We have hundreds of tables in on tablespace , but I'm planning to change as one table per one tablespace. . I wonder how other shops does ? Are there any special reasons in UDB...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.