SQL Server

SQL split function

DECLARE @str as varchar(max)
SET @str = ‘Prakash,Sanket,Jigar,JD’
SELECT value as GiveColumName from dbo.split(@str,’,’)

Split function used to insert to multiple records into tables…. see below procedure

INSERT INTO BoqThermoDrawing
SELECT @BoqNo,@RevisionNo,@RootPath+’\’+value from dbo.Split(@FileNames,’/’)

—————————————————————–
Create function in sql server… split after that u can use above function

USE [DataBaseName]
GO
/****** Object: UserDefinedFunction [dbo].[Split] Script Date: 03/04/2010 11:55:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER function [dbo].[Split](
@String nvarchar (4000),
@Delimiter nvarchar (10)
)
returns @ValueTable table ([ID] int,[Value] nvarchar(4000))
begin
declare @NextString nvarchar(4000)
declare @Pos int
declare @NextPos int
declare @CommaCheck nvarchar(1)

–Initialize
set @NextString = ”
set @CommaCheck = right(@String,1)

–Check for trailing Comma, if not exists, INSERT
–if (@CommaCheck @Delimiter )
set @String = @String + @Delimiter

–Get position of first Comma
set @Pos = charindex(@Delimiter,@String)
set @NextPos = 1

–Loop while there is still a comma in the String of levels
Declare @sr as int
set @sr =0
while (@pos 0)
begin
set @NextString = substring(@String,1,@Pos – 1)
set @sr = @sr + 1
insert into @ValueTable ([id], [Value]) Values (@sr , @NextString)

set @String = substring(@String,@pos +1,len(@String))

set @NextPos = @Pos
set @pos = charindex(@Delimiter,@String)
end

return
end

Check IsIdentity true or false

Find SQL Table Cloumn name, data type,  column size

select column_name, data_type, character_maximum_length char from information_schema.columns where table_name=’tblCountry’

Find IsIdentity true columns

SELECT * from information_schema.columns where columnproperty(object_id(table_name),column_name, ‘IsIdentity’)=1
and table_name=’personal’

Find IsIdentity False Columns

SELECT * from information_schema.columns where columnproperty(object_id(table_name),column_name, ‘IsIdentity’)=0
and table_name=’personal’

Joins in SQL Server

Inner Joins

It match records together based on one or more common fields, as do most JOINS but inner joins return only records where there are matches for whatever fields you have said are to be used for join.

Join Two Tables

Table_1 : Product_Master

product_master

Table_2 : Sales_Order_Detaisl

sales_order_details

Query:

select a.product_no,a.decscription from product_master a
inner join sales_order_details b on a.product_no = b.product_no

Multiple Inner Joins

select a.product_no,a.decscription,d.name from product_master a
inner join sales_order_details b on a.product_no = b.product_no
inner join sales_order c on b.s_order_no = c.s_order_no
inner join client_master d on c.client_no = d.client_no
where d.name=’Ivan Bayross’

Outer Joins

Syntax: Select * from left_table_name <LEFT or RIGHT>  OUTER  JOIN right_table_name on <condition>

Example

select a.product_no from product_master a
left outer join sales_order_details b on
a.product_no = b.product_no

It will display all records of left, it does not important to find matches with right table. And it does not display right table records which are not match found

CROSS Join

A cross join is differ from other joins in that there is no ON operator and that it joins every record on one side of the JOIN with every record on the other side of the JOIN.

Syntax : Select * from left_table_name cross join right_table_name

It will display possibility of matching records in both tables. Suppose LEFT table have 3 records and right table 5 then Cross join display 3*5=15 records, match possibilities.

Find Tables List Structure Count Duplicate Records

use <database_name>

Select DataBase using above query

select * from information_schema.tables

You can easily fine tables list using above syntax.

sp_help <table_name>

See particular table structure/Design.

select s_order_no, count(s_order_no) As ‘SalesOrder’ from sales_order_details group by s_order_no having (count(s_order_no)>1)

Find Duplicate Records In Table