I am trying to conditionally update a table using a user defined table type variable passed to a stored procedure. When I say, if the UDTT variable is not null, then do something with the UDTT, I get an error message. However, I get the scalar not declared error, even though I can reference this variable in a SELECT statement.
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[myProc] @table [myUDTT] NULL READONLY AS BEGIN IF @table IS NOT NULL BEGIN SELECT * FROM @table END END GO
The reference to @table after “IF” gives the error, however the reference after SELECT does not.
I’ve tried:
Making the reference as simple as possible.
Changing the @table variable to an INT and the error message was no longer displayed:
CREATE PROCEDURE [dbo].[fakeProc] @table int NULL AS BEGIN IF @table IS NOT NULL BEGIN SELECT @table END END GO
-> no error
The result I would like is that if the variable is null, the select statement is skipped.
Anya_Hayes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.