2011-09-29 41 views
0

HI我需要在數據庫中動態地創建函數。我的過程接受數據庫名稱作爲參數,然後我需要在該數據庫中動態創建此函數。 但我無法提前在數據庫中動態創建函數

Create procedure [dbo].[Sp_Insertfun] 
(
@OrgName nvarchar(100) 
) 
AS BEGIN 
Declare @orgdataInsertString nvarchar(max); 

set @orgdataInsertString='Use '[email protected]+'' ; 

set @[email protected]+char(13)+char(10)+'GO'+' 
Create function dbo.[fn_GroupDisplay](@GroupID int) 

RETURNS @Group TABLE 
(

GrpPath nvarchar(max) 

) 
as 
begin 
declare @ParentId1 int 
declare @ParentId2 varchar(50) 
declare @ParentId3 varchar(50) 
declare @parentId4 varchar(50) 
declare @Text nvarchar(max) 

set @ParentId3='''' 
begin 
set @ParentId1=(select ParentID from GroupDetails where [email protected]) 
set @ParentId4=(select GroupName from GroupDetails where [email protected]) 

while(@ParentId1>0) 
begin 
set @parentId2=(select GroupName from GroupDetails where [email protected]) 
set @ParentId3 = @ParentId2+ ''\'' + coalesce(@ParentId3,''') 
set @ParentId1=(select ParentID from GroupDetails where [email protected]) 
end 
set @[email protected][email protected] 
INSERT INTO @Group VALUES (@Text) 
end 
RETURN 
end' 

execute sp_executesql @[email protected] 
print @orgdataInsertString 
end 
+0

您的Proc。看起來很好,你爲什麼不能執行這個。問題是什麼? – Pranav

+1

你需要一個額外的'合併'(@ ParentId3,''')'。 'coalesce(@ ParentId3,'''')' –

+0

執行腳本時會出現什麼錯誤? –

回答