I made a linked of my SQLSERVER
with FIREBIRD
. But at the time I pass a variable inside the query to return the record amount I get the error:
Message 137, Level 15, State 1, Line 1 Must declare the scalar variable "@QtdReg".
Script :
DECLARE @LINKED VARCHAR(50)
DECLARE @SQL_LINKED VARCHAR(7000)
DECLARE @OPENQRY VARCHAR(7000)
DECLARE @QtdReg VARCHAR(700)
declare @codpro varchar(10)
SET @codpro = '1000'
SET @LINKED = 'Mylinked'
SET @SQL_LINKED = 'SELECT count(1) as total FROM produto i WHERE CAST(I.codigoref AS VARCHAR(10)) = '''''+@CodPro+''''' and i.caixa=201 '
SET @OPENQRY = ' SELECT @QtdReg = ItemFT.total FROM Openquery('+@LINKED+', '''+@SQL_LINKED+''') ItemFT'
EXECUTE (@OPENQRY)
SELECT @QTDREG
I tried to use SP_executesql
also the error occurs. What am I doing wrong?