1

这是我的情况。我知道必须有一个简单的答案,但我只是不熟悉 TSQL 知道如何。下面我有一个存储过程的主要查询,它选择了我需要的数据。到目前为止,我一直在工作,除了我需要调用一个名为 GetRecordMediaById 的单独存储过程,在其中我将 PhotoId 列中的 Id 提供给它,然后它从适当的数据库中选择 BLOB 数据,然后该数据需要在最后成为它自己的列查询或替换原来的 PhotoId 列。

我不知道该怎么做。我试过实现临时表,但我什至无法让它执行。

这是我的代码:

ALTER PROCEDURE [dbo].[GetRollCallData] 
@Ids        VARCHAR(255),
@LexiconId  INT,
@UUID       UNIQUEIDENTIFIER,
@ReadOnly   INT
AS


DECLARE @TableCode INT
SET @TableCode = 58
EXEC InsertInSelectionCache @Ids, @UUID, @TableCode, 0
WITH DOACTE AS(
SELECT ROW_NUMBER() OVER(PARTITION BY [File].Id ORDER BY CustomRecordsetId DESC) AS RowNumber, [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, [People].InvertedName, CustomFieldValue.Value as DateofArrest
    FROM FileType2Lexicon, SelectionCache, [People], [File]
    INNER JOIN [CustomRecordSet]
    ON [CustomRecordset].RecordId = [File].Id
    INNER JOIN CustomFieldValue
    ON  [CustomRecordset].Id = CustomFieldValue.CustomRecordsetId
    INNER JOIN [CustomField2Lexicon]
    ON CustomField2Lexicon.CustomFieldId = CustomFieldValue.CustomFieldId
    WHERE   [File].Id = SelectionCache.RecordId
    AND SelectionCache.UUID = @UUID
    AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
    AND     [File].Id <> 0 
    AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
    AND     FileType2Lexicon.LexiconId = @LexiconId
    AND     [File].ClientIdString = [People].ClientIdString
    AND     CustomFieldValue.Value <> ''
    AND     CustomField2Lexicon.Label = 'Date of Arrest'),


PHOTOCTE AS(
SELECT  [File].Id, CustomFieldValue.Value as PhotoId
    FROM FileType2Lexicon, SelectionCache, [People], [File]
    INNER JOIN [CustomRecordSet]
    ON [CustomRecordset].RecordId = [File].Id
    INNER JOIN CustomFieldValue
    ON  [CustomRecordset].Id = CustomFieldValue.CustomRecordsetId
    INNER JOIN [CustomField2Lexicon]
    ON CustomField2Lexicon.CustomFieldId = CustomFieldValue.CustomFieldId
    WHERE   [File].Id = SelectionCache.RecordId
    AND SelectionCache.UUID = @UUID
    AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
    AND     [File].Id <> 0 
    AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
    AND     FileType2Lexicon.LexiconId = @LexiconId
    AND     [File].ClientIdString = [People].ClientIdString
    AND     CustomFieldValue.Value <> ''
    AND     CustomField2Lexicon.Label = 'Booking Photo')

SELECT DOACTE.*, PHOTOCTE.PhotoId
FROM DOACTE
INNER JOIN 
PHOTOCTE
ON DOACTE.Id = PHOTOCTE.Id
WHERE DOACTE.RowNumber = 1

编辑:

我的解决方案是创建一个标量函数来解析 BLOB 数据库中的 Id 并返回 BLOB 数据。

SELECT DOACTE.*, dbo.GetImagebyId(PHOTOCTE.PhotoId) as Photo,
            FROM DOACTE
            INNER JOIN 
            PHOTOCTE
            ON DOACTE.Id = PhotoCTE.Id
            WHERE DOACTE.RowNumber = 1
4

1 回答 1

1

您可以声明一个@table_variable 并将“EXEC InsertInSelectionCache @Ids, @UUID, @TableCode, 0”的结果插入到表变量中。

然后您可以在最终查询中加入@table_variable。

有关示例,请参见此处:如何从存储过程返回临时表

于 2013-06-14T15:24:54.310 回答