Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir, i have a 2 tables. first is course second is semister. In Course table there are 2 column courseID,CourseName. And in semister Table there are also 2 column. SemID, SemNumber.

Table look like:-

Course:-
CourseID --- CourseName
  201    ---  B.tech(Cse)
  202    ---  B.tech(IT)



Semister:-
SemID  ---  SemNumber
 101   ---     1
 102   ---     2

how i have select course and sem in dropdown and get CourseID and semID.. i need to make a sql to show the result like 'B.tech(IT)[2]' . it means Combine course and sem.
Posted
Updated 10-Sep-14 23:13pm
v2

If that is the totality of your table data, then you can't: there is nothing to connect one row of the Course table with any one row of the Semister table. While there may be a logical link between the two (that you understand from your knowledge of teh overall task), SQL requires an explicit link - either the SemID in the Course table or the CourseID in the Semister table.

Without that, SQL would have to guess - and it won't do that.
 
Share this answer
 
Hi,

Check this....
SQL
SELECT COURSENAME + '[' + CONVERT(VARCHAR,SEMNUMBER) + ']' 
FROM COURSE, SEMESTER 
WHERE 
COURSEID=@CidFromDDL
AND SEMID=@SemIdFromDDL



Quote:
producing m * n rows, and then select the one that happens to join the selected course to the selected semester


or in Other way round in order to minimize cross join impact.....

SQL
Create proc pr_result
(
@pa_courseId int,
@pa_semId int
)
As
begin

create table #tmp_course
(
cid int,
cname varchar(200)
)

insert into #tmp_course(cid,cname )
select courseid,coursename from course where courseid=@pa_courseId

create table #tmp_semester
(
sid int,
sno int
)

insert into #tmp_semester(sid ,sno )
select courseid,coursename from course where courseid=@pa_semId 


SELECT cname+ '[' + CONVERT(VARCHAR,sno ) + ']' 
FROM #tmp_course, #tmp_semester

end


Hope this will help you.

Cheers
 
Share this answer
 
v2
Comments
kbrandwijk 11-Sep-14 5:30am    
So you're actually suggesting performing a cross join on the two tables first, producing m * n rows, and then select the one that happens to join the selected course to the selected semester. That's just terrible.
Magic Wonder 11-Sep-14 5:45am    
I have given solution on the basis of input given. I know m*n is terrible but don't ignore where clause.

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