IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_q_ord_1details]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_q_ord_1details]
GO

create procedure [dbo].[mps_q_ord_1details](
@cmpid int,
@bizid int,
@ordid bigint,
@ord2tag int,
@ord2group nvarchar(50),
@pagemysize int 
) as
begin
set nocount on 
declare @dbname varchar(30) 
declare @tbname varchar(50) 
declare @pkname varchar(50) 
exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,@ord2tag,@tbname output , @pkname output ,@dbname output 
set @tbname  = @dbname + @tbname 

declare @sql nvarchar(MAX) 
set @sql = '' 

if (@pagemysize>1) 
begin
set @sql = 'select top '+convert(varchar(10),@pagemysize)+N' * from '+@tbname+
N'  where ord2_ordid=@ordid  and ord2_tag=@ord2tag order by ord2_order, ord2_id 
select ord2_id  as ord2id_page from '+@tbname+'  where ord2_ordid=@ordid  and ord2_tag=@ord2tag and ord2_group=@ord2group order by ord2_order, ord2_id '
end else 
begin
set @sql = 'select * from '+@tbname+N'  where ord2_ordid=@ordid  and ord2_tag=@ord2tag and ord2_group=@ord2group order by ord2_order, ord2_id
select 0  as ord2id_page ' 
end
exec sp_executesql @sql, N'@ordid bigint ,@ord2tag int,@ord2group nvarchar(50) ', @ordid  ,@ord2tag  , @ord2group 
set nocount off 
end