Prev: Pass changing values to parameter to Crystal Report scroll thr
Next: Storing objects in Session variables
From: JB on 1 Apr 2010 17:43 Hello Community I am passing parameters SQL Server from an C# ASP.NET app. When creating the query I have need to use an “or” operator after the Where clause when comparing values. Does anyone know the syntax for this type of thing? Below is the code: public DataSet getCustomer(string Addr, int PhoneNo) { string strSQL = " select distinct a.firstname + ' ' + a.lastname as Name"; strSQL += " from table1 a Join Table2 b "; strSQL += " on a.id = b.id "; strSQL += " WHERE a.Addr = " + Addr; strSQL += " AND (b.PhoneNo = + PhoneNo OR b.PhoneNo <> PhoneNo); strSQL += " order by a.lastname, a.firstname "; } If I only wanted to check whether the PhoneNo was equal the following code works: strSQL += " AND c.PhoneNo = " + PhoneNo; But I want to get a result returned whether the PhoneNo is equal or the PhoneNo is not That means the question is: When passing variable and comparing values what is the syntax for the “or” operator within the Where clause and are the parenthesis necessary? Thanks Jeff -- JB
From: Andy O'Neill on 2 Apr 2010 06:11 "JB" <JB(a)discussions.microsoft.com> wrote in message news:EA565F5E-F04F-4BD6-90A5-711E57460C12(a)microsoft.com... > Hello Community > > I am passing parameters SQL Server from an C# ASP.NET app. > When > creating the query I have need to use an “or” operator after the Where > clause > when comparing values. Does anyone know the syntax for this type of > thing? > > Below is the code: > > public DataSet getCustomer(string Addr, int PhoneNo) > { > > string strSQL = " select distinct a.firstname + ' ' + > a.lastname as Name"; > > strSQL += " from table1 a Join Table2 b "; > > strSQL += " on a.id = b.id "; > > strSQL += " WHERE a.Addr = " + Addr; > > strSQL += " AND (b.PhoneNo = + PhoneNo OR b.PhoneNo <> > PhoneNo); > > > > strSQL += " order by a.lastname, a.firstname "; > > } > > > If I only wanted to check whether the PhoneNo was equal the following code > works: > > strSQL += " AND c.PhoneNo = " + PhoneNo; > > But I want to get a result returned whether the PhoneNo is equal or the > PhoneNo is not > That means the question is: > > When passing variable and comparing values what is the syntax for the “or” > operator within the Where clause and are the parenthesis necessary? > > Thanks > Jeff > > -- > JB You use or and you might need brackets to group the two alternative conditions.
From: Harlan Messinger on 2 Apr 2010 07:25
Andy O'Neill wrote: > > "JB" <JB(a)discussions.microsoft.com> wrote in message > news:EA565F5E-F04F-4BD6-90A5-711E57460C12(a)microsoft.com... >> Hello Community >> >> I am passing parameters SQL Server from an C# ASP.NET app. >> When >> creating the query I have need to use an “or” operator after the Where >> clause >> when comparing values. Does anyone know the syntax for this type of >> thing? >> >> Below is the code: >> >> public DataSet getCustomer(string Addr, int PhoneNo) >> { >> >> string strSQL = " select distinct a.firstname + ' ' + >> a.lastname as Name"; >> >> strSQL += " from table1 a Join Table2 b "; >> >> strSQL += " on a.id = b.id "; >> >> strSQL += " WHERE a.Addr = " + Addr; >> >> strSQL += " AND (b.PhoneNo = + PhoneNo OR b.PhoneNo <> >> PhoneNo); >> >> >> >> strSQL += " order by a.lastname, a.firstname "; >> >> } >> >> >> If I only wanted to check whether the PhoneNo was equal the following >> code >> works: >> >> strSQL += " AND c.PhoneNo = " + PhoneNo; >> >> But I want to get a result returned whether the PhoneNo is equal or the >> PhoneNo is not >> That means the question is: >> >> When passing variable and comparing values what is the syntax for the >> “or” >> operator within the Where clause and are the parenthesis necessary? >> >> Thanks >> Jeff >> >> -- >> JB > > You use or and you might need brackets to group the two alternative > conditions. Please don't confuse my fellow American. To us, "brackets" = "square brackets". We group with "parentheses". |