Hello,
I am trying to send Where clause as a parameter. Here is the detail:
Select statment:
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT * " +
" FROM myTable " +
" WHERE @WClause ";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.sqlSelectCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@WClause",
System.Data.SqlDbType.NVarChar, 255, "WClause"));
In the code, I do:
string wClause = "(MyStr LIKE 'zyx%')";
sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause;
sqlDataAdapter1.Fill(dataSet11);
I got this error: Line 1: Incorrect syntax near '@WClause'.
I tried this in the query analyzer, it works fine:
select *
from myTable
where (MyStr LIKE 'zyx%')
What is problem? 5 1101
You can't do that. Parameters are meant to send literal values (strings,
integers, dates, etc). Not executable statements (or pieces of them).
You can make the query: "Select * From MyTable WHERE MyCol like @val"
Then add a variable called "@val", and set its value to whatever you need it
to be.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com... Hello,
I am trying to send Where clause as a parameter. Here is the detail:
Select statment: // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT * " + " FROM myTable " + " WHERE @WClause "; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@WClause", System.Data.SqlDbType.NVarChar, 255, "WClause"));
In the code, I do: string wClause = "(MyStr LIKE 'zyx%')"; sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause; sqlDataAdapter1.Fill(dataSet11);
I got this error: Line 1: Incorrect syntax near '@WClause'.
I tried this in the query analyzer, it works fine: select * from myTable where (MyStr LIKE 'zyx%')
What is problem?
The problem is with that: I need to add or remove constraints based on the
user input. For string fields I use %. However, for example "Where (myId =
@ID) " if user did not give @ID, that should not have any impact on the query
result. How can I do this?
"Marina" wrote: You can't do that. Parameters are meant to send literal values (strings, integers, dates, etc). Not executable statements (or pieces of them).
You can make the query: "Select * From MyTable WHERE MyCol like @val"
Then add a variable called "@val", and set its value to whatever you need it to be.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com... Hello,
I am trying to send Where clause as a parameter. Here is the detail:
Select statment: // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT * " + " FROM myTable " + " WHERE @WClause "; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@WClause", System.Data.SqlDbType.NVarChar, 255, "WClause"));
In the code, I do: string wClause = "(MyStr LIKE 'zyx%')"; sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause; sqlDataAdapter1.Fill(dataSet11);
I got this error: Line 1: Incorrect syntax near '@WClause'.
I tried this in the query analyzer, it works fine: select * from myTable where (MyStr LIKE 'zyx%')
What is problem?
You can adjust your query based on input. Just leave that condition out of
the whereclause.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com... The problem is with that: I need to add or remove constraints based on the user input. For string fields I use %. However, for example "Where (myId = @ID) " if user did not give @ID, that should not have any impact on the query result. How can I do this?
"Marina" wrote:
You can't do that. Parameters are meant to send literal values (strings, integers, dates, etc). Not executable statements (or pieces of them).
You can make the query: "Select * From MyTable WHERE MyCol like @val"
Then add a variable called "@val", and set its value to whatever you need it to be.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com... > Hello, > > I am trying to send Where clause as a parameter. Here is the detail: > > Select statment: > // sqlSelectCommand1 > // > this.sqlSelectCommand1.CommandText = "SELECT * " + > " FROM myTable " + > " WHERE @WClause "; > this.sqlSelectCommand1.Connection = this.sqlConnection1; > this.sqlSelectCommand1.Parameters.Add(new > System.Data.SqlClient.SqlParameter("@WClause", > System.Data.SqlDbType.NVarChar, 255, "WClause")); > > > In the code, I do: > string wClause = "(MyStr LIKE 'zyx%')"; > sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause; > sqlDataAdapter1.Fill(dataSet11); > > I got this error: Line 1: Incorrect syntax near '@WClause'. > > I tried this in the query analyzer, it works fine: > select * > from myTable > where (MyStr LIKE 'zyx%') > > What is problem? >
Thanks for the reply. I used this format it works Where (myId = @ID OR @ID IS
NULL)
"Marina" wrote: You can adjust your query based on input. Just leave that condition out of the whereclause.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message news:8D**********************************@microsof t.com... The problem is with that: I need to add or remove constraints based on the user input. For string fields I use %. However, for example "Where (myId = @ID) " if user did not give @ID, that should not have any impact on the query result. How can I do this?
"Marina" wrote:
You can't do that. Parameters are meant to send literal values (strings, integers, dates, etc). Not executable statements (or pieces of them).
You can make the query: "Select * From MyTable WHERE MyCol like @val"
Then add a variable called "@val", and set its value to whatever you need it to be.
"JIM.H." <JI**@discussions.microsoft.com> wrote in message news:68**********************************@microsof t.com... > Hello, > > I am trying to send Where clause as a parameter. Here is the detail: > > Select statment: > // sqlSelectCommand1 > // > this.sqlSelectCommand1.CommandText = "SELECT * " + > " FROM myTable " + > " WHERE @WClause "; > this.sqlSelectCommand1.Connection = this.sqlConnection1; > this.sqlSelectCommand1.Parameters.Add(new > System.Data.SqlClient.SqlParameter("@WClause", > System.Data.SqlDbType.NVarChar, 255, "WClause")); > > > In the code, I do: > string wClause = "(MyStr LIKE 'zyx%')"; > sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause; > sqlDataAdapter1.Fill(dataSet11); > > I got this error: Line 1: Incorrect syntax near '@WClause'. > > I tried this in the query analyzer, it works fine: > select * > from myTable > where (MyStr LIKE 'zyx%') > > What is problem? >
Hi Jim:
You just can't use a parameter for the entire WHERE clause. What you
would be trying to do in query analyzer would essentially be:
DECLARE @clause varchar(255)
SET @clause = '(MyStr LIKE ''zyx%'')'
SELECT *
FROM dbo.Categories
WHERE @clause
which yields the same error:
Line 6: Incorrect syntax near '@clause'.
--
Scott http://www.OdeToCode.com/blogs/scott/
On Fri, 3 Jun 2005 12:35:02 -0700, JIM.H.
<JI**@discussions.microsoft.com> wrote: Hello,
I am trying to send Where clause as a parameter. Here is the detail:
Select statment: // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT * " + " FROM myTable " + " WHERE @WClause "; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@WClause", System.Data.SqlDbType.NVarChar, 255, "WClause"));
In the code, I do: string wClause = "(MyStr LIKE 'zyx%')"; sqlDataAdapter1.SelectCommand.Parameters["@WClause"].Value = wClause; sqlDataAdapter1.Fill(dataSet11);
I got this error: Line 1: Incorrect syntax near '@WClause'.
I tried this in the query analyzer, it works fine: select * from myTable where (MyStr LIKE 'zyx%')
What is problem? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DavidS |
last post by:
Have Visual Studio.NET installed on MS 2000 Professional OS laptop. No issue
ever with web development and SQL connections.
Purchased new laptop with XP Professional SP2!!!!!!!! & Visual...
|
by: Rob R. Ainscough |
last post by:
Sorry to hear about the job cuts and Oracle now out sourcing to India.
Rob.
|
by: Ramprasad |
last post by:
Life is Different here, sky has no limit but you must grip it with this
website, your Mirror of mind are present there. Whatever you want
related to your everyday life, it can present or give...
|
by: leighahh |
last post by:
Hi everyone i was wondering if there was a way to turn this code that is css into HTML
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
|
by: dibblm |
last post by:
I'll start this hopefully simple and add code where needed or requested.
Im using a combobox that bound to a DataSet. The Dataset retreives it's values from SQL.
I can retreive the values...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |