更新其值从函数返回的列

时间:2013-03-01 08:33:09

标签: sql-server tsql

我在使用从用户函数返回的值更新表时遇到问题,我不知道该怎么做。

环境: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来编写此语句

1 个答案:

答案 0 :(得分: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