title: SQL Assign value to local variable take from table
body: <p>This i my procedure,that determines classId and liveareaId for Animal to insert it into table</p>
  
  <pre><code>CREATE PROCEDURE insertAnimal
  @name nvarchar,
  @birthyear int,
  @classname nvarchar,
  @livearea nvarchar
  AS
  BEGIN
  DECLARE @classid int
  DECLARE @liveareaid int
  
  SET @classid =  
  
  (SELECT Id
   FROM dbo.Class 
   WHERE dbo.Class.Name = @classname)
  
  SET @liveareaid =
  (SELECT Id
   FROM dbo.LiveArea 
   WHERE Name = @livearea)
  
  INSERT INTO dbo.Animal (Name,BirthYear,ClassId,LiveAreaId) VALUES
  (
      @name,
      @birthyear,
      @classid,
      @liveareaid
  )
  END
  GO
  </code></pre>
  
  <p>I have a error:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'ClassId', table 'ZOO.dbo.Animal'; column does not allow nulls. INSERT fails.</p>
  </blockquote>
  
  <p>Why <code>ClassId</code> is null, can you tell me why whis doesn't work.</p>
  
  <pre><code>SET @classid =  
  
  (SELECT Id
   FROM dbo.Class 
   WHERE dbo.Class.Name=@classname)
  </code></pre>