Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Sunday, May 8, 2016

Difference between temp Table(#Table) and Table variable(@Table) in sql server





Json String in Sql Server

In this post i will show how to display/Convert Table Rows to JSON String.

Continuing the previous Post Create Temp Table in Sql Server

OutPut:

{
                "Data": [{
                                "id": 1,
                                "name": "Kiran",
                                "active": 1
                }, {
                                "id": 2,
                                "name": "Ravi",
                                "active": 1
                }]

}

Create Temp Table in Sql Server or Create Temp Table Variable in Sql Server

In this post I will show how to create Temp Table(variable) using @ symbol.

Previously I had shown

OutPut:

ID
Name
IsActive
1
Kiran
1
2
Ravi
1
3
Vara
1
4
Dinesh
1
5
Deleep
0
6
Phani
1
7
Shiva
1
8
shyam
0



Friday, April 8, 2016

Create Relationships Between Tables OR Create Foreign Key Relationships

Here I am going to show, how to create multiple tables with relations between tables (Country, State and City)

Method 1: New Tables


create table Country
(
       ID int identity(1,1) primary key,
       Name nvarchar(500) Not NULL,
       ShortName nvarchar(15)
)

create table State
(
       ID int identity(1,1) primary key,
       StateName nvarchar(500) Not NULL,
       CountryID int,
       foreign key (CountryID) references Country(ID)
)

create table City
(
       ID int identity(1,1) primary key,
       CityName nvarchar(500) Not NULL,
       StateID int,
       foreign key (StateID) references State(ID)
)

Method 2: Existing Tables

Thursday, January 31, 2013

Friday, November 16, 2012

Create Temporary Table(#Table) in Stored Procedure SQL Server 2005/2008

Here i will show how to create temporary table in Stored Procedure sql server 2005 / 2008

Syntax :



Create table #TableName
(
      --Colun Names
)

Here # is a must when creating Temporary table


EX : 

create proc SP_SPName
(
     // Parameters
)
as
begin

   create table #TempTable

   (
         ID int identity(1,1),
         Name varchar(250),
         Address varchar(500)
   )

 drop #TempTable

End

#TempTable should be dropped else we will get ERROR while executing SP 2nd time..

Function that Splits string in SQL Server / Table Valued Function

In this post i will show how to split a String at certain character and shows the OutPut in as a table.
This is also known as Table Valued Function.

OutPut :

































Wednesday, November 7, 2012

Tool that generates StoredProcedure and C# methods for CRUD operations

This is the tool that generates SP and C# methods for CRUD operations. It is mainly used when there are lots of parameters are there. If parameters are more then it takes more time to create a SP for Insert, Update and Select. To make it simple with few clicks we can generate the code we need.
This may help some one......
This TOOL was Updated U can Download....

































Friday, August 31, 2012

How to use While Loop In StoredProcedures

Here I will explain how to use while loop in Stored Procedures. Mostly we will use while loop in stored procedures.
Here is the sample stored procedure to print 1 to 5 using while loop.

Ex : 1

create proc whileLoop
as
begin

declare @I int = 1, @J int = 5 -- Declare i, J and assign values

while(@I <= @J)
begin

print @I

set @I = @I + 1;

end

end

and now to execute the above stored procedure write the below line and select if the line is in the sample script file and press F5


exec whileLoop


Out Put :

1
2
3
4
5