Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please I want to understand this SQL code in detail, and how it works?

SQL
CREATE PROC AddEmp(@mgrid int, @empid int, @e_name varchar(20), @title varchar(20)) 
AS 
BEGIN
   DECLARE @mOrgNode hierarchyid, @lc hierarchyid
   SELECT @mOrgNode = OrgNode 
   FROM HumanResources.EmployeeOrg 
   WHERE EmployeeID = @mgrid
   SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
   BEGIN TRANSACTION
      SELECT @lc = max(OrgNode) 
      FROM HumanResources.EmployeeOrg 
      WHERE OrgNode.GetAncestor(1) =@mOrgNode ;

      INSERT HumanResources.EmployeeOrg (OrgNode, EmployeeID, EmpName, Title)
      VALUES(@mOrgNode.GetDescendant(@lc, NULL), @empid, @e_name, @title)
   COMMIT
END ;
GO

What does DECLARE keyword mean, and at sign (@),
I know it's related to the procedure's parameter's but it is not clear in this code to me.

this code from the example here:
https://msdn.microsoft.com/en-us/library/2c95fa60-5b8e-4a05-ac09-cffe2b05900a(v=sql.110)[^]
Posted
Updated 16-Mar-15 10:51am
v3

1 solution

DECLARE with the @ sign is declaring a variable.

Also, perhaps you left a lot out because this code starts a transaction but then only does one thing in it, which is a waste of transaction.

Overall, it just selects some values and creates a record in the EmployeeOrg table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900