0

我无法使用从用户函数返回的值更新表,我不知道该怎么做。

环境:MsSql

 DECLARE @files TABLE  
 (  
  Id uniqueidentifier,  
  Name nvarchar(255),  
  FolderId uniqueidentifier,  
  IsCheckedOut bit,  
  CheckedOutBy nvarchar(50),  
  IsDeleted bit,  
  IsVirtual bit,  
  Content varbinary(MAX),
  FolderPath nvarchar(MAX),
  CultureCode varchar(16)
 ) 


  UPDATE @files   
 SET Content= (SELECT TOP 1 Content FROM fnGetFileContentById (@changeListId, Id, @autoRevert,@cultureCode,@defaultCultureCode)), 
CultureCode =  (SELECT TOP 1 CultureCode FROM fnGetFileContentById (@changeListId, Id, @autoRevert,@cultureCode,@defaultCultureCode)), 
     FolderPath=(SELECT TOP 1 FullPath FROM Folder WHERE Id= FolderId)  

函数脚本

FUNCTION [dbo].[fnGetFileContentById]        
(        
 @changeListId UNIQUEIDENTIFIER,        
 @fileId UNIQUEIDENTIFIER,        
 @autoRevert BIT,      
 @cultureCode VARCHAR(16),    
 @defaultCultureCode VARCHAR(16)    
)        
RETURNS @fileContent TABLE        
(        
 Id UNIQUEIDENTIFIER PRIMARY KEY NOT NULL,        
 FileId UNIQUEIDENTIFIER NOT NULL,        
 Content VARBINARY(MAX) NOT NULL,        
 [Version] INT NOT NULL,        
 ChangeListId UNIQUEIDENTIFIER,        
 FileTypeId INT NOT NULL  ,      
 CultureCode VARCHAR(16)    
)        

我不想重新运行 fnGetFileContentById 函数来更新 CultureCode,如何通过一次执行 fnGetGileContentById 编写此语句

4

1 回答 1

1

也许是这样的:

UPDATE @files 
    SET Content=FileContent.Content,
    CultureCode =FileContent.CultureCode,
    FolderPath= folder.FullPath
FROM @files
    CROSS APPLY 
   ( 
        SELECT TOP 1 
            Content,
            CultureCode
        FROM 
            fnGetFileContentById(@changeListId, Id, @autoRevert,@cultureCode,@defaultCultureCode)
   ) FileContent
   CROSS APPLY
   (
        SELECT TOP 1 
            FullPath 
        FROM 
            Folder 
        WHERE 
            f.Id = FolderId
   ) AS folder
于 2013-03-01T08:50:34.483 回答