Tuesday, April 15, 2014

Get Duplicate number of records for a column in one Table using Group By and Having clause

Those who are looking for some solution of how to get the number of duplicate records in a Table for particular rows. here is how we can achieve this requirement.

SELECT G_USER,COUNT(G_USER) FROM USER_PROFILE GROUP BY G_USER HAVING COUNT(G_USER) >1


 Using the Group By and Having clause of T-Sql we can achive this requirement stated in sample above, all you need to do is change the Table name and Column Name with your database objects names.

Hope would be Helpful!!
 

Saturday, November 9, 2013

Query to Check Currently running queries on Database server

If you are looking for which query executing on server you can use the following query to get the result


SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext


if you want to kill some long running query then you can fire following command to terminate the query execution

kill [process id]


Happy Coding.

Wednesday, October 30, 2013

Generate Random Alpha Numeric Number with

When we require to generate unique id without using NEWID() function of sql, here is the good alternative to generate unique alpha nemuric identifier to use at primary key.

DECLARE @alpha_numeric VARCHAR(16)
SET @alpha_numeric=''

SELECT @alpha_numeric=@alpha_numeric+CHAR(n) FROM
( SELECT TOP 16 number AS n FROM master..spt_values WHERE TYPE='p' and (number between 48 and 57 or number between 65 and 90) ORDER BY NEWID()) AS t

SELECT @alpha_numeric


Hope this will be helpful to generating manual alpha numeric identifier.

Happy Coding!!

Saturday, November 20, 2010

configuration management database (CMDB)

Hey Guys,

Before sometime writing to this post i was also among you people about what is CMDB, but after doing some research i came to know that it is very helpful for Enterprise Level Applications and Infrastructure projects.

Here i am sharing some basics with you to familiar with CMDB

DEFINITION -
A configuration management database (CMDB) is a database that contains all relevant information about the components of the information system used in an organization's IT services and the relationships between those components. A CMDB provides an organized view of data and a means of examining that data from any desired perspective. Within this context, components of an information system are referred to as configuration items (CI). A CI can be any conceivable IT component, including software, hardware, documentation, and personnel, as well as any combination of them. The processes of configuration management seek to specify, control, and track configuration items and any changes made to them in a comprehensive and systematic fashion.


The IT Infrastructure Library (ITIL) best practices standards include specifications for configuration management. According to ITIL specifications, the four major tasks of configuration management are:

(1)Identification of configuration items to be included in the CMDB
(2)Control of data to ensure that it can only be changed by authorized individuals
(3)Status maintenance, which involves ensuring that current status of any CI is consistently recorded and kept updated
(4)Verification, through audits and reviews of the data to ensure that it is accurate.

Guys i am sharing only basics of what CMDB is and duties it performs. i am sure you will go far than me and explore more...!!!

Happy Coding

C# program that interfaces with MS Access: Store data (text, image) in an Access database file

1. Open a new Visual C# .NET windows application. Name the project CreateDatabase.

2. Design a form. Add three Textboxes and two Buttons

3. Set Name property of Textboxes to “txtDatabase”, “txtStudentName” and “txtStudentPicture”.

4. Set Name property of Buttons to “btnCreateTable” with text property “Create Table” and “btnSave” with text property “Save”.

5. In the code file add namespace:

using System.Text;

using System.Data.OleDb;

6. Double click on the Button “Create Table” and pest the following code into the “btnCreateTable_Click” event.



OleDbCommand Cmd;
string SQL = "";
OleDbCommand objCmd = new OleDbCommand();

OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data
source=" + txtDatabase.Text + "");

SQL = "CREATE
TABLE tblStudentInfo ([StudentID] COUNTER, [StudentName] TEXT(50), [Picture]
OLEObject)";
Cmd = new
OleDbCommand(SQL, Con);

Con.Open();
objCmd = new OleDbCommand(SQL,
Con);

objCmd.ExecuteNonQuery();
Con.Close();
7. Double click on the Button “btnSave” and pest the following code into the “btnSave_Click” event.

StoreData(ImageToStream(txtStudentPicture.Text));
8. Add the following modules to the code file.

private byte[] ImageToStream(string fileName)

{
Bitmap image = new Bitmap(fileName);
MemoryStream stream = new
MemoryStream();

image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
return stream.ToArray();
}

private void
StoreData(byte[] content)
{

OleDbConnection
conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source="
+ txtDatabase.Text + "");

conn.Open();

if (conn.State.Equals(ConnectionState.Closed))

conn.Open();
try
{
OleDbCommand insert = new OleDbCommand("Insert into tblStudentInfo (StudentName,Picture)
values(@StudentName,@image)", conn);

OleDbParameter
picParameter = insert.Parameters.Add("@StudentName",
OleDbType.VarChar );

picParameter.Value = txtStudentName.Text;

picParameter.Size = 50;
OleDbParameter
imageParameter = insert.Parameters.Add("@image", SqlDbType.Binary);

imageParameter.Value = content;

imageParameter.Size = content.Length;

insert.ExecuteNonQuery();
MessageBox.Show("Data
Stored successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{

conn.Close();
}
}
9. Press F5 to build and run the project.

Put your database name with location like c:\MyAccessDB in the text box “txtDatabase”, put student name in the text box “txtStudentName” and put student picture path in the text box “txtStudentPicture” then press the “Create Table” Button and after that press “Save” button to save the student information to the database table.

Saturday, March 20, 2010

Solution to search suggestions with permutition

Hi All,

In one of requirement i need to show the all possible combinitation of text provided in input to show possible search suggestions, i started thinking like school student of permutition and i found the solution, here is my code i have solved with Javascript

permute = function(v, m){ //v1.0
for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
for(r = new Array(q); ++p < q;)
for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
for(x[3][i] = x[2][i], f = 0; !f; f = !f)
for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
return r;
};

function callPermute()
{
var a = ["A","B","C"], j = permute(a);
document.write("< h2 >", a.join(" "), " = ", j.length, "< /h2 >",j.join("< br / >"));
}

you can call the method callPermute() to show the require result

Happy Coding :)

Sunday, January 31, 2010

AssociatedControlID of Asp.Label control in Asp.net

Hi All,

Today i have found one interesting part of Asp.Net Label control. Label server control have one property AssociatedControlID.

Problem : without asssigning the AssociatedControlID property a Asp.net Label server control rendered as "span" which is not satisfied the client requirement of 'it should render as label for accessibility'

Here is the output before and after,

Before,

span id="form_103_lblFirst_Name">First Name /span

Assigned "AssociatedControlID" property and Result after,

  123 lblTitle.AssociatedControlID = txtName.ID;



label id="form_103_lblFirst_Name" for="form_103_First_Name">First Name /label

Solution : I have assigned the AssociatedControlID to the Asp.net Label server control to the textbox.ID to which it refering to. and found that it is rendered to "label" html tag which i required it also includes 'for' property which references the ID of Associated Control.

Happy Coding :)