Hi,
This is the example for calculating Query execution time in SQL
The procedure is very simple, first take start time of query execution and then write the query and then get the end time of query execution and then calculate their difference
And below is the execution plan for the query
For execution plan you have to just run the query and click on the icon menu i.e. Display Estimated Execution Plan
Happy Coding,
Regards,
Krunal Panchal
Thursday, November 25, 2010
Monday, November 22, 2010
SQL SERVER:Generating CSV in select command and again Generating Row wise data
Hi everyone,
Declare @chapters varchar(max)
select @chapters=coalesce(@chapters+',','')+ch.Chapter
from Chapter ch,Subject sb ,Syllabus s,Classroom c
--for syllabus
where s.SyllabusID ='3'
--for classroom
and c.SyllabusID =s.SyllabusID
and c.Classroom =1
--for subject
and sb.SyllabusID =s.SyllabusID
and sb.ClassroomID =c.ClassroomID
--and sb.Subject ='Eng.Lit.'
--for chapters
and ch.SyllabusID =s.SyllabusID
and ch.ClassroomID =c.ClassroomID
and ch.SubjectID =sb.SubjectID
select @chapters
Select * from UF_CSVToTable (@chapters)
Function UF_CSVToTable
USE [CMS2012]
GO
/****** Object: UserDefinedFunction [dbo].[UF_CSVToTable] Script Date: 11/22/2010 17:15:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[UF_CSVToTable]
(
@psCSString VARCHAR(max)
)
RETURNS @otTemp TABLE(sID VARCHAR(2000))
AS
BEGIN
DECLARE @sTemp VARCHAR(max)
WHILE LEN(@psCSString) > 0
BEGIN
SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
LEN(@psCSString)))
SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
LEN(@psCSString)) + 1, LEN(@psCSString))
INSERT INTO @otTemp VALUES (@sTemp)
END
RETURN
END
Happy Coding,
Regards,
Krunal Panchal
Declare @chapters varchar(max)
select @chapters=coalesce(@chapters+',','')+ch.Chapter
from Chapter ch,Subject sb ,Syllabus s,Classroom c
--for syllabus
where s.SyllabusID ='3'
--for classroom
and c.SyllabusID =s.SyllabusID
and c.Classroom =1
--for subject
and sb.SyllabusID =s.SyllabusID
and sb.ClassroomID =c.ClassroomID
--and sb.Subject ='Eng.Lit.'
--for chapters
and ch.SyllabusID =s.SyllabusID
and ch.ClassroomID =c.ClassroomID
and ch.SubjectID =sb.SubjectID
select @chapters
Select * from UF_CSVToTable (@chapters)
Function UF_CSVToTable
USE [CMS2012]
GO
/****** Object: UserDefinedFunction [dbo].[UF_CSVToTable] Script Date: 11/22/2010 17:15:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[UF_CSVToTable]
(
@psCSString VARCHAR(max)
)
RETURNS @otTemp TABLE(sID VARCHAR(2000))
AS
BEGIN
DECLARE @sTemp VARCHAR(max)
WHILE LEN(@psCSString) > 0
BEGIN
SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
LEN(@psCSString)))
SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
LEN(@psCSString)) + 1, LEN(@psCSString))
INSERT INTO @otTemp VALUES (@sTemp)
END
RETURN
END
OutPut:
Happy Coding,
Regards,
Krunal Panchal
SQL SERVER: Generate Comma Separated List with SELECT statement
Hi everyone,
Many times i require the data from select command in comma seperated format
for e.g.
SELECT [AdminId]
,[LoginId]
,[Password]
,[AdminType]
,[TaskId]
,[CreatedBy]
,[Status]
FROM [CMS2012].[dbo].[Admin_Details]
where AdminId in(3,4,5,6)
will give me result as...
But what i need is...
Login id:-
Mexus,Vibhorthegame@yahoo.co.in,qwe,123@mexuseducation.com
Password:-
Mexus,123123,qwe,123123
etc.....
so i wrote this SQL select statement...
Declare @Description varchar(4000)
Declare @password varchar(max)
select @Description = coalesce(@Description + ',','') + LoginId ,
@password =coalesce(@password+',','')+Password
FROM Admin_Details where AdminId in (3,4,5,6)
SELECT
@Description ,@password
And what i got is...
Happy Coding,
Regards,
Krunal Panchal
Many times i require the data from select command in comma seperated format
for e.g.
SELECT [AdminId]
,[LoginId]
,[Password]
,[AdminType]
,[TaskId]
,[CreatedBy]
,[Status]
FROM [CMS2012].[dbo].[Admin_Details]
where AdminId in(3,4,5,6)
will give me result as...
But what i need is...
Login id:-
Mexus,Vibhorthegame@yahoo.co.in,qwe,123@mexuseducation.com
Password:-
Mexus,123123,qwe,123123
etc.....
so i wrote this SQL select statement...
Declare @Description varchar(4000)
Declare @password varchar(max)
select @Description = coalesce(@Description + ',','') + LoginId ,
@password =coalesce(@password+',','')+Password
FROM Admin_Details where AdminId in (3,4,5,6)
SELECT
@Description ,@password
And what i got is...
Happy Coding,
Regards,
Krunal Panchal
Sunday, November 21, 2010
Order By with VARCHAR
Hi everyone,
In order by clause of varchar(50) column for numeric values,the result is different from order by with integer column..
E.g. select * from classroom order by classroom (where classroom is varchar(max) column)
the result is:
1
10
11
12
2
3
........
now suppose i need it order by like 1 2 3 4 5 ... i need to convert the column to integer , instead of changing the design of table you could write your query as this...
select * from classroom order by cast(classroom as integer)
would give you result as 1 2 3 4 5........
Happy Coding,
Regards,
Krunal Panchal
In order by clause of varchar(50) column for numeric values,the result is different from order by with integer column..
E.g. select * from classroom order by classroom (where classroom is varchar(max) column)
the result is:
1
10
11
12
2
3
........
now suppose i need it order by like 1 2 3 4 5 ... i need to convert the column to integer , instead of changing the design of table you could write your query as this...
select * from classroom order by cast(classroom as integer)
would give you result as 1 2 3 4 5........
Happy Coding,
Regards,
Krunal Panchal
How to get Id of Just Inserted Value in SQL
Hi everyone,
This is a Simple example of Getting Unique Id (Auto Id) for the inserted value after insertion of values in SQL table
Declare @subjectid INT =NULL out
-----Insertion Process-----------
Insert into Subject(SyllabusID,ClassroomID,Subject)
values(@syllabusid ,@classroomid ,@strsubject )
------Get Id into @subjectid----
set @subjectid =SCOPE_IDENTITY ();
Happy Coding,
Regards,
Krunal Panchal
This is a Simple example of Getting Unique Id (Auto Id) for the inserted value after insertion of values in SQL table
Declare @subjectid INT =NULL out
-----Insertion Process-----------
Insert into Subject(SyllabusID,ClassroomID,Subject)
values(@syllabusid ,@classroomid ,@strsubject )
------Get Id into @subjectid----
set @subjectid =SCOPE_IDENTITY ();
Happy Coding,
Regards,
Krunal Panchal
Stored Procedure For Inserting Values in Table In Multiple Column using CSV and getting their AutoId
Hi everyone,
I got indulged into one situation where i have to insert values in SQL table using CSV (Comma Seperated Values) , now the situation became more critical where i need to insert values at a particular ID for a particular table.
i.e. i am having 2 tables namely:classroom, subject, syllabus
now i have data like 4,5,6 to be checked if present and if they are not present then they should be inserted for particular syllabus-id, now by getting the classroom-id for particular syllabus-id i have to insert subject eg. English, Maths for that particular classroom-id and syllabus-id .
I made the Stored Procedure for simple Insertion.......
But the problem was that after inserting the subject the parameter of subject became null OR '' so i stored it in one variable before inserting , and set the subject parameter again to the stored one.......
This whole process also could be done in asp.net with several checks and for loops , but i thought that if it would be done in asp.net then there would be much load to the server. Then i thought of making Stored Procedure for this whole process and here it is........
Stored Procedure......
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Krunal N. Panchal
-- Create date: Monday,November ,22,2010
-- Description: This Stored Procedure is for School Merge [For Insertion of Classroom And Subject By CSV]
-- =============================================
alter PROCEDURE [dbo].[Insert_Classroom_Subject]
--Parameters of Stored Procedure--
@classroom varchar(max),
@subject varchar(max),
@syllabusid varchar(max),
@classroomid varchar(max) =null out,
@subjectid varchar(max)=NULL out
AS
DECLARE @countclassroom int
DECLARE @strclassroom VARCHAR(max)
DECLARE @spotclassroom SMALLINT
DECLARE @storedsubject varchar(max)
SET @storedsubject =@subject
--------------for classroom insertion--------------
WHILE @classroom <> ''
BEGIN
SET @subject =@storedsubject
SET @spotclassroom = CHARINDEX(',', @classroom)
IF @spotclassroom>0
BEGIN
SET @strclassroom = CAST(LEFT(@classroom, @spotclassroom-1) AS varchar(max))
SET @classroom = RIGHT(@classroom, LEN(@classroom)-@spotclassroom)
END
ELSE
BEGIN
SET @strclassroom = CAST(@classroom AS varchar(max))
SET @classroom = ''
END
--Check Classroom if Present---
SELECT @countclassroom=count(1) FROM classroom
WHERE Classroom =@strclassroom
and SyllabusID =@syllabusid
----if Classroom not present then insert it----
IF @countclassroom =0
BEGIN
Insert into Classroom(Classroom ,SyllabusID )
values (@strclassroom ,@syllabusid )
set @classroomid=SCOPE_IDENTITY ();
END
Else
-----else get the id of the classroom for selected subject----
Begin
set @classroomid=(select classroomid from Classroom WHERE Classroom =@strclassroom
and SyllabusID =@syllabusid )
End
-------------------End of classroom insertion---------------------
-------------------For Subject Insertion-----------------------------
DECLARE @countsubject int
DECLARE @strsubject VARCHAR(max)
DECLARE @spotsubject SMALLINT
----------------For Storing the subject-------------
--SET @storedsubject =@subject
WHILE @subject <> ''
BEGIN
SET @spotsubject = CHARINDEX(',', @subject)
IF @spotsubject>0
BEGIN
SET @strsubject = CAST(LEFT(@subject, @spotsubject-1) AS varchar(max))
SET @subject = RIGHT(@subject, LEN(@subject)-@spotsubject)
END
ELSE
BEGIN
SET @strsubject = CAST(@subject AS varchar(max))
SET @subject = ''
END
-----------check if subject is present for particular classroom and syllabus
SELECT @countsubject=count(1) FROM subject
WHERE ClassroomID =@classroomid
and SyllabusID =@syllabusid
and Subject =@strsubject
-----count is 0 then insert the subject------
IF @countsubject =0
Begin
Insert into Subject(SyllabusID,ClassroomID,Subject)
values(@syllabusid ,@classroomid ,@strsubject )
set @subjectid =SCOPE_IDENTITY ();
End
End
---------------------Setting value of subject-----------
End
GO
I got indulged into one situation where i have to insert values in SQL table using CSV (Comma Seperated Values) , now the situation became more critical where i need to insert values at a particular ID for a particular table.
i.e. i am having 2 tables namely:classroom, subject, syllabus
now i have data like 4,5,6 to be checked if present and if they are not present then they should be inserted for particular syllabus-id, now by getting the classroom-id for particular syllabus-id i have to insert subject eg. English, Maths for that particular classroom-id and syllabus-id .
I made the Stored Procedure for simple Insertion.......
But the problem was that after inserting the subject the parameter of subject became null OR '' so i stored it in one variable before inserting , and set the subject parameter again to the stored one.......
This whole process also could be done in asp.net with several checks and for loops , but i thought that if it would be done in asp.net then there would be much load to the server. Then i thought of making Stored Procedure for this whole process and here it is........
Stored Procedure......
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Krunal N. Panchal
-- Create date: Monday,November ,22,2010
-- Description: This Stored Procedure is for School Merge [For Insertion of Classroom And Subject By CSV]
-- =============================================
alter PROCEDURE [dbo].[Insert_Classroom_Subject]
--Parameters of Stored Procedure--
@classroom varchar(max),
@subject varchar(max),
@syllabusid varchar(max),
@classroomid varchar(max) =null out,
@subjectid varchar(max)=NULL out
AS
DECLARE @countclassroom int
DECLARE @strclassroom VARCHAR(max)
DECLARE @spotclassroom SMALLINT
DECLARE @storedsubject varchar(max)
SET @storedsubject =@subject
--------------for classroom insertion--------------
WHILE @classroom <> ''
BEGIN
SET @subject =@storedsubject
SET @spotclassroom = CHARINDEX(',', @classroom)
IF @spotclassroom>0
BEGIN
SET @strclassroom = CAST(LEFT(@classroom, @spotclassroom-1) AS varchar(max))
SET @classroom = RIGHT(@classroom, LEN(@classroom)-@spotclassroom)
END
ELSE
BEGIN
SET @strclassroom = CAST(@classroom AS varchar(max))
SET @classroom = ''
END
--Check Classroom if Present---
SELECT @countclassroom=count(1) FROM classroom
WHERE Classroom =@strclassroom
and SyllabusID =@syllabusid
----if Classroom not present then insert it----
IF @countclassroom =0
BEGIN
Insert into Classroom(Classroom ,SyllabusID )
values (@strclassroom ,@syllabusid )
set @classroomid=SCOPE_IDENTITY ();
END
Else
-----else get the id of the classroom for selected subject----
Begin
set @classroomid=(select classroomid from Classroom WHERE Classroom =@strclassroom
and SyllabusID =@syllabusid )
End
-------------------End of classroom insertion---------------------
-------------------For Subject Insertion-----------------------------
DECLARE @countsubject int
DECLARE @strsubject VARCHAR(max)
DECLARE @spotsubject SMALLINT
----------------For Storing the subject-------------
--SET @storedsubject =@subject
WHILE @subject <> ''
BEGIN
SET @spotsubject = CHARINDEX(',', @subject)
IF @spotsubject>0
BEGIN
SET @strsubject = CAST(LEFT(@subject, @spotsubject-1) AS varchar(max))
SET @subject = RIGHT(@subject, LEN(@subject)-@spotsubject)
END
ELSE
BEGIN
SET @strsubject = CAST(@subject AS varchar(max))
SET @subject = ''
END
-----------check if subject is present for particular classroom and syllabus
SELECT @countsubject=count(1) FROM subject
WHERE ClassroomID =@classroomid
and SyllabusID =@syllabusid
and Subject =@strsubject
-----count is 0 then insert the subject------
IF @countsubject =0
Begin
Insert into Subject(SyllabusID,ClassroomID,Subject)
values(@syllabusid ,@classroomid ,@strsubject )
set @subjectid =SCOPE_IDENTITY ();
End
End
---------------------Setting value of subject-----------
End
GO
Happy Coding.
Regards,
Krunal Panchal
Monday, November 15, 2010
UDF for Seperation of CSV in SQL
Hi All,
This is the UDF for values having delimiter as COMMA(,)
How to Use::
1) Copy & Paste the below UDF in SQL and Execute it
ALTER FUNCTION [dbo].[UF_CSVToTable]
(
@psCSString VARCHAR(8000)
)
RETURNS @otTemp TABLE(sID VARCHAR(20))
AS
BEGIN
DECLARE @sTemp VARCHAR(10)
WHILE LEN(@psCSString) > 0
BEGIN
SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
LEN(@psCSString)))
SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
LEN(@psCSString)) + 1, LEN(@psCSString))
INSERT INTO @otTemp VALUES (@sTemp)
END
RETURN
END
2) Now write the Query eg:
select * from [Your Table Name] where [Your Column Name] IN
(Select * from UF_CSVToTable('1,2,3,4,5')
Regards,
Krunal Panchal
This is the UDF for values having delimiter as COMMA(,)
How to Use::
1) Copy & Paste the below UDF in SQL and Execute it
ALTER FUNCTION [dbo].[UF_CSVToTable]
(
@psCSString VARCHAR(8000)
)
RETURNS @otTemp TABLE(sID VARCHAR(20))
AS
BEGIN
DECLARE @sTemp VARCHAR(10)
WHILE LEN(@psCSString) > 0
BEGIN
SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
LEN(@psCSString)))
SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
LEN(@psCSString)) + 1, LEN(@psCSString))
INSERT INTO @otTemp VALUES (@sTemp)
END
RETURN
END
2) Now write the Query eg:
select * from [Your Table Name] where [Your Column Name] IN
(Select * from UF_CSVToTable('1,2,3,4,5')
Regards,
Krunal Panchal
Insertion & Selection in SQL storedprocedure with CSV
Hi All,
This is the Stored Procedure for inserting multiple values in multiple columns in SQL
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertProductMembers]
(
@MemberId varchar(100),
@MemberProductId varchar(100)
)
AS
DECLARE @count int
DECLARE @str VARCHAR(8000)
DECLARE @spot SMALLINT
DECLARE @str1 VARCHAR(8000)
DECLARE @spot1 SMALLINT
WHILE @MemberProductId <> ''
BEGIN
SET @spot = CHARINDEX(',', @MemberProductId)
IF @spot>0
BEGIN
SET @str = CAST(LEFT(@MemberProductId, @spot-1) AS varchar)
SET @MemberProductId = RIGHT(@MemberProductId, LEN(@MemberProductId)-@spot)
END
ELSE
BEGIN
SET @str = CAST(@MemberProductId AS varchar)
SET @MemberProductId = ''
END
SET @spot1 = CHARINDEX(',', @MemberId)
IF @spot1>0
BEGIN
SET @str1 = CAST(LEFT(@MemberId, @spot-1) AS varchar)
SET @MemberId = RIGHT(@MemberId, LEN(@MemberId)-@spot)
END
ELSE
BEGIN
SET @str1 = CAST(@MemberId AS varchar)
SET @MemberId = ''
END
SELECT @count=count(1) FROM test
WHERE id=@str
AND id1=@MemberId
IF @count =0
BEGIN
INSERT INTO test (id, id1) VALUES( @str1,@str)
END
end
RETURN
Regards,
Krunal Panchal
This is the Stored Procedure for inserting multiple values in multiple columns in SQL
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertProductMembers]
(
@MemberId varchar(100),
@MemberProductId varchar(100)
)
AS
DECLARE @count int
DECLARE @str VARCHAR(8000)
DECLARE @spot SMALLINT
DECLARE @str1 VARCHAR(8000)
DECLARE @spot1 SMALLINT
WHILE @MemberProductId <> ''
BEGIN
SET @spot = CHARINDEX(',', @MemberProductId)
IF @spot>0
BEGIN
SET @str = CAST(LEFT(@MemberProductId, @spot-1) AS varchar)
SET @MemberProductId = RIGHT(@MemberProductId, LEN(@MemberProductId)-@spot)
END
ELSE
BEGIN
SET @str = CAST(@MemberProductId AS varchar)
SET @MemberProductId = ''
END
SET @spot1 = CHARINDEX(',', @MemberId)
IF @spot1>0
BEGIN
SET @str1 = CAST(LEFT(@MemberId, @spot-1) AS varchar)
SET @MemberId = RIGHT(@MemberId, LEN(@MemberId)-@spot)
END
ELSE
BEGIN
SET @str1 = CAST(@MemberId AS varchar)
SET @MemberId = ''
END
SELECT @count=count(1) FROM test
WHERE id=@str
AND id1=@MemberId
IF @count =0
BEGIN
INSERT INTO test (id, id1) VALUES( @str1,@str)
END
end
RETURN
Regards,
Krunal Panchal
Sunday, November 14, 2010
Round Up Values in C
# include <stdio.h>
# include <conio.h>
void main()
{
double d;
int i;
clrscr();
d=8.5;
i=(d<0)?d-0.5:d+0.5;
printf("The round up value is ::%d",i);
getch();
}
Output: The round up value is ::9
# include <conio.h>
void main()
{
double d;
int i;
clrscr();
d=8.5;
i=(d<0)?d-0.5:d+0.5;
printf("The round up value is ::%d",i);
getch();
}
Output: The round up value is ::9
Subscribe to:
Posts (Atom)


