Why did the SQL error, when I forget the @ variable on the variable, pass the procedure parameter?

I had a typo of the absence of @ due to using a variable. Normally, SQL will compile an undeclared variable. However, when it is used, the sproc sqr server server does not complain. He just transfers some singles and moves on.

Can someone explain why example C does not cause an error?

--Ex A. Error
SELECT id

--Ex B. Error
CREATE FUNCTION dbo.fnTest (
  @id NVARCHAR(4)
) RETURNS NVARCHAR(4) AS BEGIN 
  RETURN @id 
END
GO
DECLARE @id NVARCHAR(4) 
SET @id= 'bob'
SELECT dbo.fnTest(@id) --missing my @, this should be @id
--SELECT dbo.fnTest(id) --missing my @, this should be @id
GO
DROP FUNCTION fnTest

--Ex C. No Error
CREATE PROC spTest (
  @id NVARCHAR(4)
) AS 
  SELECT @id
GO
DECLARE @id NVARCHAR(4) 
SET @id= 'bob'
EXEC spTest id --missing my @, this should be @id
GO
DROP PROC spTest
+4
source share
2 answers

For the same reason, this always worked:

EXEC sp_who2 active;

Compared to expected:

EXEC sp_who2 N'active';

Stored procedures can take an input string without single quotes and still treat it as a string if it does not have special characters, such as spaces or dashes.

, ?

, , , SQL Server (, , Sybase) . , " , ". , , , . (, .)


PS , ,

EXEC dbo.spTest @id = @id;

, (, - ).

+7

, TSQL .

, SQL Engine, .

enter image description here

id . , . , EXEC , .

Basically late binding.

SELECT . , SELECT. , , @, . , . , .

Basically early binding.

enter image description here

+3

All Articles