微軟的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>