Pages

Social Icons

Monday 30 April 2012

Difference between stored procedure and function


Below are few differences between Stored Procedure and Function 


1) Stored procedure are compiled for first time and compiled format is saved and executes compiled code when ever it is called. But function is compiled and executed every time it is called. 


2) Function must return a value but in stored procedure it is optional. 


3) Functions can be called from select statement, but stored procedures can not be called from select statement. 


4) We can build logic in functions and we can even break complex logic in to methods. 


5) We can use try catch statements in stored procedures but in functions we can not use. 


6) We can not use insert,delete,update and create statements in functions but in stored procedures we can use those statements. 


7 ) Functions can have only input parameters but stored procedure can have input and out put parameters 


8) You can use UDF in Join but we can not use Stored procedure in Join

Identify last statement run for a specific SQL Server session


You can use the @@spid() system function to return the session_id of the current session as follows:


SELECT @@spid


For my test I get session_id = 61


Now run your query:
SELECT * FROM <table> 


Run the following query you will get the your last executed query :


SELECT DEST.TEXT  
FROM sys.[dm_exec_connections] SDEC 
CROSS APPLY sys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle]) AS DEST 
WHERE SDEC.[most_recent_session_id] = 61 


OR
  
SELECT SDEC.[most_recent_sql_handle], DEST.[text]  
FROM sys.[dm_exec_connections] SDEC 
CROSS APPLY sys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle]) AS DEST 
WHERE SDEC.[most_recent_session_id] = 61 

Check SQL-Server Version using Query

With the help of below query you can find the SQL-Server Version and other details.

SELECT SERVERPROPERTY('productversion') AS VERSION, 
  SERVERPROPERTY ('productlevel') AS ServicePack, 
  SERVERPROPERTY ('edition') AS Edition
OR
SELECT @@version

Wednesday 25 April 2012

Load listbox on-demand in JQuery

Here I am posting the JQuery code to load the list box on demand. This code will load some number of record first and when you say Load more record it will load more record.


Exapmle :


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
        <script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
        <script language="javascript" type="text/javascript">
            function getRecord(id) {
                try {
                    var myOptions = {
                        12321: 'text1',
                        12322: 'text2'
                    };
                    $.each(myOptions, function (val, text) {
                        $('#' + id).append(
                        $('<option></option>').val(val).html(text)
                    );
                });
                }catch(e)
                {
                    alert(e);
                }
               
            }
        </script>
</head>
<body>
   <select id = "Record" multiple="multiple" size="10">
                <option> 1 </option>
                <option> 2 </option>
                <option> 3 </option>
                <option> 4 </option>
                <option> 5 </option>
                <option> 6 </option>
                <option> 7 </option>
                <option> 8 </option>
                <option> 9 </option>
                <option> 10</option>
                <option> 11</option>
                <option> 12</option>
                <option id="loadMoreRecord" onclick="getRecord('Record');"> Load more record </option>
        </select>
</body>
</html>


Tuesday 24 April 2012

CUBE, ROLLUP In SQL SERVER



I learn about CUBE and ROLLUP function today and I found that these operator are useful in generating reports that contain subtotals and totals. There are extensions of the GROUP BY clause.


Difference b/w CUBE and ROLLUP:
- CUBE generates a result set that shows aggregates for all combinations of values in the selected columns.
- ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns.


Let me give you one example :


select 'A' [class], 1 [rollno], 'a' [section], 80 [marks], 'manoj' stuName
into #tempTable
UNION
select 'A', 2, 'a', 70 ,'harish'
UNION
select 'A', 3, 'a', 80 ,'kanchan'
UNION
select 'A', 4, 'b', 90 ,'pooja'
UNION
select 'A', 5, 'b', 90 ,'saurabh'
UNION
select 'A', 6, 'b', 50 ,'anita'
UNION
select 'B', 1, 'a', 60 ,'nitin'
UNION
select 'B', 2, 'a', 50 ,'kamar'
UNION
select 'B', 3, 'a', 80 ,'dinesh'
UNION
select 'B', 4, 'b', 90 ,'paras'
UNION
select 'B', 5, 'b', 50 ,'lalit'
UNION
select 'B', 6, 'b', 70 ,'hema'


select class, rollno, section, marks, stuName from #tempTable


WITH ROLLUP:


select class, section, sum(marks) [sum]
from #tempTable
group by class, section with ROLLUP


Output:
class section sum
A a 230
A b 230
A NULL 460  -- 230 + 230  = 460
B a 190
B b 210
B NULL 400  -- 190 + 210 = 400
NULL NULL 860  -- 460 + 400 = 860




WITH CUBE:


select class, section, sum(marks) [sum]
from #tempTable
group by class, section with CUBE


Output:
class section sum
A a 230
A b 230
A NULL 460  -- 230 + 230  = 460
B a 190
B b 210
B NULL 400  -- 190 + 210 = 400
NULL NULL 860  -- 460 + 400 = 860
NULL a 420  -- 230 + 190 = 420
NULL   b 440  -- 230 + 210 = 440