The problem was solved through the trial:
CREATE PROCEDURE FOTO_USUARIO
@Codigo integer,
@TipoPessoa integer,
@FOTO VARBINARY(max) OUTPUT
AS
begin
DECLARE @FILE VARCHAR(100);
DECLARE @nome_usuario VARCHAR(100);
DECLARE @CAMINHO NVARCHAR(MAX);
DECLARE @COMANDO NVARCHAR(MAX);
DECLARE @Parametro NVARCHAR(max);
DECLARE @PATH VARCHAR(MAX);
DECLARE @CODEXT VARCHAR(17);
DECLARE @TIPO_GRAVACAO_FOTO INTEGER;
SELECT @TIPO_GRAVACAO_FOTO = NOMEARQUIVOWEBCAM,
@PATH = COALESCE(PATH_FOTO_ALT, PATH_FOTO)
FROM CONFIG WITH(NOLOCK)
SET @Parametro = N'@fotoParametro varbinary(max) output';
IF (@TipoPessoa = 1)
BEGIN
SELECT @FILE = NOME,
@CODIGO = CODIGO,
@CODEXT = CODEXT,
@nome_usuario = NOME
FROM FISICA WITH(NOLOCK)
WHERE CODIGO = @Codigo
IF (@TIPO_GRAVACAO_FOTO = 1)
BEGIN
SET @FILE = @CODEXT;
END;
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(@FILE, ' ', '_'),'\',''),'/',''),':',''),
'*',''),'?',''),'>',''),'<',''),'|',''),'"',''),'.',''),'''','') + '_' +
CAST(@CODIGO AS VARCHAR) + '.JPG';
END
ELSE IF (@TipoPessoa = 2)
BEGIN
SELECT @FILE = PAI_NOME,
@CODIGO = FA.CODIGO
FROM FAMILIAS FA WITH(NOLOCK)
INNER JOIN DADOSPF D WITH(NOLOCK)
ON D.FAMILIA = FA.CODIGO
WHERE D.FAMILIA = @Codigo
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(@FILE, ' ', '_'),'\',''),'/',''),':',''),
'*',''),'?',''),'>',''),'<',''),'|',''),'"',''),'.',''),'''','') + '_' +
CAST(@CODIGO AS VARCHAR) + '_pai.JPG';
END
ELSE IF (@TipoPessoa = 3)
begin
SELECT @FILE = MAE_NOME,
@CODIGO = FA.CODIGO
FROM FAMILIAS FA WITH(NOLOCK)
INNER JOIN DADOSPF D WITH(NOLOCK)
ON D.FAMILIA = FA.CODIGO
WHERE D.FAMILIA = @Codigo
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(@FILE, ' ', '_'),'\',''),'/',''),':',''),
'*',''),'?',''),'>',''),'<',''),'|',''),'"',''),'.',''),'''','') + '_' +
CAST(@CODIGO AS VARCHAR) + '_mae.JPG';
END
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'É','E'),'È','E'),'Ê','E');
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'Í','I'),'Ì','I'),'Î','I');
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(@FILE, 'Á','A'),'À','A'),'Â','A'),'Ã','A');
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(@FILE, 'Ó','O'),'Ò','O'),'Ô','O'),'Õ','O');
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'Ú','U'),'Ù','U'),'Û','U');
SET @FILE = REPLACE(REPLACE(@FILE, 'Ý','Y'),'`Y','Y');
SET @FILE = REPLACE(@FILE, 'Ç','C');
set @CAMINHO = @PATH + '\' + @FILE;
print @CAMINHO;
SET @COMANDO = N'SELECT @fotoParametro = BulkColumn FROM OPENROWSET(BULK N' + '''' + @CAMINHO + '''' + ' , SINGLE_BLOB) Load'
BEGIN TRY
EXEC sp_executesql @COMANDO, @Parametro, @fotoParametro = @foto OUTPUT;
END TRY
BEGIN CATCH
set @foto = NULL
END CATCH
if ((@foto is null) and (@TipoPessoa = 1))
begin
if (@TIPO_GRAVACAO_FOTO = 1)
begin
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(@NOME_USUARIO, ' ', '_'),'\',''),'/',''),':',''),
'*',''),'?',''),'>',''),'<',''),'|',''),'"',''),'.',''),'''','') + '_' +
CAST(@CODIGO AS VARCHAR) + '.JPG';
end
else
begin
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(
REPLACE(@CODEXT, ' ', '_'),'\',''),'/',''),':',''),
'*',''),'?',''),'>',''),'<',''),'|',''),'"',''),'.',''),'''','') + '_' +
CAST(@CODIGO AS VARCHAR) + '.JPG';
end
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'É','E'),'È','E'),'Ê','E');
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'Í','I'),'Ì','I'),'Î','I');
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(@FILE, 'Á','A'),'À','A'),'Â','A'),'Ã','A');
SET @FILE = REPLACE(REPLACE(REPLACE(REPLACE(@FILE, 'Ó','O'),'Ò','O'),'Ô','O'),'Õ','O');
SET @FILE = REPLACE(REPLACE(REPLACE(@FILE, 'Ú','U'),'Ù','U'),'Û','U');
SET @FILE = REPLACE(REPLACE(@FILE, 'Ý','Y'),'`Y','Y');
SET @FILE = REPLACE(@FILE, 'Ç','C');
set @CAMINHO = @PATH + '\' + @FILE;
SET @COMANDO = N'SELECT @fotoParametro = BulkColumn FROM OPENROWSET(BULK N' + '''' + @CAMINHO + '''' + ' , SINGLE_BLOB) Load'
BEGIN TRY
EXEC sp_executesql @COMANDO, @Parametro, @fotoParametro = @foto OUTPUT;
END TRY
BEGIN CATCH
set @foto = NULL
END CATCH
end
end
I could not return in batch, because it is a Procedure. But I could return if the Registration has photo or not through the system report, on screen.