2

在复选框方面需要您的帮助。这是我的代码:

CREATE PROCEDURE stad_list

@inp_location_code numeric (3,0) = -1
@inp_item_code_bgn numeric (5,0) = -1
@inp_item_code_end numeric (5,0) = -1
@inp_shipped numeric (2,0) = 0,    --checkbox, if it is checked then returns the items in table which were shipped (value in table is 1)
@inp_check numeric (2,0) = 0, --checkbox, if it is checked then returns data which is still in process of checking for availability in warehouse (value in table is 0)
@inp_not_ship numeric(2,0) = 0   --checkbox, if it is checked then returns data which had not been shipped during some problems (valuein table is 2)

AS

BEGIN
   SET NOCOUNT ON;

   DECLARE @SELECT as varchar (3000) = ''
   DECLARE @WHERE as varchar (1000) = ''

   IF (@inp_location_code != -1)
      SET @WHERE = @WHERE + ' WHERE [dbo].item.location =' + CONVERT(varchar,@inp_location_code)

   IF (@inp_item_code_bgn != -1)
       SET @WHERE = @WHERE + ' AND [dbo].item.code>=' + CONVERT(varchar, @inp_item_code_bgn)

   IF (@inp_item_code_end != -1)
       SET @WHERE = @WHERE + ' AND [dbo].item.code<=' + CONVERT(varchar, @inp_item_code_end)

   IF (@inp_shipped = 1)
      SET @WHERE = @WHERE + ' AND [dbo].item.ship = 1'

   IF (@inp_check = 1)
      SET @WHERE = @WHERE + ' AND [dbo].item.ship = 0'

   IF (@inp_not_ship = 1)
      SET @WHERE = @WHERE + ' AND [dbo].item.ship = 2'



    SET @SELECT = @SELECT + '
        SELECT
             [dbo].item.name as ''NAME'',
             [dbo].item.location as ''LOCATION'',
             [dbo].item.code as ''CODE'',
             [dbo].item.ship as ''STATUS''
        FROM [dbo].item '
        + @WHERE 


    EXEC (@SELECT)

    RETURN @@ROWCOUNT

END

当我只选中一个复选框时它工作正常,但在选中其中 2 个时无法获取任何数据。那么当我想选中两个复选框时该怎么办?有什么建议么?

这是我的桌子

Id     Item_code    Item_name   Item_location   Item_ship
1      14789        Juice       Tokyo            0
2      14785        Boots       Tokyo            2
3      98744        Hat         Osaka            0
4      36987        Socks       Kyoto            1

并且有三个复选框

  • 已发货

  • 检查

  • 未发货

如果我检查发货,那么输出将是这样的:

Id     Item_code    Item_name   Item_location   Item_ship
    4      36987        Socks       Kyoto            1

但我想这样做。当我检查已发货和检查时,输出将是:

Id     Item_code    Item_name   Item_location   Item_ship
    1      14789        Juice       Tokyo            0
    3      98744        Hat         Osaka            0
    4      36987        Socks       Kyoto            1
4

2 回答 2

2

使用临时表作为参数表

IF (@inp_shipped = 1) OR (@inp_check = 1) OR (@inp_not_ship = 1)
BEGIN
  IF OBJECT_ID('tempdb.dbo.#Parameters') IS NOT NULL DROP TABLE dbo.#Parameters
  SELECT *
  INTO #Parameters
  FROM (
        SELECT CASE WHEN @inp_shipped = 1 THEN 1 END AS Ship
        UNION ALL
        SELECT CASE WHEN @inp_check = 1 THEN 0 END
        UNION ALL
        SELECT CASE WHEN @inp_not_ship = 1 THEN 2 END
        ) p
   WHERE p.Ship IS NOT NULL     
   SET @WHERE = @WHERE + ' AND [dbo].item.ship IN (SELECT ship FROM #Parameters)'
 END 

为了更好的性能(使用索引查找操作)如果你在ship列上有索引,需要在临时表上创建索引:

CREATE INDEX x ON #Parameters(ship)

在此处输入图像描述

于 2013-03-27T11:37:08.587 回答
1

您可以将您的选项组合到这种结构中:

declare @orcheck varchar(255)
set @orcheck = 'AND 1=2 ('

IF (@inp_shipped = 1)
SET @orcheck = @orcheck + ' OR [dbo].item.ship = 1'

IF (@inp_check = 1)
SET @orcheck = @orcheck + ' OR [dbo].item.ship = 0'

IF (@inp_not_ship = 1)
SET @orcheck = @orcheck + ' OR [dbo].item.ship = 2'

set @orcheck = @orcheck + ')'

这样,当您选择了所有 3 个复选框时,WHERE 子句将如下所示:

AND (1=2
    OR [dbo].item.ship = 1
    OR [dbo].item.ship = 0
    OR [dbo].item.ship = 2
)

当没有选择任何内容时,它将是:

AND (1=2
)

这是你所期望的,我相信。

于 2013-03-27T10:55:43.840 回答