I am taking value from textbox1 and trying to pass that value to stored procedure. but while creating stored procedure I am getting below error
Msg 102, Level 15, State 1, Procedure proc_1, Line 8
Incorrect syntax near '@name'.
in c#
string nam = " and city='" + textbox1.Text + "'";
cmd.Parameters.AddWithValue("@name", nam );
create procedure proc_1
(
@name varchar (max)
)
as
begin
select [country] from table_name where city='delhi' @name
end
please let me know, how can I insert the textbox1 value into @name variable

Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 16, 2013, 2:22 AM EST
where city='delhi' @name
it is wrong. it should be
where city=@name
Gurjeet Singh107 reputationPosted Jan 16, 2013, 2:29 AM EST
AND city='" + textbox1.Text + " into @name variable.
i know that WHERE condition must be like:
WHERE city='delhi' AND city=@name
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 16, 2013, 2:31 AM EST
WHERE city='delhi' OR city=@name
Gurjeet Singh107 reputationPosted Jan 16, 2013, 2:32 AM EST
I want to use only AND condition
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 16, 2013, 2:33 AM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 2:41 AM EST
SELECT country FROM table_name WHERE
city='Delhi' AND city=@name
Gurjeet Singh107 reputationPosted Jan 16, 2013, 2:41 AM EST
i want to use only like this :
where city='delhi' @name
Gurjeet Singh107 reputationPosted Jan 16, 2013, 2:41 AM EST
i want to use only like this :
where city='delhi' @name
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 16, 2013, 2:43 AM EST
what your really want to query?
you want to query something like
where city ='Delhi address1' like this??
where address1 is one of the value for @name.
If that is the case.,then try
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 2:43 AM EST
Gurjeet Singh107 reputationPosted Jan 16, 2013, 3:02 AM EST
if(textbox1.text="")
{
string nam =" ";
}
else
{
string name= " and city='" + textbox1.Text + "'";
}
I am using stored procedure for above purpose
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 3:08 AM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 3:13 AM EST
chellappan pandiarajan79 reputationPosted Jan 16, 2013, 3:14 AM EST
change you SQL Proc like below
create procedure proc_1
(
@name varchar (max)
)
as
begin
select [country] from table_name where city in ('delhi', @name)
end
and Change you C# coding like below
if (textbox1.text = "")
{
string name = " ";
}
else
{
string name = "'" + textbox1.Text + "'";
}
it will work.
-Pandian
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 3:23 AM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 3:32 AM EST
Sudhakar ChaudharyRank #1,379 · 1.2K reputationPosted Jan 16, 2013, 3:33 AM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 3:38 AM EST
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
but i didn't find any use of this so let us know what you are trying to do
Gurjeet Singh107 reputationPosted Jan 16, 2013, 9:25 PM EST
Gurjeet Singh107 reputationPosted Jan 16, 2013, 9:26 PM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 9:32 PM EST
Try this
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
Gurjeet Singh107 reputationPosted Jan 16, 2013, 9:33 PM EST
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
suppose
if(textbox1.text="")
{
string nam =" ";
}
else
{
string name= " and city='" + textbox1.Text + "'";
}
cmd.Parameters.AddWithValue("@name", nam );
if TextBox1.text is empty then store procedure will work like this
select Country from tblCity where CityName='delhi''
or if TextBox1.Text is having value then store procedure will work like this
select Country from tblCity where CityName='delhi' and city=' toronto'
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 9:38 PM EST
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jan 16, 2013, 9:40 PM EST
If @name is not null and @name>''
else
select [country] from table_name where city='delhi'
When passing from UI, make sure if user didnt enter then @name should be passed blank
Sudhakar ChaudharyRank #1,379 · 1.2K reputationPosted Jan 16, 2013, 9:47 PM EST
(
@name varchar (max)
)
as
begin
if(@name is null || '')
select [country] from table_name where city='delhi'
else
select [country] from table_name where city='delhi' OR city=@name
end
Gurjeet Singh107 reputationPosted Jan 16, 2013, 9:52 PM EST
But i want to pass and city=' toronto'
select Country from tblCity where CityName='delhi' and city=' toronto'
is it possible.
Or let me know any alternative in stored procedure to validate data...
Sudhakar ChaudharyRank #1,379 · 1.2K reputationPosted Jan 16, 2013, 9:55 PM EST
Dorababu MekaRank #457 · 5.5K reputationPosted Jan 16, 2013, 10:26 PM EST
sabdhi naik59 reputationPosted Jan 21, 2013, 2:17 AM EST
This is the procedure for inserting-----
Deepesh21 reputationPosted Jan 28, 2013, 2:06 PM EST
When you run the create script you will get the incorrect syntax error because it expects some kind of condition to be applied to it.
try this instead
create procedure proc_1
Clive Scott2 reputationPosted Feb 10, 2013, 11:26 AM EST
select [country] from table_name where city='delhi' @name