NeGueSa
asked on
Running SQL Store Procedure with Parameters
I have a SQL Store Procedure called from Visual Studio 2005 vb code behind.
In the store procedure I want to delete a serie of record which meet the following condition:
DELETE FROM [DATABASE].[dbo].[TABLE]
WHERE [Sequence] = @sequence AND [Product] IN(@selctedlist)
Product field is an Int field and @selctedlist is a string character. i.e '1, 2, 3, 4, 5'
when I run the store procedure is returning me an error.
If I run all from vb code behind I will have no error:
i.e = Command = "DELETE FROM [DATABASE].[dbo].[TABLE]
WHERE [Sequence] = " & sequence & " AND [Product] IN(" & selctedlist & ")"
But I really want to run it from the Store Procedure. Can anybody help ?
In the store procedure I want to delete a serie of record which meet the following condition:
DELETE FROM [DATABASE].[dbo].[TABLE]
WHERE [Sequence] = @sequence AND [Product] IN(@selctedlist)
Product field is an Int field and @selctedlist is a string character. i.e '1, 2, 3, 4, 5'
when I run the store procedure is returning me an error.
If I run all from vb code behind I will have no error:
i.e = Command = "DELETE FROM [DATABASE].[dbo].[TABLE]
WHERE [Sequence] = " & sequence & " AND [Product] IN(" & selctedlist & ")"
But I really want to run it from the Store Procedure. Can anybody help ?
product is a string field? if so pass the stored procedure '1','2','3','4','5'
ASKER
No Produc is an Integer Field and @selctedlist is a parameter string of this type '1, 2, 3, 4, 5'
post the exception you receive
Well if you quoted the whole string and the field is a number it won't work. take out the quotes. if the column is a string then it would not give an error, but you would have to quote each item individually to get them all recognized in the SQL statement.
IN('1, 2, 3, 4, 5') will not work if Product is a number
IN(1, 2, 3, 4, 5) will work if Product is a number
IN('1, 2, 3, 4, 5') will not work if Product is a number
IN(1, 2, 3, 4, 5) will work if Product is a number
ASKER
Well this value comes from a hiden label and is stored as :
"1, 2, 3, 4, 5"
"1, 2, 3, 4, 5"
if the quotes are not passed to the stored proc, then you must have a problem with the way you call the stored proc, please post your code where you call the stored proc.
ASKER
Dim SqlCon As New System.Data.SqlClient.SqlC onnection( )
Dim SqlCom As New System.Data.SqlClient.SqlC ommand()
SqlCon.ConnectionString = "data source=SERVERNAME;initial catalog=DATABASE;password= ttt;persis t security in" & _
"fo=True;user id=uuuu;workstation id=MACHINE;packet size=4096"
SqlCon.Open()
SqlCom.CommandText = "dbo.[DELETEPROD]"
SqlCom.CommandType = System.Data.CommandType.St oredProced ure
SqlCom.Connection = SqlCon
SqlCom.Parameters.Add(New System.Data.SqlClient.SqlP arameter(" @selctedli st", System.Data.SqlDbType.VarC har, 800)).Value = me.list.text
SqlCom.Parameters.Add(New System.Data.SqlClient.SqlP arameter(" @sequence" , System.Data.SqlDbType.Int, 4, System.Data.ParameterDirec tion.Input , False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion .Current, me.sequence.text))
Try
SqlCom.ExecuteNonQuery()
success = True
Catch exc As Exception
success = False
End Try
SqlCom = Nothing
SqlCon.Close()
Dim SqlCom As New System.Data.SqlClient.SqlC
SqlCon.ConnectionString = "data source=SERVERNAME;initial catalog=DATABASE;password=
"fo=True;user id=uuuu;workstation id=MACHINE;packet size=4096"
SqlCon.Open()
SqlCom.CommandText = "dbo.[DELETEPROD]"
SqlCom.CommandType = System.Data.CommandType.St
SqlCom.Connection = SqlCon
SqlCom.Parameters.Add(New System.Data.SqlClient.SqlP
SqlCom.Parameters.Add(New System.Data.SqlClient.SqlP
Try
SqlCom.ExecuteNonQuery()
success = True
Catch exc As Exception
success = False
End Try
SqlCom = Nothing
SqlCon.Close()
>Well this value comes from a hiden label and is stored as :
"1, 2, 3, 4, 5"
Then, in VB, you need to convert the above, so your parameter looks like this:
"(Select 1 Union select 2, union select 3 union select 4, union select 5)" to make your procedure work.
"1, 2, 3, 4, 5"
Then, in VB, you need to convert the above, so your parameter looks like this:
"(Select 1 Union select 2, union select 3 union select 4, union select 5)" to make your procedure work.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
oh, lol, no wonder, i had just assumed that he was using dynamic SQL, never thought to ask...