Sunday, February 26, 2012

Combine multiple rows in a single column, Concatenate records without UDF


CREATE TABLE test
(
id INT, Categoryid INT, Category VARCHAR(50), Status VARCHAR(50)
)

------------------------------
INSERT INTO test(id,categoryid,category,[Status])
SELECT 34676 ,02 ,'CLOTHING' ,'ACTIVE'
UNION
SELECT 34676 ,02 ,'CLOTHING' ,'DANCE'
UNION
SELECT 34676 ,02 ,'CLOTHING' ,'LWR'
----------------------------------

There is two ways to implement this
1.
SELECT DISTINCT id,categoryid,category ,dbo.fnMurliTest(categoryid) As NewColumn From test

Alter function fnTest(@int int)
Returns varchar(8000) As
Begin
DECLARE @strOutput VARCHAR(8000)
SET @strOutput = ''
SELECT @strOutput = @strOutput + ',' + [Status] FROM test
WHERE [CategoryID] = @int ORDER BY [Status]
RETURN STUFF(@strOutput, 1, 1, '')
--RETURN SubString(@strOutput, 2, 8000)
End

2.
SELECT DISTINCT id,categoryid,category,
(
SELECT ',' + status as 'data()'
FROM test
FOR XML PATH('')
) As NewColumn
FROM test

Thursday, February 23, 2012

jQuery Function

jQuery substring

jQuery('#textArea').keypress(function() {
var text = jQuery('#textArea').val();
if
(text.length > 400) {
text = text.substring(0, 400);
jQuery('#textArea').val(text); }
}
);

jQuery refresh page
jQuery('#refresh-link').click(function() {     
location.reload();
}
);

jQuery message box

#darkbg {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;

opacity: .5;
filter: alpha(opacity=50);
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
}
#message_box {
width: 300px;
height: 150px;
background: #fff;

border: 4px solid #f0f0f0;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;

position: absolute;
top: 100px;
left: 50%;
margin-left: -150px;

text-align: center;
z-index: 1000;
display: none;
}
#message_box input[type=button] {
float: right;
margin-right: 10px;
}

var message_box = function() {
var button = '<input type="button" onclick="message_box.close_message();" value="Okay!" />';
return {
show_message: function(title, body) {
if(jQuery('#message_box').html() === null) {
var message = '<div id="message_box"><h1>' + title + '</h1>' + body + '<br/>' + button + '</div>';
jQuery(document.body).append( message );
jQuery(document.body).append( '<div id="darkbg"></div>' );
jQuery('#darkbg').show();
jQuery('#darkbg').css('height', jQuery('html, body').height());
jQuery('#message_box').css('top', jQuery('html, body').scrollTop() + 150);
jQuery('#message_box').show('slow');
} else {
var message = '<h1>' + title + '</h1>' + body + '<br/>' + button;
jQuery('#darkbg').show();
jQuery('#darkbg').css('height', jQuery('html, body').height());
jQuery('#message_box').css('top', jQuery('html, body').scrollTop() + 150);
jQuery('#message_box').show('slow');
jQuery('#message_box').html( message );
}
},
close_message: function() {
jQuery('#message_box').hide('fast');
jQuery('#darkbg').hide();
}
}
}();

----------
message_box.show_message('Hi!', 'Whatup?!');
-------------

jQuery('#some_link').onclick(function() { message_box.show_message('Hi!', 'Whatup?!'); });

Javascript to count selected items in CheckBoxList

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
CheckBoxList1.Attributes.Add("onclick", "javascript:CopyNumItemsToTextBox();")
End Sub

--------------------------------

function CopyNumItemsToTextBox() {
//This function updates TextBox1 with the number of items checked when an item is checked/unchecked
//Get the checkbox object
var textBox = document.getElementById("TextBox1");
//Get the checkboxlist object
var checkBoxList = document.getElementById("CheckBoxList1");
//Get the number of checkboxes in the checkboxlist
var numCheckBoxItems = checkBoxList.cells.length;
var numItemsChecked = 0;

for(i=0; i<numCheckBoxItems; i++)
{
//Get the checkboxlist item
var checkBox = document.getElementById(checkBoxList.id + '_' + [i]);
//Check if the checkboxlist item exists, and if it is checked
if(checkBox!=null && checkBox.checked){ numItemsChecked = numItemsChecked + 1; }
}
//Set the text box to the number of items checked in the checkboxlist
textBox
.value = numItemsChecked;
}

Tuesday, February 21, 2012

Copy values from one object to another

public static void CopyPropertyValues(object source, object destination)
{
var destProperties = destination.GetType().GetProperties();

foreach (var sourceProperty in source.GetType().GetProperties())
{
foreach (var destProperty in destProperties)
{
if (destProperty.Name == sourceProperty.Name &&
destProperty
.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
{
destProperty
.SetValue(destination, sourceProperty.GetValue(
source
, new object[] { }), new object[] { });

break;
}
}
}
}

Friday, February 3, 2012

What are the difference between DDL, DML and DCL commands?

DDL


Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:
  • CREATE - to create objects in the database
  • ALTER - alters the structure of the database
  • DROP - delete objects from the database
  • TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
  • COMMENT - add comments to the data dictionary
  • RENAME - rename an object

DML


Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
  • SELECT - retrieve data from the a database
  • INSERT - insert data into a table
  • UPDATE - updates existing data within a table
  • DELETE - deletes all records from a table, the space for the records remain
  • MERGE - UPSERT operation (insert or update)
  • CALL - call a PL/SQL or Java subprogram
  • EXPLAIN PLAN - explain access path to data
  • LOCK TABLE - control concurrency

DCL


Data Control Language (DCL) statements. Some examples:
  • GRANT - gives user's access privileges to database
  • REVOKE - withdraw access privileges given with the GRANT command

TCL


Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
  • COMMIT - save work done
  • SAVEPOINT - identify a point in a transaction to which you can later roll back
  • ROLLBACK - restore database to original since the last COMMIT
  • SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use