微軟的SQL腳本生成令我傷透了心……我一直以為是我的程序上的問題,或者我操作上的失誤,
並且,客服務人員屢屢埋怨我的程序bug,多次測試之後,靠,原來都是微軟惹的禍……
SQL Server 的腳本生成有不少漏洞,經常由它生成的腳本運行起來卻有錯誤。下面舉例說明:
1、並沒有根據sysdenpends的依賴關系生成SQL代碼,而是根據“優先級”(呵呵,所謂的優先級)來生成。
比如:他認為vIEw的優先級就要比function高。
那麼,我寫了下面的測試程序,形成如下的依賴關系:fnT1 <-- vwT1 <-- fnT2
就是,vIEw vwT1處於依賴的中間。
------------------------------------
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
-------------------------------------
運行到數據庫之後,用Enterprise生成SQL代碼。(選項不一樣,會有所不同,我沒有選數據庫和用戶的)
-----------------------------------------------------------------------------------
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
-----------------------------------------------------------------------------
呵呵,一眼你就可以看出來了,建立vIEw要比建立function先。