Pages

Friday, October 10, 2014

What is TVP (Table Value Parameter) & How to use it ? TIP #57

 

This is one of the interesting feature which I like most. Instead of passing values from collection one by one pass entire collection to stored procedure as a table value parameter.

I know above statement is not digestive enough so lets understand this by an example.

Suppose , I have a table tblStudent with 4 columns studentId, FirstName, LastName, and class  as shown in below figure

Studenttable

Now I need to insert value in this table so I have created a simple stored procedure as shown in below figure

without_tvpProc

Now , I can easily insert values into it by calling the stored procedure for each student FirstName, LastName, course,

Suppose , I need to insert 4 records in the table then I need to call this stored procedure 4 times as shown in below figure

call_sp_without_TVP

Now think you have .net program and you have to call this stored procedure more than 100 times Smile .

I know you are bit scary.

Not to worry thanks to SQL Server again by providing Table value parameter to handle such kind of situation.

Now let me explain how to create table value parameter and call it to resolve such problem step by step

Step 1:-

To create Table value parameter you have to  choose  user define data type  as shown in below figure

User_Define_Type

Step 2:-

Now you can modify the template script in my case I am creating a student type User defined table type as shown in below figure . You will find I am using all the column which I need to insert into tblStudent table

 

Student_Type_Definie

Step 3:-

Now I am writing a  new stored procedure which use this table type as shown in below figure

StudentType_Procedure

You find in the above snap we have created a procedure with student type parameter which we have created .

The point to remember here that the table value parameter should be READONLY when passes in a stored procedure

Step 5:- Now let see how to call this stored procedure which has student type table value parameter

PassingTVP_Sp_Call

Now when we run above snap statement we inserted 3 rows in tblStudent.

Now lets consider the above problem calling stored procedure 1000 times instead of that you will call stored procedure only once with this amazing Table value parameter feature.

You can pass any collection and data table as a parameter from .NET.

Isn’t it made our life easy ?  I am big fan of this feature.

I hope if you use you will also.

Hope this tip may help you somewhere.

Thanks & Enjoy !!!

RJ !!!

Thursday, October 9, 2014

Good to Know SQL JARGON - An interview TIP #56

 

Hello friends,

Just wanted to share some technical words here again it is good to know

1) OLAP :  On Line Analytical  Processing

2) MOLAP :- Multidimensional  On Line Analytical Processing

3) ROLAP :- Relational On Line Analytical Processing

4) HOLAP :- Hybrid On Line Analytical Processing

5) RTOLAP :- Real Time On Line Analytical Processing

6) DOLAP :- Desktop On Line Analytical Processing

8) SSIS :- SQL Server Integration Services

9) SSAS :- SQL Server Analysis Services

10) SSMS :- SQL  Server Management Studio

11) SSRS :- SQL Server Reporting Services

12)Magic Table:- A temporary table use at the time of  trigger

13)DML :- Data Manipulation Language (Insert/Update/Delete)

14) DDL :- Data Definition Language  (Create /Alter /Drop)

15) DCL :- Data Control Language (Grant /Revoke)

16) TCL:- Transaction Control Language  (Rollback /Commit)

17) ACID :- Atomicity,  Consistency , Isolation ,Durability

18) PK- Primary Key

19) FK – Foreign Key

20) NF:- Normal Form (1NF,2NF,3NF)

21) ODBC :-  Open Database Connectivity

22) OLEDB :- Object Linking  and Embedding Database

23) RDBMS :- Relational Database Management System

24) CRUD:- Create, Read , Update, Delete

25) ETL :- Extract Transform Load

26) BI :- Business Intelligence

27) SP :- Stored Procedure

28) VLDB :- Very Large Data Base

29) CTE:- Common Table Expression

30) CDC :- Change Data Capture

31) TDE :- Transparent Data Encryption

I will add more in future.

Thanks & Enjoy !!!

RJ !!!

Maintenance of fragmented Heap table–TIP #55

 

Friends,

In last post  post #54  we understood what is a Heap table. Now the challenge is what if this table is highly fragmented so.

Now our task is to run maintenance of this highly fragmented table.

So you have to run following command

“ALTER TABLE  TABLENAME REBUILD;

For example  I run the Rebuild for  Adventureworks2012 database’s heap table which we found in post #54.

See below snap for detail

Rebuild_Heap 

So, with this REBUILD command the table is rebuild and the fragmentation issue resolved.

I hope it may help you some where.

Thanks & Enjoy!!!

RJ!!

Wednesday, October 8, 2014

What is Heap table in SQL Server and How to get all the heap table from a database ? TIP # 54

 

Heap table:-

A table without cluster index is called Heap table. Now you are thinking why we are talking about this.

So as  SQL developer we should aware and In next post I will explain which is related to maintenance how to Rebuild a heap table.

Now our next question is how to determine all the heap table from a database

so below is simplest query to determine all the heap tables from a  database

SELECT T.Name 'Heaptable'
FROM sys.indexes I    
    INNER JOIN sys.tables T
        ON I.object_id = T.object_id
WHERE I.type = 0 AND T.type = 'U'

I ran this query on Adventureworks database and 2 tables with heap as shown in below figure

heaptable

now in next post we will discuss how to rebuild fragmented heap table which is a maintenance activity.

I hope this article may help you.

Thanks !!!

RJ!!

Tuesday, October 7, 2014

How Enable /Disable all CONSTRAINT on a table ? tip #53

 

Dear Friends,

Although , When we have designed our database he/she knows what type of constraints is require to make data perfect and the Database designer applies those constraints but it might be possible that sometime we have to disable those constraints make some tweak with the data in the table and then again enable the constraints.

Below are two simple command to Enable & Disable constraints of a table.

a) Disable constraints on a table

ALTER TABLE  tableName NOCHECK CONSTRAINT  ALL

b) Enable Constraints on a table

ALTER TABLE TABLE_NAME CHECK CONSTRAINT ALL

Lets understand this by an example suppose, I have city_seed table which having Foreign key constraint of country_seed table.

Now  when we run disable constraint command as shown above then you will find the constraint is disable  as shown in figure

disable_Constraint

Now when we run enable constraint then see below figure

disable_Constraint

I hope it might helpful you somewhere.

Thanks & Enjoy!!!

RJ!!!

Monday, October 6, 2014

A hidden feature sp_MSforeachdb TIP #52

 

As we discussed yesterday a hidden feature tip #51 which is sp_MSForeachtable . sp_MsForeachtable is useful to run command on each table of selected database. Now what if we want to run command on each database also ? then in that case we need to take help of sp_MsForeachDB.

Sp_MsForEachDB  by the name it is clear that it will run provided command to each database.

The syntax of sp_MsForEachDB  is very simple almost like sp_MsForEachTable.

Lets understand this by an example

Suppose we want name of each database  of our SQL server for this we will run following command

Execute sp_MSforeachdb " SELECT  '?' AS Name"

Now  when we execute it we will get following result as shown in below figure.

sp_MSForEachDB

This two stored procedures are not documented but it help a lot in maintenance task and other tasks.

I will share some maintenance query  with sp_MsForEachDB & sp_MsForEachtable  soon.

I hope this will help you somewhere.

Enjoy !!!

RJ!!!

Sunday, October 5, 2014

A hidden feature sp_MSforeachtable–run on entire tables of a Database TIP# 51

Problem:-

Sometimes it happened that you need to run a single statement on entire tables which exists in database.  so most of the time we think of cursor which run for each sys.objects (table)  and we execute the dynamic statement by replacing table name.

Solution:

Although the solution we are thinking is correct there is no issue at all but SQL SERVER made our life more each by providing “sp_MSForEachTable”  which is a hidden stored procedure.

By the name it is clear that it will run on each table.

Lets understand this by an example. Suppose you want to  row count of each table then you can write following statement.

DECLARE @tblRowCount AS TABLE (Counts  INT,
                               TableName VARCHAR(100))

INSERT INTO @tblRowCount (Counts,TableName)
EXEC sp_MSforeachtable
@command1='SELECT COUNT(1) As counts,''?'' as tableName FROM ?'


SELECT * FROM @TblRowCount ORDER BY Counts desc

Now when we run it we will get row count of each table as shown in below snap

sp_msforeachtable_rowcount

 

A part from it you can use this hidden feature in maintenance also like running re indexing, re organizing entire table index etc.

I hope this may help you some where.

Enjoy!!!

RJ!!