Tuesday, 21 April 2020

Recursive or self-referencing Foreign Key in sql server


The recursive foreign key is a foreign key that refers back to the primary key in the same table where a child and parent table are the same. When we reference the same table at the same time of pulling data.
For example, in an employee table, we have employees and managers. Managers are employee.
Let’s see the example.

I am creating a table.
CREATE TABLE tblEmp
 (   EmpID INT PRIMARY KEY NOT NULL
                ,EmpName VARCHAR(25)
                ,Title VARCHAR(25)
                ,ManagerID INT
                ,HireDate DATETIME
                ,DepartmentID INT        
                CONSTRAINT FK_Emp_Manager FOREIGN KEY (ManagerID) REFERENCES tblEmp(EmpID)
)

In this table, we have created foreign key on the ManagerID column because Managers are also an employee.
Now I am inserting some records.
INSERT INTO tblEmp VALUES  (1, 'Bagesh', 'President', NULL, '05/10/2015',10);
INSERT INTO tblEmp VALUES(2, 'Kumar', 'IT Manager', 1, '05/01/2016',20);
INSERT INTO tblEmp VALUES(3, 'Bagi', 'Finance Manager', 2, '12/01/2015',  30);
INSERT INTO tblEmp VALUES(4, 'Rajesh', 'Sales Manager', 2, '07/08/2015',  40);
INSERT INTO tblEmp VALUES(5, 'Mahesh', 'BI Developer', 3, '09/02/2017',  20);
INSERT INTO tblEmp VALUES(6, 'Ganesh', 'Data Analyst', 3, '02/25/2016',  20);
INSERT INTO tblEmp VALUES(7, 'Raju', 'Accountant', 4, '02/01/2015',  30);
INSERT INTO tblEmp VALUES(8, 'Ravi', 'Accounting Assistant', 4, '09/28/2016', 30);
INSERT INTO tblEmp VALUES(9, 'Amit', 'Salesman', 5, '05/03/2015',  40);
INSERT INTO tblEmp VALUES(10, 'Gopal', 'Salesman', 5, '06/03/2015', 40);
Records in the table.
       


Let’s see how we will get the employee ID with their manager's name.
SELECT e.EmpID
 ,e.EmpName AS Employee
 ,e.Title
 ,e.ManagerID
 ,m.EmpName AS Manager
FROM tblEmp AS e
 LEFT JOIN tblEmp AS m
ON e.ManagerID = m.EmpID
ORDER BY e.EmpID
      

Now we are inserting an employee and we assign a manager id which is not an employee, in this case, we will get below error.
                         
 


SET ROWCOUNT in SQL Server


The SQL ROWCOUNT is one of the Set Function, which causes the SQL server to stop the query processing after the specified numbers returned.

Syntax

SET ROWCOUNT number (to stop the query)
   


Basically, if we want to get the top n records from the script in this case either we write top clause or we can set this.
For the above, we need to write the below query to get the top 2 records from the table.
   


Keep in mind when we are setting SET ROWCOUNT, it works on the current session window only if we will open other window script works as usual.

Sp_procoption in sql server


SQL Server offers the system stored procedure sp_procoption which can be used to designate one or more stored procedures to automatically execute when the SQL Server service is started.

exec sp_procoption @ProcName = ['stored procedure name'],
@OptionName = 'STARTUP',
@OptionValue = [on|off]

Parameter @ProcName is self-explanatory; it's the name of the procedure marked for auto-execution
Parameter @OptionName is the option to use. The only valid option is STARTUP
Parameter @OptionValue toggles the auto-execution on and off

Using sp_procoption comes with certain restrictions:

Ø  We must be logged in as a sysadmin to use sp_procoption
Ø  We can only designate standard stored procedures, extended stored procedures, or CLR stored procedures for startup
Ø  The stored procedure must be located in the master database
Ø  The stored procedure must not require any input parameters or return any output parameters

SET NOCOUNT ON | OFF Statement in sql server


Stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set. When SET NOCOUNT is ON, the count is not returned. When SET NOCOUNT is OFF, the count is returned.
Set’s see in this demo.
SET NOCOUNT OFF
This shows the number of affected rows in a message window.
    

SET NOCOUNT ON
This prevents the message from showing which contains the number of affected rows.
   


SET NOCOUNT and @@ROWCOUNT function
We can use the @@ROWCOUNT function to get the number of affected rows in SQL Server. The NOCOUNT ON function does not have any impact on the @@ROWCOUNT function.

Execute the following query, and we get the number of rows affected with the Insert statement.
   

Thursday, 2 April 2020

Annotations for Precedence Constraints (ShowAnnotation property in Precedence Constraints) in SSIS


Annotations in SSIS are a great tool to add notes, comments or other descriptive information to a package. Generally, we are adding this to the control flow, data flow, and event handlers. With the help of ShowAnnotation property in Precedence Constraints we can also add to our precedence constraints as well.


Below the given example, we have three expressions on Precedence Constraints. Seeing these we will not know the expression. Each precedence constraint references the value of a different variable in order for it to be allowed to proceed. It is a bit difficult to know the expression for all precedence constraints. It's a bit easier to see what the package is doing at a glance if the precedence constraint annotations are displayed.
   
 

For the displaying the precedence constraint annotations we need to set the ShowAnnotation properties on the precedence constraints.
Let’s see who we will set this.
   

ShowAnnotation properties we will get below options
Ø  AsNeeded
Ø  ConstraintName
Ø  ConstraintDescription
Ø  ConstraintOptions
Ø  Never
By default, the "ShowAnnotation" property on a precedence constraint is set to "AsNeeded". The canvas does show the Fx symbol which means there's an expression placed on the precedence constraint.
ConstraintName
If we select this property the constraint name will be show.
   

By default constrain name will be like constrain n. we can change it.
ConstraintDescription
If we select this property the constraint Description will be shown.
  

By default description is blank.
ConstraintOptions
If we select this property the constraint expression will be shown.
  


Never

It will disable annotations.

  

Hope this will help to package design and documentation.


Popular Posts