微软的SQL脚本生成令我伤透了心——我一直以为是我的程序上的问题,或者我操作上的失误,并且,客服人员屡屡埋怨我的程序bug,多次测试之后,才发现,原来都是微软惹的祸……
Sql Server的脚本生成有不少漏洞,经常由_art_center>它生成的脚本运行起来会有错误。下面举例说明:
1. 并没有根据sysdenpends的依赖关系生成SQL代码,而是根据所谓的“优先级”来生成。
比如:他认为view的优先级就要比function高。
那么,我写了下面的测试程序,形成如下的依赖关系:fnT1 <-- vwT1 <-- fnT2
就是,view vwT1处于依赖的中间。
_NOBR> _CODE>create function fnT1()
returns Integer
as
begin
return 123
end
go
create view vwT1
as
select aa=dbo.fnT1()
go
create function fnT2()
returns table
as
return (select * from vwT1)
go_CODE>
_NOBR>
运行到数据库之后,用Enterprise生成SQL代码。要注意选项不一样,生成的代码会有所不同,在这里我没有选数据库和用户。
_NOBR> _CODE>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnT1]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnT1]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fnT2]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fnT2]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vwT1]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vwT1]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
Create view vwT1
As
Select aa=dbo.fnT1()
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
Create function fnT1()
Returns Integer
As
begin
Return 123
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
Create function fnT2()
Returns Table
As
Return (Select * From vwT1)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO_CODE>
_NOBR>