Pages

Tuesday, December 15, 2015

Oracle to SQLServer Translation

=======================================
SQL Server Create Table As Select Syntax
=======================================
SELECT *
INTO NEW_TABLE
FROM OLD_TABLE
WHERE my_column='AAA'

=======================================
DESC
=======================================
exec sp_columns myTable
exec sp_help myTable

BUT - exec sp_columns dbo.myTable does not work...

=======================================
SQL Server NVL is ISNULL()
=======================================
SELECT product, price, SUM(ISNULL(units_on_order,0)) AS TOTAL_UNITS
FROM Products

=======================================
Concatenate Strings
=======================================
SELECT product+' '+price+' '+CONVERT(VARCHAR(10),SUM(ISNULL(units_on_order,0))) AS TOTAL_UNITS
FROM Products

=======================================
DECODE
=======================================

SELECT CASE customer_type
         WHEN 'B' THEN 'Business'
         ELSE 'Non Business'
       END
FROM CUSTOMER_TABLE 

OR

SELECT CASE 
         WHEN customer_type = 'B' 
           THEN 'Business'
           ELSE 'Non Business'
         END
FROM CUSTOMER_TABLE



=======================================
SYSDATE vs DATEADD()
=======================================
DATEADD has the following format: DATEADD(datepart,number,date)

To get last day entries, use DATEADD(day,-1,getdate())


=======================
Function Based Index
=======================
In SQL Server there is no option to directly create a Function Based Index.
The solution is a two step process:
1. Add Function Column to the table.
2. Create an Index on this new Column.

Example:
ALTER TABLE [MY_TABLE] ADD computed_column AS CONVERT(CHAR(8), [base_column], 112);

CREATE INDEX function_index ON [MY_TABLE](computed_column);

=======================
Data Dictionary in SQL Server
=======================
USER_TABLES in SQL_SERVER
use mySchema
SELECT * 
FROM INFORMATION_SCHEMA.TABLES 
WHERE UPPER(TABLE_NAME) LIKE 'MY_TABLE%';

USER_TAB_COLUMNS in SQL_SERVER
SELECT table_name, column_name
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE UPPER(column_name) LIKE '%SOME_COLUMN%'

USER_SOURCE/ALL_SOURCE in SQL_SERVER
SELECT DISTINCT object_name(id) 
  FROM syscomments with (nolock) 
 WHERE ctext LIKE '%DELETE%';

See All Functions and Procedures per schema.
SELECT * FROM INFORMATION_SCHEMA.ROUTINES;

No comments:

Post a Comment