Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2006, 04:33 PM
Authorized User
 
Join Date: Jan 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default sql statement problem

hi,

I have a doubt in writing sql statement.nested subquery infact.
i want to generate a timetable.
im inserting values in timetable table which enters in the following format:

select PID - ie program ID(in dropdownlist)
enter timetable ID - TTID (in textbox)
Day P1 P2 .... P7 PID
sat bmr301(textbox) cis306 fff BBA
sun bmr301(textbox) cis306 bba

it goes into timetable table which contains the following format
ttid day p1 p2..... p7 pid
1 sat bmr301 cis306 1
1 sun bmr301 cis306 1

now i want to display a timrteable when a student is clicking the timetable link.
so im prompting the student to enter student id ie SID
enter SID (IN TEXTBIOX)

AFTER THAT IT CHECKS IN THE COURSESTAKEN TABLE WHICH ALL SUBJECTS TAKEN by tht particular student id say 010302050.

the coursestaken table is the following format

coursestaken table
cid cname sid
bmr301 mobile computing 010302050
cis306 marketing 010302050

sudent has already registered for a program ID ie PID - example say CIS(computers) or BBA (business).after the student enters thd sid,
it checks in the coursestaken table what all subjects taken by sid 010302050

ie select cid from coursestaken where sid='010302050'
it dispalys a list of courses where sid 01030250 has regstered for.
so one by one it shud take the first record cid bmr301 and checks in the timetable table to match whetehr in p1 or p2 ....till p7, bmr301 exists in the day SAT or sun for the particular PID and for the next record and so on.this is done to generate timetable according to students registerd courses.

so i want the nested sql statement for this.can anybody help mne out.
i made an sql statement .i am not sure this is the rite one.

select * from timetable where pid='1' and p1=(select cid from coursetaken where sid='010302050')

plese tel me a correct one.im not sure abt this.





 
Old February 21st, 2006, 04:19 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Try:

SELECT * FROM timetable
WHERE pid = 1' and p1 in (SELECT cid from coursetaken WHERE sid = '010302050')

I think that syntax might work but I'm not sure if its going to give you the results you require.



 
Old February 21st, 2006, 04:54 PM
Authorized User
 
Join Date: Jan 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i know the synatx might work.but how to pass each time the cid value in the coursestaken to the timetable to check. i am badly looking out for a solution for this.somebody knows pls reply me back.

 
Old February 22nd, 2006, 11:35 AM
Registered User
 
Join Date: Feb 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Pranav Joshi
Default

The query like
   SELECT * FROM timetable
   WHERE pid = 1 and p1 in (SELECT cid from coursetaken WHERE
   sid = '010302050')
will give you the entire rows that have the courseid (cid) you get from COURSETAKEN table.

But if I am not mistaken you want the column names in which the course exists.
In this case you will have to use 'sysobjects' and 'sysindexes' tables. These tables contain the details of the tables and the columns respectively.

Regards


 
Old February 22nd, 2006, 01:59 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I'm having trouble understanding exactly what you're trying to do. And I don't know the table structure.

Can you post the CREATE TABLE code for the tables involved?

Also, is this related to any sort of application? If this is related to an application it would be a lot easier. You could grab the sid and then loop through the timetable TABLE and check to see if that cid exists in the coursetaken TABLE WHERE sid = '00302050'. If you get a match then you can display that value and so on.

If the tables have primary and foreign keys all you might need to do is a simple INNER JOIN. So all that would be present in the coursetaken table is the sid and cid columns. If you already know all of this and I just don't understand the situation, please excuse me.

SELECT * from timetable INNER JOIN coursetaken ON timetable.cid = coursetaken.cid WHERE coursetaken.sid = '010302050'










Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL select statement problem shrisangeeta Classic ASP Databases 3 June 15th, 2006 10:28 AM
SQL Statement Problem Ben Horne Access 11 February 4th, 2004 11:01 PM
help needed with a SQL select statement problem wslyhbb Java Databases 1 August 14th, 2003 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.