Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to write a script which populates a database.
I have been told I need to use temporary tables and cursors to write the script.
The instructions are to use a temporary table to store the "inputs" to your script, then use a cursor to iterate over those inputs and to generate records in the database if they don't exist.
I understand how to use temporary tables, however I don't have a firm understanding of cursors.
I need this script to create the schools, qualifications and courses in the database, for any given year if they don't exist. The scrip should also index the courses within the correct qualification and also the qualification should be indexed within the correct school.

Can someone please help me here?

SQL
SET NOCOUNT ON;

/***** START OF INPUTS *****/

DECLARE @year int;
SET @year='2013';

--list of schools and qualifications...
create table #mock(facultyTitle varchar(64), qualificationTitle varchar(64),  Qoccurs varchar(64), courseTitle varchar(128), courseCode varchar (64), Coccur varchar(64));

--repeat the below for each school, qualification and course
INSERT INTO #mock(facultyTitle, qualificationTitle, Qoccurs, courseTitle, courseCode, Coccur) VALUES ('Art and Design', 'Design, 2013', '0= whole year', 'Creative Process', 'CP', '0= whole year');
INSERT INTO #mock(facultyTitle, qualificationTitle, Qoccurs, courseTitle, courseCode, Coccur) VALUES ('Art and Design', 'Design, 2013', '0= whole year', 'Core Theory Art and Design', 'CTAD', '1= first semester');
INSERT INTO #mock(facultyTitle, qualificationTitle, Qoccurs, courseTitle, courseCode, Coccur) VALUES ('Art and Design', 'Design, 2013', '0= whole year', 'History, Culture, Context', 'HCC', '1= first semester');
INSERT INTO #mock(facultyTitle, qualificationTitle, Qoccurs, courseTitle, courseCode, Coccur) VALUES ('Art and Design', 'Design, 2013', '0= whole year', 'Digital Technology and Design', 'DTD', '2= second semester');
--Note: not all insert statements are included

/***** END OF INPUTS *****/

--check that the schools, qualifications and courses we have are valid...
SELECT #mock.facultyTitle, #mock.qualificationTitle, #mock.Qoccurs, #mock.courseTitle, #mock.courseCode, #mock.Coccur, school.schoolID, qualification.qualificationID, course.courseID
INTO #mockdata2
FROM #mock
LEFT JOIN school ON #mock.facultyTitle=school.name COLLATE Latin1_General_CI_AS
LEFT JOIN qualification ON #mock.qualificationTitle=qualification.title COLLATE Latin1_General_CI_AS
LEFT JOIN course ON #mock.courseTitle=course.title COLLATE Latin1_General_CI_AS AND #mock.courseCode=course.code COLLATE Latin1_General_CI_AS


--check that all the schools can be found, message should come as school shouldn't exit 
IF EXISTS(SELECT 1 FROM #mockdata2 WHERE schoolID IS NULL)
BEGIN
	print 'At least one school can not be found';
	select DISTINCT facultyTitle from #mockdata2 WHERE schoolID IS NULL
END

--check that all the qualifications can be found, message should come as qualification shouldn't exit 
IF EXISTS(SELECT 1 FROM #mockdata2 WHERE qualificationID IS NULL)
BEGIN
	print 'At least one qualification can not be found';
	select DISTINCT qualificationTitle from #mockdata2 WHERE qualificationID IS NULL
END

--check that all the courses can be found, message should come as course shouldn't exit 
IF EXISTS(SELECT 1 FROM #mockdata2 WHERE courseID IS NULL)
BEGIN
	print 'At least one course can not be found';
	select DISTINCT courseTitle, courseCode from #mockdata2 WHERE courseID IS NULL
END


DECLARE @errorCount int
DECLARE @schoolID int
DECLARE @qualificationID int
DECLARE @courseID int


SET @errorCount=0

--Check that the schools we have been given exist in database
DECLARE schoolCursor CURSOR FOR 
	SELECT DISTINCT schoolID, qualificationID, courseID FROM #mockdata2;

	OPEN schoolCursor;
	
	FETCH NEXT FROM schoolCursor INTO @schoolID, @qualificationID, @courseID; 
	WHILE @@FETCH_STATUS = 0
	BEGIN;
Posted
Comments
RedDk 15-Aug-13 16:10pm    
Overlook [school], [qualification], and [course] ...
Aamir Mitha 18-Aug-13 6:33am    
Sorry I am a little new to this I don't understand what you mean?
Aamir Mitha 21-Aug-13 17:47pm    
Hey mate could you please explain what you mean ?
RedDk 21-Aug-13 18:46pm    
All three of these things are outside the scope of the query. The error is Msg 4104, "multi-part identifier X could not be found". For starters, ditch those fields. What do you see?

This:

facultyTitle qualificationTitle Qoccurs courseTitle courseCode Coccur
Art and Design Design, 2013 0= whole year Creative Process CP 0= whole year
Art and Design Design, 2013 0= whole year Core Theory Art and Design CTAD 1= first semester
Art and Design Design, 2013 0= whole year History, Culture, Context HCC 1= first semester
Art and Design Design, 2013 0= whole year Digital Technology and Design DTD 2= second semester

So you've done something right.
Aamir Mitha 22-Aug-13 1:45am    
sorry I don't understand you.. what do i ditch ? Please be clear, I am new to this.

1 solution

I have small template I made a while ago for whenever I need a cursor.

You have to adjust it to Your needs:

SQL
DECLARE @VARA int
DECLARE @VARB varchar(2)
DECLARE @VARC datetime

DECLARE MyPointer CURSOR FOR SELECT A, B, C FROM MyTable
OPEN MyPointer
-- Find first record
FETCH NEXT FROM MyPointer INTO @VARA, @VARB, @VARC
-- Check @@FETCH_STATUS for more records
WHILE @@FETCH_STATUS = 0
    BEGIN
        -- Do something here with @VARA, @VARB, @VARC

        FETCH NEXT FROM MyPointer INTO @VARA, @VARB, @VARC
    END
CLOSE MyPointer
DEALLOCATE MyPointer


Don't forget to mark it as answered if it's the case.
 
Share this answer
 
v2

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