Pages

Social Icons

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

Thursday, 29 March 2012

SQL Server 2005 Computed Column Is Persisted

"Persisted" means "stored physically" in this context

It means that the computed value is computed once on insert (and on updates) and stored on disc, so it does not have to be computed again on every select.

Persisted also causes a performance penalty on insert and updates, since the column must be computed, but will increase the performance on subsequent select queries.

So, it depends on your usage pattern, which approach to follow: if you update infrequently, but query a lot, you should set persisted = true.

If you update frequently, or if you do not care about retrieval performance, you should consider setting persisted = false

If column is not persisted then we can't create the indexes on it.

Wednesday, 28 March 2012

Deterministic and Non Deterministic Function in SQL


Deterministic functions always return the same result any time they are called with a specific set of input values and given the same state of the database. Non deterministic functions may return different results each time they are called with a specific set of input values even if the database state that they access remains the same. 

Deterministic functions - SUM, AVG, DAY, ISNUMERIC, ISNULL, CONVERT 
Non deterministic functions - GETDATE, RAND, @@ROWCOUNT. USER_NAME, IDENTITY



Below is the query by which we can find that our function or Stored Procedure is deterministic or not.


IF OBJECTPROPERTY (OBJECT_ID('Function or Stored Procedure Name'),'IsDeterministic') = 1
   PRINT 'Function is detrministic.'
ELSE IF OBJECTPROPERTY (OBJECT_ID ('Function or Stored Procedure Name') ,'IsDeterministic') = 0
   PRINT 'Function is NOT detrministic'
GO

Monday, 26 March 2012

Alter the Computed Column

ALTER COMPUTED COLUMN is not allowed if the compatibility level is 65 or lower. For more checking the compatibility level use sp_dbcmptlevel (Transact-SQL).

so you would need to Drop the computed column and re-create it.

ALTER TABLE [MyTable] DROP COLUMN MyColumn
GO

ALTER TABLE [MyTable] ADD MyColumn AS (A+B+C) PERSISTED 



Sunday, 4 March 2012

Thursday, 1 March 2012

How to check given year is leap year or not

There is a function which gives wheather given year is leapyear or not



CREATE FUNCTION dbo.IsLeapYear (@year INT)
RETURNS INT
AS
BEGIN
DECLARE @value INT
DECLARE @datetime DATETIME

SET @datetime = CONVERT(DATETIME,'02/01/'+CONVERT(VARCHAR,@year),101)

IF (DATEPART(DD,DATEADD(DD,-1,DATEADD(MM,1,CAST(CAST(YEAR(@datetime) AS VARCHAR)+'-'+CAST(MONTH(@datetime) AS VARCHAR)+'-01' AS DATETIME)))) = 29 )
BEGIN SET @value = 1 END ELSE BEGIN SET @value = 0 END 

RETURN @value
END


we can use this function to check the year if the given year is leap year then It will return 1 Otherwise it will return 0.