在列SQL上调用存储过程

时间:2013-06-14 14:24:44

标签: tsql exec procedure

这是我的情况。我知道必须有一个简单的答案,但我不熟悉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

1 个答案:

答案 0 :(得分:0)

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

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

请参阅此处的示例:How to return temporary table from stored procedure