tags:

views:

66

answers:

2

Whats wrong wrong with this query please correct me on this syntactcally it's going wrong please correct me on this how to concatenate this

string cmd = "SELECT * 
                FROM [tbl_students] 
               WHERE course_id=@courseId 
                 AND branch_id IN ("+branchId+") 
                 AND (@passoutYear is null or passout_year>=@passoutYear) 
                 AND (@currentBacklog is null or current_backlog<=@currentBacklog)
                 AND gender=@sex 
                 AND (@eGap is null or gapin_education<=@egap)
                 AND (@firstYrPercent is null or first_yea_percent>=@firstYrPercent
                 AND (@secondYrpercent is null or second_year_percent>=@secondYrPercent)
                 AND (@thirdYrPercent is null or third_year_percent>=@thirdYrPercent)
                 AND (@finalYrpercent is null or final_year_percent>=@finalYrpercent)
                 AND (@currentDegreePercentage is null current_degree_percent>=@currentDegreePercentage)
                 AND (@highSchoolPercentage is null high_school_percentage>=@highSchoolPercentage)
                 AND (@higherSchoolPercentage is null higher_school_percentage>=@higherSchoolPercentage)
                 AND (@graduationPercent is null graduation_percent>=@graduationPercentage)
                 AND (@diplomaPercentage is null diploma_percentage>=@diplomaPercenage)
                 AND (@noOfAtkt is null or no_of_atkt<=@noOfAtkt)
                 AND (@date is null or date_of_birth>=@date)";
A: 

I think I got it.

Are you getting an error while trying to COMPILE the code, as opposed to running the SQL query?

I'm going to bet that you're using C#, and that you should do multi-line strings with an @-symbol before the "-symbol. Such as this:

string blabla = @"
     hello
     there
";

Then of course you need it also when you open up the string after branchId

Helgi Hrafn Gunnarsson
you are apsolutely correct my dear friend u made it cleari did the same explained by you above and it work.But can you elaborate a bit on this y i have to do this, i mean i want you to tell me regarding concatenation on it
NoviceToDotNet
I am honestly not sure why they designed it that way. I think it makes sense though, that a normal string never carry text that is natural to the text editor. It seems like a good principle, for special characters to be denoted by a sequence of characters, like \n. Then I suppose they offered this as a short hand of some sort.
Helgi Hrafn Gunnarsson
on running y is it giving error Must declare the scalar variable "@courseId"
NoviceToDotNet
+2  A: 

First, if branchId is a string, then you'll need these single quotes:

branch_id IN('"+branchId+"') AND 

though I don't understand why it's not parameterized like so:

branch_id = @branchId AND 

Second, you may have misspelled the word 'year' in this field name:

AND  (@firstYrPercent is null or first_year_percent>=@firstYrPercent

Finally, you're missing a few OR's here:

AND  (@currentDegreePercentage is null OR current_degree_percent>=@currentDegreePercentage)
AND  (@highSchoolPercentage is null OR high_school_percentage>=@highSchoolPercentage)
AND  (@higherSchoolPercentage is null OR higher_school_percentage>=@higherSchoolPercentage)
AND  (@graduationPercent is null OR graduation_percent>=@graduationPercentage)
AND  (@diplomaPercentage is null OR diploma_percentage>=@diplomaPercenage)
branchId an object od string builder having integer values that's ycorrect me if i am wrong somewhere
NoviceToDotNet
What's the value of `branchId`'s string representation?
on running y is it giving error Must declare the scalar variable "@courseId"
NoviceToDotNet