IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_privatesql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_privatesql]
GO
 
CREATE procedure [dbo].[mps_save_privatesql](
@cmpid int,
@psid int ,
@pssql nvarchar(4000),
@pscond nvarchar(4000) 
)
as
begin
set nocount on
update mps_privatesql set ps_sql=@pssql , ps_extcond=@pscond where ps_id=@psid 
-- 下面的语句是
declare @s nvarchar(4000) 

set @s = 'IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(''[dbo].[privateview_'+LTRIM(rtrim(str(@psid)))+
']'')) DROP VIEW [dbo].[privateview_'+LTRIM(rtrim(str(@psid)))+']'  
exec sp_executesql @s 
if (len(@pssql)>10) 
begin
set @s= 
N'CREATE VIEW [dbo].[privateview_'+LTRIM(rtrim(str(@psid)))+N']
AS
'+@pssql  
exec sp_executesql @s  

delete mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='private' and fldi_tbid=@psid 

insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_tbname,fldi_fieldname,
fldi_fieldcaption,fldi_datatype,fldi_size) 
Select @cmpid,'private',@psid,0,o.Name, c.name , c.name,t.name  , c.length  
From SysObjects As o , SysColumns As c , SysTypes As t
Where  o.type='V' and  o.id = c.id And c.xtype = t.xtype  and o.Name='privateview_'+LTRIM(rtrim(str(@psid)))  and t.name<>'sysname'
Order By o.name , c.name , t.name , c.domain 

-- 以下是修正
update a set a.fldi_fieldcaption = b.fldi_fieldcaption 
from mps_fieldinfo a , mps_fieldinfo b 
where a.fldi_cmpid=@cmpid and b.fldi_cmpid=@cmpid and 
a.fldi_fieldname=b.fldi_fieldname and a.fldi_ord2tag=b.fldi_ord2tag and 
a.fldi_size=b.fldi_size and a.fldi_datatype=b.fldi_datatype and b.fldi_fieldcaption<>b.fldi_fieldname 
and a.fldi_tbtype='private' and a.fldi_tbid=@psid 

update a set a.fldi_fieldcaption = b.fldi_fieldcaption 
from mps_fieldinfo a , mps_fieldinfo b 
where a.fldi_cmpid=@cmpid and b.fldi_cmpid=@cmpid and 
a.fldi_fieldname=b.fldi_fieldname and a.fldi_ord2tag=b.fldi_ord2tag and 
a.fldi_size=b.fldi_size and   b.fldi_fieldcaption<>b.fldi_fieldname and a.fldi_fieldcaption=a.fldi_fieldname 
and a.fldi_tbtype='private' and a.fldi_tbid=@psid 

end 
set nocount off
end