Posted by: R Manimaran | June 3, 2009

Delete all the Oracle tables data’s in the database

Procedure:

 

create or replace FUNCTION TRUNC_SCHEMA RETURN NUMBER AS

CURSOR SELECT_TABLE IS SELECT TABLE_NAME AS TNAME FROM USER_TABLES ORDER BY 1;

sTableName varchar2(128);

sUser VARCHAR2(128);

sConstraintName varchar2(128);

plsql_block VARCHAR2(128);

BEGIN

  SELECT USER INTO suser FROM Dual;

  IF((suser=’SYSTEM’) or (suser=’SYS’)) then

     RETURN 1;

  END IF;

 

–EXECUTE IMMEDIATE ‘PURGE RECYLCLEBIN’;

 

–Disalbe Table’s Constraints

FOR C1 IN (SELECT CONSTRAINT_NAME,TABLE_NAME FROM user_constraints where STATUS=

‘ENABLED’ AND constraint_type in (‘P’,'R’) ORDER BY R_CONSTRAINT_NAME)

LOOP

sConstraintName := C1.CONSTRAINT_NAME;

sTableName := C1.TABLE_NAME;

plsql_block :=’ALTER TABLE ‘|| sTableName ||’DISABLE CONSTRAINT’||sConstraintName;

EXECUTE IMMEDIATE plsql_block;

END LOOP;

 

FOR D in SELECT_TABLE LOOP

–get the table name

sTableName:=D.TNAME;

–clear Table

plsql_block := ‘TRUNCATE TABLE ‘||sTableName;

EXECUTE IMMEDIATE plsql_block;

END LOOP;

 

–Enable Table CONSTRAINTS

FOR C2 IN (SELECT CONSTRAINT_NAME,TABLE_NAME FROM user_constraints where STATUS=

‘ENABLED’ AND constraint_type in (‘P’,'R’) ORDER BY R_CONSTRAINT_NAME)

LOOP

sConstraintName := C2.CONSTRAINT_NAME;

sTableName := C2.TABLE_NAME;

plsql_block :=’ALTER TABLE ‘|| sTableName ||’ENABLE CONSTRAINT ‘||sConstraintName;

EXECUTE IMMEDIATE plsql_block;

END LOOP;

 

RETURN 0;

EXCEPTION WHEN OTHERS THEN

dbms_output.put_line(‘ERROR:’||SQLERRM);

RETURN 1;

END TRUNC_SCHEMA;

Posted by: R Manimaran | June 3, 2009

SQL Order by based on the Field values

SQL Order by based on the Field values

 In the SQL query, we use Order By clause to order the data in the output. In the Order by we use the Fieldname with Ascending or descending option. But suppose we need to override the Order By to order the output based on the values a Field.

 

Normal Order By clause

 NormalOrderBy

Normal Order By Output

 Order By Output

Order By using Case

 OrderByQuery

Order By Output using Case

 OrderByOutput

 

SP_MSFOREACHTABLE stored Procedure in SQL SERVER

 

i.                    Print all table names with owner

 

EXEC SP_MSFOREACHTABLE @command1 =”PRINT ‘?’”

 

ii.                  Drop all the tables

 

EXEC SP_MSFOREACHTABLE @command1 =”DELETE FROM ‘?’”

 

iii.                Delete all the tables

 

EXEC SP_MSFOREACHTABLE @command1 =”DROP TABLE ‘?’”

 

iv.                Delete all the data’s inside all the tables in a Database

In a table, we may have some referential Constraint to tables. So when deleting values it will show the error message that it has reference to other table. In that situation it is difficult to search each and every reference.

 

Here is the list of Query which will delete all the table data’s

 

–List all the tables

EXEC SP_MSFOREACHTABLE @command1=”PRINT ‘?’”

 

–Disable all the Reference Integrity in all tables

EXEC SP_MSFOREACHTABLE ‘ALTER TABLE ? NO CHECK CONSTRAINT ALL’

GO

–Delete the data’s

EXEC SP_MSFOREACHTABLE

     IF OBJECTPROPERTY(object_id(“?”),”TABLEHASFOREIGNREF”)=1

         DELETE FROM ?

    ELSE

         TRUNCATE TABLE ?

GO

–Now enable reference integrity

EXEC SP_MSFOREACHTABLE ‘ALTER TABLE ? CHECK CONSTRAINT ALL’

GO

Posted by: R Manimaran | April 20, 2009

Comma separated Column value in Crystal Report

I have come across a situation in Crystal Report. Suppose for example I have two tables Employees and Departments.

An Employee can be belonged to two departments. In the report I need some thing like this

 

Report Output

Report Output

 

 I don’t want to insert any group. Just a list of employees with their corresponding Departments

 

Solution:

To achieve this in crystal report we need to write some formula.

 

Formula 1: @Department

 

WhilePrintingRecords;

Shared StringVar Result;

if(NextIsNull({Employees.EMPID})=true or

  {Employees.EMPID }<>Next({Employees.EMPID })) then

   Result:={Departments.Name}

else

   Result:={Departments.Name }& “,” & chr(13)& Next({Departments.Name })

 

Place the @Departments formula in the details section. Now you will get the output like this.

 

 

 

 

Report Output

Report Output

 

We need to eliminate the second row and fourth row.  For that we need to do

  1. Right click the EMP.ID field in the details section and select Suppress if duplicated option.
  2. Now select the EMP.Name and @Departments fields. Right click and select Format Field . In the common tab check Suppress.
  3. In the X2 formula section of the Suppress write this formula.

WhilePrintingRecords;

if(PreviousIsNull({Employees.EMPID})=true or {Employees.EMPID}<>

Previous({Employees.EMPID})) then

   false

else

   true

  1. Now whole second row and fourth row are appearing blank.
  2. To eliminate that space, Right click details section and select Section Expert.
  3. Check the Suppress blank section option.

If anybody founds any other way achieve this, can post the things here.

Happy Coding!!

 

Showing “Open with” Option on clicking any drive in the Explorer

Sometimes when we click the drive in the explorer it will show the “Open with” option. This is due to the some virus which creates the autorun.inf file.

For this follow these steps.

1.    Open cmd prompt.

2.    Select the drive that is showing the “Open with” box. (Move to the root)

3.    Suppose if C drive shows open with means you have to be in the root path as

a.    C:\>

4.    Type “attrib –r –h –s autorun.inf”

5.    Then type “del autorun.inf”

6.    Open Task Manager and Select Explorer in the running processes.

7.    Click End Task.

8.    Under the Application tab click “New Task” button.

9.    Type Explorer and click ok.

10. Now the explorer is restarted. Now Click the C drive. Is it works ?

Posted by: R Manimaran | March 30, 2009

Show Hidden files and Folders Problem in Windows XP

Show Hidden files and Folders Problem in Windows XP.

In Windows XP we have a option to hide or show the hidden files. In the Windows explorer click Toolsà Folder option.

On the opened dialog box Select View Tab.

Under Hidden Files and Folders you have two option box

  • Do not show Hidden files and Folders
  • Show Hidden files and folders

 

In Windows XP sometimes if we set the Show Hidden Files and Folders option it automatically reset to Do not show Hidden files and Folders.

Resolution:

To fix this, we need to do some modification in the registry.

Type “regedit” in the Run Dialog box.

Search the following path.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL

On the Right side delete the KeyWord “CheckedValue” and add a new DWORD “CheckedValue” with the value “1”.

Close the Regedit and log off the system and log-in.

Now Change the option in the Toolsà Folder option.

Posted by: R Manimaran | March 30, 2009

“RESTORE MY DESKTOP” error in Windows XP System

“RESTORE MY DESKTOP” error in Windows XP System

Sometimes when working in Windows XP we may come with this Problem. A button will be showed in the desktop with a Text “Restore My Desktop”. On Clicking the button you will get the script error message

Error:

file:///C:/Documents%20and%20Settings/My%20Name/Application%20Data/Microsoft/Internet%20Explorer/Desktop.htt

Resolution:

For this we need to do some change in the Registry.

Note: Extra Care should be taken while working with Registry.

Type “regedit” in the RUN dialog box.

Search for the following Key

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Desktop\SafeMode\Components

Change the Keyvalue of the right side of the pane as follows

DeskHtmlVersion REG_DWORD 0×00000110(272) to decimal zero

Close Regedit and logoff the system and log on.

Posted by: R Manimaran | October 2, 2008

Debug a Windows Service in Visual Studio 2005

Debug a Windows Service application in Visual studio 2005
For normal application we can set the breakpoint in Visual studio and start debugging by pressing F10 or F11. But for the Windows services application we can’t directly debug the application. Following steps will help to debug the windows service application.

1. Create the setup project for the Windows Service application.
2. If the Solution contain more than one Project, select the Windows Service project as the Start up project by right-click the project and select Set as Startup project
3. In the Solution configuration select Debug mode. In the release mode we can’t debug the application.
4. Install the Windows service setup in the system
5. Go to the services and start your windows services. Once it successfully started proceed to next step.
6. Set Breakpoint on your application( press F9 for breakpoint)
7. Go to Tools–>Options menu in the VS 2005
8. Select Debugging –>Symbols

9. In the Symbol file(.pdb) locations add the path of the pdb file path.(should include the pdb file name also)
i.e \Debug\

10. The Pdb file is located in the application Debug directory.
11. In the Cache symbols servers to this directory browse the path where the Pdb file is located.(no pdb file name required here. Only path up to Debug folder)
12. Click ok to set your setting. After finishing this again to this window and double check that your options are still available.
13. Select Debug–>Attach to Process option as shown in fig

14. Select your running windows service in the Available processes

15. Click Attach button to attach the Windows service to your application.
16. Now you can see the application is starts running.
17. Please wait until the flow reaches your breakpoint.
18. Once it reaches your breakpoint then you can use your normal F10 or F11 keys to debug the application.

happy Coding!!!

Posted by: R Manimaran | September 26, 2008

DataTable to Excel File

ADD REFERENCE
• Interop.Excel
• Interop.Office
• Microsoft.Vbe.Interop

private static Excel.Application app = null;
private static Excel.Workbook workbook = null;
private static Excel.Worksheet worksheet = null;
private static Excel.Range worksheet_range = null;

public static void CreateExcelDoc()
{
try
{
app = new Excel.Application();
app.Visible = true;
workbook = app.Workbooks.Add(1);
worksheet = (Excel.Worksheet)workbook.Sheets[1];
}
catch (Exception ex)
{
throw new ApplicationException(“Error while creating the Excel
file.Message:” + ex.Message);
}
}
public static void CloseDoc(string Path)
{
workbook.Close(true, Path, false);
app.Quit();
if (worksheet != null)
Marshal.ReleaseComObject(worksheet);
if (workbook != null)
Marshal.ReleaseComObject(workbook);
if (app != null)
Marshal.ReleaseComObject(app);
}

private static void InsertSummaryDataToExcel(DataTable dtResult, ref int row)
{
int col=0;
string WorksheetRangeSt = string.Empty;
string WorksheetRangeEnd = string.Empty;
foreach (DataRow dtRow in dtResult.Rows)
{
col=1;
WorksheetRangeSt = “A” + row.ToString();
foreach (DataColumn dtCol in dtResult.Columns)
{
worksheet.Cells[row, col] = dtRow[dtCol.ColumnName].ToString();
col = col + 1;
}
row = row + 1;
}
WorksheetRangeEnd = “E” + row.ToString();
worksheet_range = worksheet.get_Range(WorksheetRangeSt,
WorksheetRangeEnd);
worksheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
worksheet_range.Interior.Color = System.Drawing.Color.Ivory.ToArgb();
row = row + 1;
}

Posted by: R Manimaran | September 26, 2008

Convert DataTable to CSV File

Get the DataTable from Sql
private DataTable GetQueryOutput(string Sql)
{
DataTable dtResult = null;
if (con.State != ConnectionState.Open)
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(Sql, con);
dtResult = new DataTable(“Output”);
dtResult.BeginLoadData();
da.Fill(dtResult);
dtResult.EndLoadData();
return dtResult;
}

//Declare a StreamWriter
System.IO.StreamWriter writer=null;
string SepChar=”,”;
try
{
writer = new StreamWriter(FileName);
string sep = “”;
StringBuilder builder = new StringBuilder();
string Sql=string.Empty;
//Writing the Column Heading
foreach (DataColumn col in dt.Columns)
{
builder.Append(sep).Append(col.ColumnName);
sep = SepChar;
}
writer.WriteLine(builder.ToString());

//write the Rows
foreach (DataRow row in dt.Rows)
{
sep = “”;
builder = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
builder.Append(sep).Append(row[col.ColumnName]);
sep = SepChar;
}
writer.WriteLine(builder.ToString());

There may be a situation where we need to modify the Column Title display string.
We can change the Column title using the Display string in the Format Field option.

Select the Column Title in the Cross-tab. Right click and select Format Field option. Under the Common Tab select the Display string x-2 button.

Use the GridRowColumnValue function in the condition statement
i.e.
if GridRowColumnValue(“TableName.ColumnName”)=”string to Check” then
“New Disp String”
Else if GridRowColumnValue(“TableName.ColumnName”)=”string to Check” then
“New Disp String2″

Older Posts »

Categories