Вы находитесь на странице: 1из 3

-- exec intbiladm_lst_supcategories 1,null,2,null,10,1

ALTER PROCEDURE dbo.intbiladm_lst_supcategories


(
@relid int = null,
@supplierid int = null,
@keyword varchar(10) = null,
@type int,
@orderby varchar(50) = null,
@pagesize int,
@pagenumber int
)
AS
BEGIN
DECLARE @sql varchar(2048)
DECLARE @where varchar(512)
DECLARE @totalrows int

SELECT @sql= 'select SC.RELID,S2.SECID, S2.NAME AS SECNAME, C.MAINCAT, ' +


CHAR(13) +
'C2.NAME AS MAINCATNAME, C.CATID, C.NAME, ' + CHAR(13) +
'SC.PENTACODE, SC.PENTANAME, SC2.PENTANAME AS PENTAMAINCATNAME,
SC.INDEXKATEGORINAME, SC.INDEXGRUPNAME, SC.DESPECKATEGORINAME, SC.DESPECGRUPNAME'
+ CHAR(13) +
'from SUPPLIER_CATS SC ' + CHAR(13) +
'left join SUPPLIER_CATS SC2 on SC.PENTAMAINCAT = SC.PENTACODE' +
CHAR(13) +
'left join CATEGORIES C on C.CATID = SC.CATID' + CHAR(13) +
'left join SECTIONS S2 on S2.SECID = C.SECID' + CHAR(13) +
'left join CATEGORIES C2 on C2.CATID = C.MAINCAT '

SELECT @where = ''

IF @supplierid = 1
SELECT @where = @where + dbo.PutAND(@where) + ' SC.PENTACODE IS NOT
NULL ' + CHAR(13)
ELSE IF @supplierid = 2
SELECT @where = @where + dbo.PutAND(@where) + ' SC.INDEXGRUPID IS NOT
NULL ' + CHAR(13)
ELSE IF @supplierid = 3
SELECT @where = @where + dbo.PutAND(@where) + ' SC.DESPECGRUPID IS NOT
NULL ' + CHAR(13)

IF @relid is not null


SELECT @where = @where + dbo.PutAND(@where) + ' SC.RELID = ' +
cast(@relid as varchar(10)) + CHAR(13)

IF @keyword is not null


SELECT @where = @where + dbo.PutAND(@where) + ' C.NAME like ''%' +
@keyword + '%''' + CHAR(13)

IF @type = 1
SELECT @where = @where + dbo.PutAND(@where) + ' C.CATID IS NOT NULL ' +
CHAR(13)
ELSE IF @type = 2
SELECT @where = @where + dbo.PutAND(@where) + ' C.CATID IS NULL ' +
CHAR(13)

IF @where <> ''


SELECT @sql = @sql + 'WHERE ' + @where

/*
IF @orderby = 'CATID'
SELECT @sql = @sql + 'order by C.CATID '
ELSE IF @orderby = 'CATNAME'
SELECT @sql = @sql + 'order by C.NAME '
ELSE IF @orderby = 'ADDDATE'
SELECT @sql = @sql + 'order by C.ADDED '
ELSE IF @orderby = 'UPDDATE'
SELECT @sql = @sql + 'order by C.UPDATED '
ELSE IF @orderby = 'ENABLED'
SELECT @sql = @sql + 'order by C.ENABLED '
ELSE
SELECT @sql = @sql + 'order by C.NAME '
*/

DECLARE @start int, @end int

IF @pagesize <> 0 AND @pagenumber <> 0


BEGIN
SET @start = (((@pagenumber - 1) * @pagesize) + 1)
SET @end = (@start + @pagesize - 1)
END

CREATE TABLE #SupCatTemp


(
ROW int IDENTITY(1,1) PRIMARY KEY,
RELID int,
SECID int,
SECNAME varchar(50),
MAINCAT int,
MAINCATNAME varchar(100),
CATID int, NAME varchar(100),
PENTACODE varchar(10),
PENTANAME varchar(100),
PENTAMAINCATNAME varchar(100),
INDEXKATEGORINAME varchar(150),
INDEXGRUPNAME varchar(150),
DESPECKATEGORINAME varchar(150),
DESPECGRUPNAME varchar(150)
)

SELECT @sql = 'INSERT INTO


#SupCatTemp(RELID,SECID,SECNAME,MAINCAT,MAINCATNAME,CATID,NAME,PENTACODE,PENTANAME
,PENTAMAINCATNAME,INDEXKATEGORINAME,INDEXGRUPNAME,DESPECKATEGORINAME,DESPECGRUPNAM
E) ' + @sql
PRINT @sql
exec(@sql)

SELECT @totalrows = @@rowcount


If @pagesize > 0 AND @pagenumber > 0
SELECT *, @totalrows as TOTALROWS
FROM #SupCatTemp
WHERE ROW BETWEEN @start and @end
ELSE
SELECT *, @totalrows as TOTALROWS
FROM #SupCatTemp

END