I'm trying to use the same scalar variables for more than one query, but during the execution of the second query, I get the error "Must declare scalar variable @new_serial".
Here is my code:
BEGIN TRANSACTION T1;
DECLARE @refDoc1 CHAR(10)
DECLARE @refDoc2 CHAR(10)
DECLARE @docAmount DECIMAL(18,4)
DECLARE @docIvaAmount DECIMAL(18,4)
DECLARE @new_serial CHAR(10)
SET @refDoc1 = '0000671853'
SET @refDoc2 = '0000682341'
SET @docAmount = 9.38
SET @docIvaAmount = (SELECT @docAmount * 0.22)
SET @new_serial = (SELECT format(autonum + 1, '0000000000') FROM serialtable WHERE tablecode = 'prog\MYTABLE')
SELECT
@new_serial,
pncodute,
pncodcau,
@docAmount
FROM mytable
WHERE pnserial = @refDoc1
;
SELECT
@new_serial,
pncodute,
pncodcau,
@docAmount
FROM mytable
WHERE pnserial = @refDoc2
;
ROLLBACK TRANSACTION T1;
I've tried to include the queries inside a single transaction, but I still get the error.
I read several guides about scalar variables and my example doesn't seem to me one of the cases where the scalar variables are out of scope in the second query.
Thank you in advance.