如何通过select而不是= by vb.net和mssql存储过程来执行存储过程
Dim sSQL As String
Dim objConn As SqlConnection
Dim objcmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds = New DataSet()
sSQL = "getinvoice"
objConn = utility.getconnect
objcmd = New SqlCommand(sSQL, objConn)
objcmd.CommandType = CommandType.StoredProcedure
objcmd.Parameters.Add("@invoiceid", SqlDbType.VarChar)
objcmd.Parameters.Item("@invoiceid").Value = "1,5,13,18" '<-- problem
da = New SqlDataAdapter(objcmd)
da.Fill(ds)
ALTER PROCEDURE [dbo].[getinvoices]
@invoiceid varchar(50)
AS
BEGIN
select * from invoice where invoiceid in @invoiceid '<-- problem
END
答案 0 :(得分:1)
我认为你需要使用动态查询。将过程更改为
ALTER PROCEDURE [dbo].[getinvoices]
@invoiceid varchar(50)
AS
BEGIN
declare @sql nvarchar(4000)
set @sql =' select * from invoice where invoiceid in ( '+ @invoiceid +')'
EXEC (@sql)
END
或者使用拆分功能将值拆分为表变量(那里有很多拆分函数,快速搜索会抛出一个)。
ALTER PROCEDURE [dbo].[getinvoices] @invoiceid VARCHAR(50)
AS
BEGIN
SELECT *
FROM invoice
WHERE invoiceid IN ( SELECT *
FROM dbo.splitstring(@invoiceid) )
END