I created the following function to return the binary value of an image. However, when you run the query you are displaying the following message:
CREATE FUNCTION [dbo].[fn_image](@DSCAMINHO VARCHAR(4000), @DSARQUIVO VARCHAR(4000))
RETURNS VARBINARY (MAX)
AS
BEGIN
DECLARE
@EXTENSAO VARCHAR(5) = '.PNG',
@SQL NVARCHAR(4000),
@BULKCOLUMN VARBINARY (MAX)
SET @SQL = 'SELECT @BULKCOLUMN = BULKCOLUMN
FROM OPENROWSET(BULK ''' + @DSCAMINHO + @DSARQUIVO + @EXTENSAO + ''', SINGLE_BLOB'+') AS IMG'
EXEC sp_executesql @SQL, N'@BULKCOLUMN VARBINARY (MAX) OUT', @BULKCOLUMN OUTPUT;
RETURN @BULKCOLUMN
END
But when you run it
SELECT
CAST (CDEMPRESA AS VARCHAR(20)) CDEMPRESA,
CAST (CDDESTAQUE AS VARCHAR(20)) CDDESTAQUE,
CAST (DSDESTAQUE AS VARCHAR(50)) DSDESTAQUE,
dbo.FN_IMAGE(DIRDESTAQUE,DSDESTAQUE) AS DIRDESTAQUE
FROM
DESTAQUE
I get the following error message:
Message 557, Level 16, State 2, Line 1 Only functions and some extended stored procedures can be performed in a function.