Re: Insert with multiple subqueries - possible?
From: David Gugick (davidg-nospam_at_imceda.com)
Date: 01/15/05
- Next message: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Previous message: Tausif: "Re: Get the Identity value"
- In reply to: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Next in thread: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Reply: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 15 Jan 2005 00:16:24 -0500
John Dinning wrote:
> Firstly, the purpose of my question originally was simply to find out
> if it was possible to use multiple subqueries in an insert statement
> (particularly in MSAccess), and if so, what the syntax is.
> I can do what I need to do with 3 queries, but not knowing how to do
> it in one is bugging me.
>
> Secondly, I have now become concerned that either I am going about
> this all the wrong way (quite possible), or I have failed to explain
> myself properly. Let me try again.
>
>
> I have a Course table (lets say with 3 fields):
> Course_ID (AutoNumber - unique)
> Course_Code (Text - also Unique)
> Course_Description (Text)
>
> Sample Data:
> 1 'BIO' 'Biology'
> 2 'MAT' 'Mathematics'
> 3 'GEO' 'Geology'
>
>
> I have a Student Table (lets say it also has 3 fields):
> Stu_ID (AutoNumber - unique)
> Stu_Number (Text - also Unique)
> Stu_Name(Text)
>
> Sample Data:
> 1 '1234' 'Fred'
> 2 '2323' 'Harry'
> 3 '765' 'Mary'
> 4 '99999' 'Jane'
>
>
> The third table, CourseStudent, has three fields:
> SC_ID (AutoNumber - unique - not really necessary, I have been
> advised that every table should have one but in this instance I see
> no reason for it) SC_CourseID (the Course_ID from the Course table)
> SC_StuID (the Stu_ID from the Student table)
>
Maybe this will help. The example has some restrictions on it (see
constraints on all tables). If the conditions in the example, do not
meet your conditions, you may have to restrict the rows in the final
insert by comaring to the existing tables or using DISTINCT.
Drop Table Student
Create Table Student (Stu_ID int identity PRIMARY KEY CLUSTERED,
Stu_Number int not null)
Create unique index Student_Number_IDX on Student (Stu_Number)
Insert Into Student Values (1000)
Insert Into Student Values (2000)
Insert Into Student Values (3000)
Drop Table Course
Create Table Course (Course_ID int identity PRIMARY KEY CLUSTERED,
Course_Number int not null)
Create unique index Course_Number_IDX on Course (Course_Number)
Insert Into Course Values (10000)
Insert Into Course Values (20000)
Insert Into Course Values (30000)
Drop Table Student_Course
Create Table Student_Course (Stu_ID int NOT NULL REFERENCES Student,
Course_ID int NOT NULL REFERENCES Course)
Insert Into Student_Course Values (1,1)
Insert Into Student_Course Values (2,1)
Insert Into Student_Course Values (3,2)
Insert Into Student_Course Values (3,3)
Drop Table ImportStudents
Create Table ImportStudents (Stu_Number INT NOT NULL, Course_Number INT
NOT NULL PRIMARY KEY CLUSTERED (Stu_Number, Course_Number))
Insert Into ImportStudents Values (1000,20000)
Insert Into ImportStudents Values (3000,10000)
Select * from Student_Course
Stu_ID Course_ID
----------- -----------
1 1
2 1
3 2
3 3
Select * from ImportStudents
Insert Into Student_Course (
Stu_ID, Course_ID )
Select a.Stu_ID,
b.Course_ID
>From ImportStudents c inner join Course b
on c.Course_Number = b.Course_Number
inner join Student a
on c.Stu_Number = a.Stu_Number
Select * from Student_Course
Stu_ID Course_ID
----------- -----------
1 1
2 1
3 2
3 3
1 2
3 1
-- David Gugick Imceda Software www.imceda.com
- Next message: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Previous message: Tausif: "Re: Get the Identity value"
- In reply to: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Next in thread: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Reply: John Dinning: "Re: Insert with multiple subqueries - possible?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|