IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_751_get_grades]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_751_get_grades]
GO
CREATE PROCEDURE userfunc_751_get_grades(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@viewid varchar(10),@date varchar(10),@types varchar(50),@branch varchar(20),@area varchar(50),@store varchar(20))
as
begin



    set nocount on;
    declare @sql1 nvarchar(1000);
    create table #temp1 (grade varchar(10));
    insert into #temp1(grade) values('全部类别');
    
    set @sql1 = 'insert into #temp1(grade) select distinct [门店类别] FROM [DBTC].[datas].[dbo].[dd_date_md]'
           + ' where [数据日期] =''' + @date + ''' and [商圈类型] in (' +  @types + ')';
           
    if @viewid = 'branchf'
    begin
       set @sql1 = @sql1 + ' and [分公司名称] =''' + @branch + '''';
    end 
    else if @viewid = 'area'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] =''' + @branch + ''' and [区域] = ''' + @area + '''';
    end
    else if @viewid = 'store'
    begin 
       set @sql1 = @sql1 + ' and [分公司名称] =''' + @branch + ''' and [门店号] = ''' + @store + '''';
    end
    
    exec(@sql1);
    select grade from #temp1;


end