筛选存储过程SQL Server

时间:2016-12-14 21:33:43

标签: sql sql-server tsql stored-procedures

我想在我的存储过程中过滤行,所以当我搜索一个频道时,我只获得参数字段中指定的频道值。

以下是代码:

CREATE TABLE iGuide 
    ([Zone] varchar(3), [Enabled] int, [Channel] varchar(4), [DMA] varchar(7), [Region] varchar(7), [HQ] varchar(10), [Machine] varchar(10))
;

INSERT INTO iGuide 
    ([Zone], [Enabled], [Channel], [DMA], [Region], [HQ], [Machine])
VALUES
    ('ACC', 1, 'AEN', 'Norfolk', 'Other', 'virginia', 'dzsfk'),
    ('ACC', 1, 'CNN', 'Norfolk', 'Other', 'virginia', 'dzsfk'),
    ('ACC', 1, 'ESPN', 'Norfolk', 'Other', 'virginia', 'dzsfk'),
    ('ACC', 1, 'HIST', 'Norfolk', 'Other', 'virginia', 'dzsfk'),
    ('ACC', 1, 'FOOD', 'Norfolk', 'Other', 'virginia', 'dzsfk'),
    ('ANJ', 1, 'AEN', 'Paducah', 'Pacific', 'hqs3ftbgwi', 'adanj'),
    ('ANJ', 1, 'ESPN', 'Paducah', 'Pacific', 'hqs3ftbgwi', 'adanj'),
    ('ANJ', 1, 'HIST', 'Paducah', 'Pacific', 'hqs3ftbgwi', 'adanj'),
    ('ANJ', 1, 'CNN', 'Paducah', 'Pacific', 'hqs3ftbgwi', 'adanj'),
    ('CHD', 1, 'ESPN', 'Denver', 'Pacific', 'hqs3ftbgwi', 'adchd'),
    ('CHD', 1, 'FOOD', 'Denver', 'Pacific', 'hqs3ftbgwi', 'adchd'),
    ('CHD', 1, 'DISC', 'Denver', 'Pacific', 'hqs3ftbgwi', 'adchd'),
    ('CHI', 1, 'AEN', 'Denver', 'Pacific', 'hqs3ftbgwi', 'adchd'),
    ('CHI', 1, 'FOOD', 'Chico', 'Pacific', 'hqs4ftbgwi', 'adv1chicca'),
    ('CHI', 1, 'ESPN', 'Chico', 'Pacific', 'hqs4ftbgwi', 'adv1chicca'),
    ('CHI', 1, 'CNN', 'Chico', 'Pacific', 'hqs4ftbgwi', 'adv1chicca')
;

GO

CREATE Procedure [dbo].[Usp_GetWHERE]
    @Filter Varchar(MAX)

AS

   DECLARE @columns NVARCHAR(MAX)      
           ,@sql NVARCHAR(MAX)
           ,@zone NVARCHAR(MAX)


    SET @columns = N''
    --Get column names for entire pivoting
    SELECT @columns += N', ' + QUOTENAME(Zone)
      FROM (select distinct Zone 
            from iGuide  WHERE (DMA LIKE @Filter)   OR (Channel = @Filter) OR (@Filter = ' Select All') OR (@Filter = 'Select All') OR (Zone LIKE @Filter) OR (Machine LIKE @Filter) OR (HQ LIKE @Filter) OR (Region LIKE @Filter)
         ) AS T

    select @columns;

    SET @zone = N''
    --Get column names for entire pivoting
    SELECT @zone += N', ' + QUOTENAME(Zone, '''')
      FROM (select distinct Zone 
            from iGuide  WHERE (DMA LIKE @Filter)   OR (Channel = @Filter) OR (@Filter = ' Select All') OR (@Filter = 'Select All') OR (Zone LIKE @Filter) OR (Machine LIKE @Filter) OR (HQ LIKE @Filter) OR (Region LIKE @Filter)
         ) AS T

    select  @zone;    

    SET @zone = '(' + substring(@zone, 3, len(@zone)-2) + ')';         
    select  @zone;


    SET @sql = N'
    SELECT  [Channel], ' + STUFF(@columns, 1, 2, '') + '
    FROM (SELECT Channel, [Zone]
          FROM iGuide 
          WHERE [Zone] IN ' + @zone + ')  as source
    PIVOT
    (
      count([Zone]) FOR [Zone] IN ('
      + STUFF(REPLACE(@columns, ', [', ',['), 1, 1, '')
      + ')
    ) AS Pivot1  ORDER BY Channel
    ' 
    select @sql;
    EXEC sp_executesql @sql;

GO 
EXECUTE [dbo].Usp_GetWHERE N'AEN';      

本守则的结果:

    Channel ACC ANJ CHI
1   AEN      1   1   1
2   CNN      1   1   1
3   ESPN     1   1   1
4   FOOD     1   0   1
5   HIST     1   1   0

执行' AEN'作为一个值应该是:

    Channel ACC ANJ CHI
1   AEN      1   1   1

3 个答案:

答案 0 :(得分:1)

为您的数据透视添加了where子句。

ALTER Procedure [dbo].[Usp_GetWHERE]
    @Filter Varchar(MAX)

AS

   DECLARE @columns NVARCHAR(MAX)      
           ,@sql NVARCHAR(MAX)
           ,@zone NVARCHAR(MAX)


    SET @columns = N''
    --Get column names for entire pivoting
    SELECT @columns += N', ' + QUOTENAME(Zone)
      FROM (select distinct Zone 
            from iGuide  WHERE (DMA LIKE @Filter)   OR (Channel = @Filter) OR (@Filter = ' Select All') OR (@Filter = 'Select All') OR (Zone LIKE @Filter) OR (Machine LIKE @Filter) OR (HQ LIKE @Filter) OR (Region LIKE @Filter)
         ) AS T

    select @columns;

    SET @zone = N''
    --Get column names for entire pivoting
    SELECT @zone += N', ' + QUOTENAME(Zone, '''')
      FROM (select distinct Zone 
            from iGuide  WHERE (DMA LIKE @Filter)   OR (Channel = @Filter) OR (@Filter = ' Select All') OR (@Filter = 'Select All') OR (Zone LIKE @Filter) OR (Machine LIKE @Filter) OR (HQ LIKE @Filter) OR (Region LIKE @Filter)
         ) AS T

    select  @zone;    

    SET @zone = '(' + substring(@zone, 3, len(@zone)-2) + ')';         
    select  @zone;


    SET @sql = N'
    SELECT  [Channel], ' + STUFF(@columns, 1, 2, '') + '
    FROM (SELECT Channel, [Zone]
          FROM iGuide 
          WHERE [Zone] IN ' + @zone + ')  as source
    PIVOT
    (
      count([Zone]) FOR [Zone] IN ('
      + STUFF(REPLACE(@columns, ', [', ',['), 1, 1, '')
      + ')
    ) AS Pivot1
    WHERE channel = ''' + @filter + ''' ORDER BY Channel
    ' 
    select @sql;
    EXEC sp_executesql @sql;

GO 
EXECUTE [dbo].Usp_GetWHERE N'AEN';   

答案 1 :(得分:1)

您获得5个结果,因为@filter可能与DMAChannelZoneMachineHQ或{{ 1}}。

如果您想严格按照频道进行过滤,请将Region更改为

@zone

或通过添加 SELECT @zone += N', ' + QUOTENAME(Zone, '''') FROM (select distinct Zone from iGuide WHERE Channel = @Filter ) AS T

的条件在主查询中对其进行过滤
channel=@Filter

答案 2 :(得分:0)

您需要做的就是将其添加到WHERE

SET @sql = N'
SELECT  [Channel], ' + STUFF(@columns, 1, 2, '') + '
FROM (SELECT Channel, [Zone]
      FROM iGuide 
      WHERE [Zone] IN ' + @zone + ' AND [Channel] = @Your_Param_Here)  as source