删除不在存储过程中工作

时间:2017-10-13 14:46:48

标签: sql sql-server

我使用以下命令执行存储过程,但删除不会运行。有什么问题?

exec sp_delete 'fruits', '''1.5'',''3.5'''

sp_delete

CREATE PROCEDURE [dbo].[sp_delete] 
@stock_type VARCHAR(255) 
,@weight VARCHAR(255)

AS 

IF @stock_type ='fruits'
BEGIN TRY
IF EXISTS(SELECT * FROM tblshop WHERE quantity='1' and weight in ( @weight))
    BEGIN
        DELETE FROM tblshop WHERE quantity='1' and weight in ( @weight )
    END
END TRY
BEGIN CATCH
END CATCH

2 个答案:

答案 0 :(得分:3)

让它变得动态:

CREATE PROCEDURE [dbo].[sp_delete] 
@stock_type VARCHAR(255) 
,@weight VARCHAR(255)
AS 
declare @sql as nvarchar(max)
IF @stock_type ='fruits'
BEGIN TRY
set @sql = '
IF EXISTS(SELECT * FROM tblshop WHERE quantity=''1'' and weight in ( ' + @weight + '))
    BEGIN
        DELETE FROM tblshop WHERE quantity=''1'' and weight in ( ' + @weight + ' )
    END'
exec(@sql)
END TRY
BEGIN CATCH
END CATCH

并将其称为[更新内联评论]

exec sp_delete 'fruits', '1.5,3.5'

答案 1 :(得分:1)

您需要将@weight值拆分为类似数组的

然后WHERE quantity='1' and weight in dbo.splitFunc(@weight)

How to pass an array into a SQL Server stored procedure