Search This Blog

Friday 29 January 2016

Can we call stored procedure in trigger in sql server?

Question: Can we call stored procedure in trigger in sql server?
Answer  : Yes

Question: Can we call trigger inside stored procedure in sql server?
Answer  : No, it is automatically executed when we perform DML operation on table.

Let’s explore it.

Step 1: Create table

CREATE TABLE TABLE1
(
       ID INT IDENTITY,
       NAME VARCHAR(50)
)

CREATE TABLE TABLE2
(
       ID INT IDENTITY,
       NAME VARCHAR(50)
)

Step 2: Create insert procedure

CREATE PROC spINSERT
@NAME VARCHAR(50)
AS
BEGIN
       INSERT INTO TABLE2(NAME) VALUES(@NAME)
END

Step 3: Create update procedure

CREATE PROC spUPDATE
@ID INT,
@NAME VARCHAR(50)
AS
BEGIN
       UPDATE TABLE2
       SET  NAME = @NAME
       WHERE ID = @ID
END

Step 4: Create delete procedure

CREATE PROC spDELETE
@ID INT
AS
BEGIN
       DELETE FROM TABLE2
       WHERE ID = @ID
END

Step 5: Create insert trigger

CREATE TRIGGER TRG_INSERT
ON TABLE1 FOR INSERT
AS
BEGIN
       -- Using Query
       --INSERT INTO TABLE2(NAME) SELECT NAME FROM INSERTED

       -- Using Store procedure
       DECLARE @NAME VARCHAR(50)

       SELECT @NAME = NAME FROM INSERTED 

       EXEC spINSERT @NAME
END

Step 6: Create update trigger

ALTER TRIGGER TRG_UPDATE
ON TABLE1 FOR UPDATE
AS
BEGIN
       DECLARE @ID VARCHAR(50)
       DECLARE @NAME VARCHAR(50)

       SELECT @ID = ID, @NAME = NAME FROM INSERTED 

       -- Using Query
       --UPDATE TABLE2 SET NAME = @NAME WHERE ID = @ID

       -- Using Store procedure
       EXEC spUPDATE @ID, @NAME
END

Step 7: Create delete trigger

ALTER TRIGGER TRG_DELETE
ON TABLE1 FOR DELETE
AS
BEGIN
       DECLARE @ID VARCHAR(50)

       SELECT @ID = ID FROM DELETED 

       -- Using Query
       --DELETE FROM TABLE2 WHERE ID = @ID

       -- Using Store procedure
       EXEC spDELETE @ID
END

Step 8: insert record in table1

INSERT INTO TABLE1(NAME)VALUES('A')
GO 2

Step 9: Select the both table record, you will see 2 rows added in both the table, in table2 record added by trigger fired on table1.  

SELECT * FROM TABLE1

ID          NAME   
----------- --------
1           A
2           A

(2 row(s) affected)

SELECT * FROM TABLE2


ID          NAME
----------- --------
1           A
2           A

(2 row(s) affected)

Step 10: update the record of table1, whose id is 2, in table2 record updated by trigger fired on table1.  

UPDATE TABLE1 SET NAME = 'B' WHERE ID = 2

Step 11: Select the both table record, you will see updated rows in both the table

SELECT * FROM TABLE1

ID          NAME   
----------- --------
1           A
2           B

(2 row(s) affected)

SELECT * FROM TABLE2

ID          NAME
----------- --------
1           A
2           B

(2 row(s) affected)

Step 12: delete the record of table1, whose id is 2, in table2 record deleted by trigger fired on table1.  
  
DELETE FROM TABLE1 WHERE ID = 2

Step 13: Select the both table record

SELECT * FROM TABLE1

ID          NAME   
----------- --------
1           A

(1 row(s) affected)

SELECT * FROM TABLE2

ID          NAME
----------- --------
1           A

(1 row(s) affected)

DROP TABLE TABLE1
DROP TABLE TABLE2

Note:

I have commented the query line in all trigger, you simply uncomment the line and comment the store procedure execution line.

If both line are open in trigger the query fire twice.

For more information visit following link

Friday 22 January 2016

PRIMARY KEY Vs UNIQUE constraints in sql server

PRIMARY KEY

  1. Primary key on table columns does not accept NULL values.
  2. Only one Primary key allows on table columns.

UNIQUE

  1. Unique constraints on table columns does accept at least one NULL value.
  2. Unique constraints can create more than one on table columns.

Clustered and nonclustered index in sql server

Indexes are automatically created when PRIMARY KEY and UNIQUE constraints are defined on table columns.

PRIMARY KEY constraints
  1. When you create the PRIMARY KEY constraints on table column, the Database Engine automatically creates clustered index on that column.
  2. You cannot create more than one clustered index on table columns.


UNIQUE KEY constraints
  1. When you create the UNIQUE KEY constraints on table columns, the Database Engine automatically creates nonclustered index on that column.
  2. You can create more than one nonclustered index on table columns.


Note:
You can create the CLUSTERED and NONCLUSTERED index without PRIMARY KEY and UNIQUE constraints on table columns.

Let's explore it


PRIMARY KEY constraints


Step 1:- Create table without any constraints

CREATE TABLE #TestTable
(
    Col1 INT NOT NULL,
    Col2 VARCHAR(10) NULL,
    Col3 VARCHAR(50) NULL
)

Step 2:- Alter table and PRIMARY KEY constraints

ALTER TABLE #TestTable ADD CONSTRAINT PK_COL1 PRIMARY KEY(Col1)

Step 3:- Create the CLUSTERED index on table columns

CREATE CLUSTERED INDEX INDX_TestTable_Col1
ON #TestTable (Col1);

You will get the following error

Msg 1902, Level 16, State 3, Line 48
Cannot create more than one clustered index on table '#TestTable'. Drop the existing clustered index 'PK_COL1' before creating another.

Conclusion

When you created the PRIMARY KEY constraints on table column, the Database Engine automatically created the clustered index on that column.

Step 4:- Drop the PRIMARY KEY constraints

ALTER TABLE #TestTable DROP CONSTRAINT PK_COL1

Step 5:- Create the CLUSTERED index on table columns

CREATE CLUSTERED INDEX INDX_TestTable_Col1
ON #TestTable (Col1);

Conclusion

You noted that you will create the CLUSTERED INDEX without PRIMARY KEY constraints on table columns

Step 6:- Finally DROP the table

DROP TABLE #TestTable


UNIQUE constraints


Step 1:- Create table without any constraints

CREATE TABLE #TestTable
(
    Col1 INT NOT NULL,
    Col2 VARCHAR(10) NULL,
    Col3 VARCHAR(50) NULL
)

Step 2:- Alter table and UNIQUE constraints

ALTER TABLE #TestTable ADD CONSTRAINT UK_COL1 UNIQUE(Col1)

Step 3:- Create the NOTCLUSTERED index on table columns

CREATE NONCLUSTERED INDEX INDX_TestTable_Col1
ON #TestTable (Col1);

CREATE NONCLUSTERED INDEX INDX_TestTable_Col2
ON #TestTable (Col2);

Conclusion

You can create more than one nonclustered index on table columns.

Step 4:- Drop the UNIQUE constraints & INDEX

ALTER TABLE #TestTable DROP CONSTRAINT UK_COL1

DROP INDEX INDX_TestTable_Col1 ON #TestTable
DROP INDEX INDX_TestTable_Col2 ON #TestTable

Step 5:- Create the NONCLUSTERED index on table columns

CREATE NONCLUSTERED INDEX INDX_TestTable_Col1
ON #TestTable (Col1);

Conclusion
You noted that you will create the NONCLUSTERED INDEX without UNIQUE constraints on table columns

Step 6:- Finally DROP the table


DROP TABLE #TestTable