Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Tuesday, June 29, 2010

Write Mysql output to file

This is what i use to write the SQL output to a filedrive:

SELECT 1 FROM DUAL INTO OUTFILE '\\\\\192.168.0.13\\Divers\\demo.sql';

Note: you can write only once like this, otherwise you get message that the file exists. :-)

 

This example shows how to create dynamic name:

CREATE PROCEDURE export_dynamic(IN file_name CHAR(64))
BEGIN
SET @myvar = CONCAT('SELECT * INTO OUTFILE ',"'",file_name,"'",' FROM Table1') ;
PREPARE stmt1 FROM @myvar;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END;

Tuesday, February 12, 2008

Handy stuff when you work with MS SQL Database

GENERAL REMARK:
SQL SERVER IS IN MOST CASES INSTALLED CASE INSENSITIVE
IF INSTALLED CASE SENSITIVE USE UCASE('YOUR_VALUE')


The select for finding all Stored (P) Procedures, (TR) Triggers, (V) Views, (FN) scalar functions, (TF) Table valued Functions, (IF) inline table valued functions, (RF) replication filter procedures and (R) rules where you use some specific text:
SELECT *
FROM sys.sql_modules m INNER JOIN
sys.objects o
ON m.object_id = o.object_id
WHERE definition LIKE '%YOUR_VALUE%';



Statement that returns all tables and views where column is used:



SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'YOUR_VALUE';




Select statement for finding the indexes use on some Column name:



SELECT *
from sys.index_columns ic INNER JOIN
sys.columns o
ON ic.object_id = o.object_id AND ic.column_id = o.column_id INNER JOIN
sys.tables t
ON ic.object_id = t.object_id INNER JOIN
sys.indexes i
ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE o.name = 'YOUR_VALUE'



Select statement for finding constraints use on some column name:

SELECT *
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE COLUMN_NAME = 'YOUR_VALUE';



Monday, February 11, 2008

Handy stuff when you work with oracle Database

When a database is so big and you do not now where to start, here are some helpfull selectstatements.

Let oracle search it for you.

Here are some usefull Selects:

  • The select for finding all Stored Procedures where you use some specific text
    select * from all_source where Upper(text) like Upper('%YOUR_VALUE_TO_SEARCH%');

  • Select statement where you use the column name in a table
    select * from all_tab_columns where column_name = Upper('YOUR_VALUE_TO_SEARCH');

  • Selectstatement for finding the indexes use on some Column name
    select * from all_ind_columns where column_name = Upper('YOUR_VALUE');

  • Select statement for finding constraints use on some column name
    select * from all_cons_columns where column_name = Upper('YOUR_VALUE');

Performace Tips:

  • Find the record count , the fastest way
    Select Count(0) from TABLENAME;

  • Left join is slower against Inner join.

Reason:
When you use * it uses a varchar, when you use a number is it faster. there is no cast form varchar to Integer

  • Update TableName set Columname = 1 where CMD_ID in (Select CMD_ID from TableName)
    the updatestatement above works but slow, use this instead:
    Update TableName1 set Columname = 1
    where exists (
    Select 1 from TableName2
    where TableName1.CMDID = TableName2.CMDID
    )

  • Write a select statement that works faster
    Select id, name, firstname, function
    from functionlist FL
    inner join person P on P.personid = FL.id
    inner join functions F on F.functionid = FL.idfunction

  • If table contains something like
    Select 1 from TableName where rownum < 2
    The database stops searching when it finds the first item.

Oracle Version

  • select * from v$version
  • select * from product_component_version;

Tuesday, January 29, 2008

Cleaning up transaction log from database

I had a problem with cleaning up logfiles from some database.
But when the script does not work you have to search an other way.

I found a solution to clean up the logfiles, it's not a neat one but it works.

Follow these 3 steps:
  • Detach the database
  • rename the LDF
  • Attach again.
    It will alert you to the fact that there's no log file, and will create a fresh one for you.
    (The log file has shrunk from 1Gb to 700k)