| Author |
Topic |
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-06-06 : 04:10:38
|
| Hello, I want to pass a field name as a variable so as to take it's count. My Stored Proc isCreate procedure sp_subjectExist@sub varchar(10),@sgCode varchar(3)asdeclare @sCnt intbegin select Count(@Sub) from marks where regno in(select sregno from student where subjectgroupid=@sgCode) select @subendsp_subjectExist 'sub01', 'A'it's count is not working.Please helpRegardsCeema |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-06-06 : 04:15:45
|
| counting of records?use select count(*) from tablenamewhere subjectgroupid=@sgcodeor is this a join?select count(*) from table1 t1join table2 t2 on t1.regno=t2.regnowhere t2.subjectgroupid=@sgcodewhat's sub for?--------------------keeping it simple... |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-06-06 : 04:16:03
|
| Not sure what you want to do.That's not setting @sub soselect @sCnt = Count(*) from marks where regno in(select sregno from student where subjectgroupid=@sgCode) select @sCntprobablt @sCnt should be an output variable.Then the question is what do you want to do with @submaybeselect @sCnt = Count(*) from marks where regno in(select sregno from student where subjectgroupid=@sgCode) and subject = @sub==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-06 : 04:25:39
|
ceema,can you post your marks table structure ? if you need to do sp_subjectExist 'sub01', 'A'sp_subjectExist 'sub02', 'A'etc quote: I want to pass a field name as a variable so as to take it's count
It seems to me that it is not normalized. Also avoid naming your stored procedure begin with sp_. It will cause SQL Server to search the master db for the SP before the current DB which is not efficient. KH |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-06-06 : 04:34:07
|
| Actually, I am trying to pass the field name by @Sub Variable. The last line is likeExec sp_subjectExist 'sub01', 'A'My field names may vary and Subject groupID too can vary. So, what I want isI have to get sregno from student table according to SubjectGroupID.Then, I have to get the count of any of the fields(here ,I am passing it from stored procedure call as 'sub01'(my fields name values are like sub01,sub02,sub03 etc)) from Marks Table whoes RegNo is in the first selection set. RegardsCeema |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-06-06 : 04:43:11
|
| you are counting the rows as per record, unless you want to disregard null values for the field you specifiedin this case, you will need dsql as Maddy suggested...--------------------keeping it simple... |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-06-06 : 04:53:45
|
| Hello, Thank you every one for the reply. I will try Dynamic Query. And 'khtan', My table structure isRegNo Sub01 Sub02 Sub03 Sub04 ... Sub16I know it's not normilized, but I am newly joined in this company and I can't change the existing system. Thank you for your advice in naming the stored proc.RegardsCeema |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-06 : 05:39:48
|
You can create a view that is normalized and use the view in the stored procedure. Then you can avoid using Dynamic SQL.create view v_marksasselect RegNo, 'Sub01' as SubCode, Sub01 as Subfrom marksunion allselect RegNo, 'Sub02' as SubCode, Sub02 as Subfrom marksunion allselect RegNo, 'Sub03' as SubCode, Sub03 as Subfrom marks. . . Create procedure usp_subjectExist@sub varchar(10),@sgCode varchar(3)asdeclare @sCnt intbegin select Count(*) from v_marks where SubCode = @sub and RegNo in(select sregno from student where subjectgroupid=@sgCode)end KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-06 : 07:45:04
|
Good idea khtan. ceema - I'd definitely go for that one... Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
ceema
Yak Posting Veteran
80 Posts |
Posted - 2006-06-07 : 07:07:40
|
| My requirement, I think no body is getting.Actually, I want to pass the field name as the variable, not a value to match to the variable.I have 6 fields in my tableRegNo, Sub01,Sub02,Sub03,Sub04,Sub05I want to make a query in such a way that, I want to pass any of these field names(not a value to the field, it's exactly the field name only) ,So, in my first queryCreate procedure sp_subjectExist@sub varchar(10),@sgCode varchar(3)asdeclare @sCnt intbegin select Count(@Sub) from marks where regno in(select sregno from student where subjectgroupid=@sgCode) select @subendExec sp_subjectExist 'sub01', 'A' .....> Sub01 is not a value, but it's a field name.For exampleIn Student table 3 values are thereSRegNo SgCode111 A222 B333 AMarksRegNo Sub01 Sub02 Sub03 Sub04 Sub05111 2 3 4 5 6222 12 13 Null 15 16 .....> Here Null for Sub03333 22 23 24 25 26If I will passExec sp_subjectExist 'sub01', 'A' it should give me the value fields without null count as 3Exec sp_subjectExist 'sub03', 'A' it should give me the value fields without null count as 2Hope that now I explained it in detailPlease helpCeema |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-07 : 07:55:54
|
Try thisCreate procedure sp_subjectExist@sub varchar(10),@sgCode varchar(3)asdeclare @sCnt intbegin Exec('select Count('+@Sub+') from marks where regno in (select sregno from student where subjectgroupid='''+@sgCode+''')' endExec sp_subjectExist 'sub01', 'A' Also you need to read about Normalisationhttp://www.datamodel.org/NormalizationRules.htmlMadhivananFailing to plan is Planning to fail |
 |
|
|
|