Tuesday, December 3, 2013

ORA-01810: format code appears twice


Error Message

ORA-01810: format code appears twice

Cause of Error

You tried to use the TO_DATE function in a query, but you used a format code twice in the date format.

Resolution

Re-write the TO_DATE function so that you only use each format code once. Examples of format codes are:

Format CodeExplanation
YEARYear, spelled out
YYYY4-digit year
MMMonth (01-12; JAN = 01).
MONAbbreviated name of month.
MONTHName of month, padded with blanks to length of 9 characters.
DDay of week (1-7).
DAYName of day.
DDDay of month (1-31).
DDDDay of year (1-366).
DYAbbreviated name of day.
HHHour of day (1-12).
HH12Hour of day (1-12).
HH24Hour of day (0-23).
MIMinute (0-59).
SSSecond (0-59).
SSSSSSeconds past midnight (0-86399).

For example, if you tried to execute the following SELECT statement:
SELECT TO_DATE('2004/12/14 4:29 PM', 'YYYY/MM/DD HH:MM PM' )
FROM dual;
You would receive the following error message:

Some people mistakenly use the MM format code to represent minutes, thus using the MM format for both the months and the minutes.
You could correct this SELECT statement as follows:
SELECT TO_DATE('2004/12/14 4:29 PM', 'YYYY/MM/DD HH:MI PM' )
FROM dual;

Wednesday, November 6, 2013

Hidden share

A network share on a Microsoft network that is not visible when viewing another computer's shares; however, it is still accessible if the name of the hidden share is known.
Default Microsoft Windows hidden shares
Below are examples of Microsoft Windows default hidden shares.
ADMIN$ - The default systemroot or Windows directory.
C$ and x$ - The default drive share, by default C$ is always enabled. x$ represents any other partitions or drives found by Windows that will also be shared, e.g. D$, E$, etc.
FAX$ - Share used by fax clients to access cover pages and other files on a file server.
IPC$ - See the IPC definition for additional information about IPC.
PRINT$ - Share used by printers, enabling remote administration on printers.
Creating a Microsoft Windows hidden share
A Microsoft Windows hidden share is created by adding a "$" at the end. For example, if the share was "hope", adding a "$" to the end of hope, so the shared name is "hope$", will make a hidden share.
Accessing a Microsoft Windows hidden share
Assuming we were attempting to access the "hope$" share that we created in the above example from another computer we would type the below network path to access the hidden share.
//<computer_name>/hope$
Viewing Microsoft Windows hidden shares
Hidden shares give users a false impression that the share cannot be found unless it is known. Although this may be true for most users, a user can use dozens of utilities available on the Internet to view all shares, regardless if they are hidden or not. If you are creating a hidden share to protect sensitive data, password protect the share instead of making it hidden.
An easy method for users to display any hidden shares on their computer is to use the net use command.

Friday, October 25, 2013

Troubleshooting & diagnosing TNS SQL*Net connectivity errors


There are a variety of common network connectivity error messages, and most DBA's have seen TNS error messages these at sometime in their careers.  Here is just a small sample of possible TNS network connectivity-related errors:
  • TNS-12545: Connect failed because target host or object does not exist
  • ORA-12154: TNS: Could not resolve service name
  • ORA-12157: TNS Internal network communication error 
Steps to troubleshoot Oracle connectivity:
To troubleshoot & diagnose Oracle connectivity problems, I like to start at the simplest, outermost level, and follow this checklist:
  1. Check with "ping" using the IP address ("ping 1.1.4.3")
  2. Check with "ping" using DNS name (e.g. "tnsping database")
  3. Try telnet to the IP on port 1521 (telnet 1.2.3.4 1521)
  4. Check with "tnsping" using TNS service name
  5. Invoke SQL*Plus from the OS command line "sqlplus abcd@database".  If this fails, check to ensure that your listener has the database service defined.
  6. Sign-on to SQL*Plus and connect with the TNS name
  7. Within SQL*Plus, try a select * from table


    Above mentioned steps are the first sign of checking the connection error. My server was able to clear all the above seven steps. Still I am not able to connect to database via my application. Then I changed my sqlnet.ora. There were multiple entries, to which I reduced them to a single line

    names.directory_path= (tnsnames)

    Then on investigation I found out, the code that was causing trouble is:

    SQLNET.AUTHENTICATION_SERVICES = (NTS)
    names.default_domain = world

    So even if I commented these two lines in my original sqlnet.ora, it worked. Hope this works for you as well.






Monday, October 21, 2013

ALL_TABLES versus ALL_ALL_TABLES

What's the difference between ALL_TABLES and ALL_ALL_TABLES

Answer
1) Both views provide all tables to which the current user has access to but, in addition to the tables returned by ALL_TABLES, the ALL_ALL_TABLES will also return all object tables (system generated or not) accessible by the current user.

SQL> select *
  2    from dictionary
  3   where table_name in ('TABS','ALL_TABLES','ALL_ALL_TABLES')
  4  /

TABLE_NAME           COMMENTS
-------------------- ------------------------------------------------------------------------------------------------
ALL_ALL_TABLES       Description of all object and relational tables accessible to the user
ALL_TABLES           Description of relational tables accessible to the user
TABS                 Synonym for USER_TABLES


2) The following 3 columns that appear only in ALL_ALL_TABLES, but not in ALL_TABLES 
give you details about the object type on which the object table was created and the object identifier type used: 

OBJECT_ID_TYPE 
TABLE_TYPE_OWNER 
TABLE_TYPE 

If you want to see the difference only between the two views, you can use a select like
the following: 

SELECT * FROM ALL_ALL_TABLES 
WHERE TABLE_TYPE IS NOT NULL 


Even if you don't have object tables in your schemas, you will probably still see some of the object tables used by different Oracle features installed, like XDB, a.s.o. 

3) ALL_ALL_TABLES includes object tables as well as relational tables.


Also, this may be an interview question (e.g. how can you get all tables you have access to?) and you may leave a good impression if you respond with another question: "Do you also want object tables to be included?". :)

Tuesday, October 15, 2013

How to Debug or Test your Windows Service Without Installing it

When you develop a Windows Service and want to run or debug it, you get a message box with this message:
Cannot start service from the command line or a debugger.
A Windows Service must first be installed (using installutil.exe)
and then started with the ServerExplorer, Windows Services 
Administrative tool or the NET START command.
So for testing you have to first install it on your computer, but it is a long process and also boring because every time you make changes, you have to reinstall your service and test it again.

There are many ways it can be done, but I opted this way out becuase of its simplicity.....

For debugging or testing your service without installing it, make changes in Program.cs like this.
static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
 { 
      new MyService() 
 };
        ServiceBase.Run(ServicesToRun);
    }
}
Change it to:
static class Program
{
    static void Main()
    {
        #if(!DEBUG)
           ServiceBase[] ServicesToRun;
           ServicesToRun = new ServiceBase[] 
    { 
         new MyService() 
    };
           ServiceBase.Run(ServicesToRun);
         #else
           MyService myServ = new MyService();
           myServ.Process();
           // here Process is my Service function
           // that will run when my service onstart is call
           // you need to call your own method or function name here instead of Process();
         #endif
    }
}
After adding #if and #else to your main fuction, now when you press F5 or run your service, it will not show you the previous message and simply run, so attach a break point to your method which will be called by the service when it will start. With the use of this code, you can simply debug your service without installing it.
For this no need to add any extra using directive (like using System.Data or using System.IO) to your class file. It will simply as it is.

'installutil' is not recognized as an internal or external command, operable program or batch file.


You get this error when you type 'installutil.exe' in a command prompt.
Reason: .net folder path is not set in the 'PATH' variable because of which location of installutil.exe was not found.

Resolution:

a) Use the visual studio command prompt, it will work there
b) Set .NET folder path in system's 'PATH' variable . Then try it should get the path of 'installutil.exe' and you can use it directly in any command prompt.

c) Use Coding
It's is really easy to just add it to the service itself. Add a reference to System.Configuration.Install and then update your Main()-function in Program.cs like this.
static void Main(string[] args)
{
    if (Environment.UserInteractive)
    {
        string parameter = string.Concat(args);
        switch (parameter)
        {
            case "--install":
            ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
            break;
            case "--uninstall":
            ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
            break;
        }
    }
    else
    {
        ServiceBase[] servicesToRun = new ServiceBase[] 
                          { 
                              new ValidatorService() 
                          };
        ServiceBase.Run(servicesToRun
Then you can just call WindowsService1.exe with the --install argument and it will install the service and you can forget about InstallUtil.exe.

Monday, October 14, 2013

Error saying "The 'DisplayGroupTree' property cannot be set declaratively"

Public Property DisplayGroupTree As Boolean’ is obsolete.

In this version Crystal Reports can have a parameter panel. I now have to tell crystal reports what will be displayed in the left panel:

  • the group tree
  • the parameter panel
  • nothing

The parameter is ToolPanelView. If you don’t want to show nothing in the left panel:
rptViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None

Thursday, September 19, 2013

Retrive password from Toad for Oracle

One of the oldest feature Toad has is saving login passwords. This is accomplish easy with enabling check box "Save passwords" on login screen. 
The whole connection process is defined through three files located in %USERPROFILE%\AppData\Roaming\Quest Software\Toad for Oracle\11.6\User Files\, where "11.6" is Toad version and may vary in your cases: 
  1. CONNECTIONS.INI
  2. CONNECTIONACTIONS.INI
  3. CONNECTIONPWDS.INI
Passwords are stored in encrypted way in CONNECTIONPWDS.INI file. However they are not exposed in any normal way (you can read them) but only to use them as login without knowing password, which was once placed. But having stored passwords allow Toad many beautiful automation and wide a lot actions that might need password as input. 

The solution

The trick is based on another Toad for Oracle feature-get SQL for any kind of DDL action, which was performed through GUI, in this case creating db link. Here is what you have to do to retrieve scott password:
  1. Choose Database|Create|DB Link menu item
  2. Fill the Link Name and Database data as shown in the picture:

  3. Click on 'Set user/Pass to current'
  4. As you can see I have chosen scott user and password is automatically retrieved from saved passwords file.
  5. Choose Show SQL as shown in the picture and you'll get pure SQL which contains password
The trick is working for every user's password. 

Someone might say this is security issue, but I think it is not! Mentioned file with stored passwords is encrypted with two keys:

  1. Domain user name
  2. Some kind of workstation unique hash value
These ensures that password file cannot be copied to another workstation and Domain admins (or other privileged users on that workstation) cannot use that file in any way! 

Wednesday, August 21, 2013

System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

Errorinfo is "System.Data.OracleClient requires Oracle client software version 8.1.7 or greater."

I have installed Oracle Client 11g (11.2.0.1.0) on Server is greater than 8.1.7 , still the error comes. On serching out I find out following solution:

It is a security issue, so to fix it simply do the following:
1- Go to the Oracle Client folder.
2- Right Click on the folder.
3- On security Tab, Add "Authenticated Users" and give this account Read & Execute permission.
4- Apply this security for all folders, Subfolders and Files (IMPORTANT).
5- Don't Forget to REBOOT your Machine; if you forgot to do this you will still face the same problem unless you restart your machine.

Friday, August 16, 2013

SQL Optimization



1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'. 
If we use '*' , then sql takes time to expand it. Though time taken is very less but in large and complex queries, it can be a considerable factor.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;

2) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use HAVING clause for any other purposes.
For Example: Write the query as
SELECT subject, count(subject) FROM student_details WHERE subject != 'Science' AND subject != 'Maths' GROUP BY subject;
Instead of:
SELECT subject, count(subject) FROM student_details GROUP BY subject HAVING subject!= 'Vancouver' AND subject!= 'Toronto';

3) Sometimes you may have more than one subqueries in your main query. Try to minimize the number of subquery block in your query.
For Example: Write the query as
SELECT name FROM employee WHERE (salary, age ) = (SELECT MAX (salary), MAX (age) FROM employee_details) AND dept = 'Electronics';
Instead of:
SELECT name FROM employeeWHERE salary = (SELECT MAX(salary) FROM employee_details) AND age = (SELECT MAX(age) FROM employee_details) AND emp_dept = 'Electronics';

4) Use operator EXISTS, IN and table joins appropriately in your query.
a) Usually IN has the slowest performance.
b) IN is efficient when most of the filter criteria is in the sub-query.
c) EXISTS is efficient when most of the filter criteria is in the main query.
For Example: Write the query as
Select * from product p where EXISTS (select * from order_items o where o.product_id = p.product_id)
Instead of:
Select * from product p where product_id IN (select product_id from order_items

5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as
SELECT d.dept_id, d.dept FROM dept d WHERE EXISTS ( SELECT 'X' FROM employee e WHERE e.dept = d.dept);
Instead of:
SELECT DISTINCT d.dept_id, d.dept FROM dept d,employee e WHERE e.dept = e.dept;

6) Try to use UNION ALL in place of UNION.
For Example: Write the query as
SELECT id, first_name FROM student_details_class10 SELECT id, first_name FROM sports_team;
Instead of:
SELECT id, first_name, subject FROM student_details_class10 UNION SELECT id, first_name FROM sports_team;

7) Be careful while using conditions in WHERE clause.
For Example: Write the query as
SELECT id, first_name, age FROM student_details WHERE age > 10;
Instead of:
SELECT id, first_name, age FROM student_details WHERE age != 10;
Write the query as
SELECT id, first_name, age FROM student_details WHERE first_name LIKE 'Chan%';
Instead of:
SELECT id, first_name, age FROM student_details WHERE SUBSTR(first_name,1,3) = 'Cha';
Write the query as
SELECT product_id, product_name FROM product WHERE unit_price BETWEEN MAX(unit_price) and MIN(unit_price)
Instead of:
SELECT product_id, product_name FROM product WHERE unit_price >= MAX(unit_price) and unit_price <= MIN(unit_price)
Write the query as
SELECT id, first_name, age FROM student_details WHERE age > 10;
Instead of:
SELECT id, first_name, age FROM student_details WHERE age NOT = 10;
8)  To store large binary objects, first place them in the file system and add the file path in the database.
9) To write queries which provide efficient performance follow the general SQL standard rules.
a) Use single case for all SQL verbs
b) Begin all SQL verbs on a new line
c) Separate all words with a single space
d) Right or left aligning verbs within the initial SQL verb