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


-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mps_query_sql_byname]
@cmpid int,
@sqlname varchar(100),
@sqltype varchar(100)
AS
BEGIN
SET NOCOUNT ON;
--先从mps_sql中查找
declare @sql_name varchar(100),
@sql_type nvarchar(100),
@sql_text nvarchar(4000),
@sql_params varchar(2000),
@sql_sourcetables varchar(200),
@sql_targettables varchar(200),
@sql_keyparam varchar(30)
select  
@sql_name=sql_name,
@sql_type=sql_type,
@sql_text=sql_text,
@sql_params=sql_params,
@sql_keyparam=sql_keyparam
from mps_sql
where sql_name=@sqlname and sql_cmpid=@cmpid
--如果没有找到,则从pub_sql中查找
if @@ROWCOUNT = 0
begin
select  @sql_name=sql_name,
@sql_type=sql_type,
@sql_text=sql_text,
@sql_params=sql_params,
@sql_keyparam=sql_keyparam
from ebs_kernel.dbo.pub_sql
where sql_name=@sqlname 
end
--返回结果
--if @sql_text is not null
begin
select  @sql_name as sql_name,
@sql_type  as  sql_type,
@sql_text as sql_text,
@sql_params as sql_params,
@sql_keyparam as sql_keyparam,
case when LEN(@sql_params)>0 then 'PROC' else 'SQL' end as sql_ptype,
case when @sql_name is null then 0 else 1 end as qresult
end
END