Wednesday, June 10, 2015

Retrieving all the user created users, roles, and associated permissions in Azure SQL

I have been struggling every time I needed to retrieve all the users, roles, and the associated permisisons that I have created since Azure SQL is a little different from SQL Server I am used to when it comes to user administration. So I came up with this query that I'd like to share:

SELECT p.[name] as 'Principal_Name',
   CASE WHEN p.[type_desc]='SQL_USER' THEN 'User'
   WHEN p.[type_desc]='DATABASE_ROLE' THEN 'Role' END As 'Principal_Type',
   --principals2.[name] as 'Grantor',
   dbpermissions.[state_desc] As 'Permission_Type',
   dbpermissions.[permission_name] As 'Permission',
   CASE WHEN so.[type_desc]='USER_TABLE' THEN 'Table'
   WHEN so.[type_desc]='SQL_STORED_PROCEDURE' THEN 'Stored Proc'
   WHEN so.[type_desc]='VIEW' THEN 'View' END as 'Object_Type',
   so.[Name] as 'Object_Name'
   FROM [sys].[database_permissions] dbpermissions
   LEFT JOIN [sys].[objects] so ON dbpermissions.[major_id] = so.[object_id] 
   LEFT JOIN [sys].[database_principals] p ON dbpermissions.  [grantee_principal_id] = p.[principal_id]
   LEFT JOIN [sys].[database_principals] principals2  ON dbpermissions.[grantor_principal_id] = principals2.[principal_id]
   WHERE p.principal_id > 4


Adding principal_id > 4 ensures removal of dbo, public etc...

Wednesday, May 13, 2015

How to enable the function key on Windows

Enable function key -> Control Panel -> Hardware and Sound (Category) -> Windows Mobility Center -> Adjust Commonly Used Mobility Settings Look Under Function Key Behavior and change the dropdown value to "Function Key" to enable Fn key. To disable it, select "Multimedia Key"

Thursday, April 16, 2015

How to export data into SQL Azure database

  1. From Management Studio right-click on the database you'd like to export data from
  2. Go to Tasks -> Export Data
  3. Select your source database - regular SQL Server 2008 or 2012
  4. For the destination database (your Azure database), select .NET Framework Data Provider for SQL Server
  5. Enter your Azure server name [serverName.database.windows.net], also fill in User ID and Password, Set Encrypt property to True and Integrated Security to False
  6. Click on Next and select the tables you are looking to import into SQL Azure

Monday, May 12, 2014

ORA-01031: insufficient privileges

Error: ORA-01031: insufficient privileges

Details: Occurred when attempting to shutdown the database using SHUTDOWN ABORT while logged in as oracle user.

Solution:
The solution was quite simple: exit sqlplus and login again as sysdba

Sunday, May 11, 2014

ORA-12537: TNS:connection closed - standby error in DGMGRL

Error: ORA-12537: TNS:connection closed

Details: Occurred on a standby database after switchover from primary to standby. Error came up when checking DataGuard broker configuration.

Cause: The mrp process not running on standby
Solution:
Checked if MRP process was running on standby by executing:
ps -ef | grep mrp
Nothing was running.
Performing regular maintenance on standby database fixed the issue
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP NOMOUNT;
SQL> ALTER DATABASE MOUNT STANDBY DATABASE;
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION; 

Thursday, May 8, 2014

Warning: ORA-16826: apply service state is inconsistent with the DelayMins property

Error: Warning: ORA-16826: apply service state is inconsistent with the DelayMins property

Details: Gotten this warning after switching over from primary to standby. All redo logs were applied and no errors showed up in alert log, but DataGuard shown the warning in standby

Cause:
Solution:
When I executed:
SELECT DEST_NAME,DATABASE_MODE,RECOVERY_MODE,PROTECTION_MODE,STATUS FROM V$ARCHIVE_DEST_STATUS
WHERE DATABASE_MODE='MOUNTED-STANDBY';
I'd get:
--------------------------------------------------------------------------
DEST_NAME           DATABASE_MODE   RECOVERY_MODE           PROTECTION_MODE      STATUS
--------------- ----------------------- -------------------- --------------
LOG_ARCHIVE_DEST_3  MOUNTED-STANDBY    MANAGED               MAXIMUM AVAILABILITY VALID
So RECOVERY_MODE came up as "MANAGED", meanwhile it should have been "MANAGED REAL TIME APPLY". Restarting MRP, as it was suggested by every source I could find, did not help and the next solution was to do that with NODELAY keyword:

ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

ALTER DATABASE RECOVER MANAGED STANDBY DATABASE NODELAY;
That did not help either. I waited until next logfile switch but the warning was still there. Finally I decided to remove the standby database and then re-add and re-enable it but gotten an error on the attempt to remove it:
DGMGRL> remove database 'Stby';
Error: ORA-16627: operation disallowed since no standby databases would remain to support protection mode So my last resort was to disable and re-enable Data Broker configuration:
DGMGRL>disable configuration;

DGMDRL>enable configuration;
Then
DGMGRL>show configuration;
finally returned SUCCESS.

Thursday, April 3, 2014

How to fix a DataGuard issue - logs do not ship from primary to standby

One way to check if logs get shipped and applied from primary to standby is by running the following query on both primary and standby databases:
select al.thrd "Thread",
almax "Last Seq Received",
lhmax "Last Seq Applied"
from (select thread# thrd, max(sequence#) almax
from v$archived_log
where resetlogs_change#=(select resetlogs_change# from v$database)
group by thread#) al,
(select thread# thrd, max(sequence#) lhmax
from v$log_history
where first_time=(select max(first_time) from v$log_history)
group by thread#) lh
where al.thrd = lh.thrd;
If there is a discrepancy between primary and standby, one way to fix it is by rolling forward the physical standby using RMAN incremental backups of the primary. Here is a document on how to do that.
On Standby:
  1. Retrieve the current SCN number to be used for backing up database later on
    
    SQL> SELECT to_char(current_scn, '999999999999999999') FROM V$DATABASE;
    
    TO_CHAR(CURRENT_SCN
    -------------------
           XXXXXXXXXXX
    
  2. Cancel the managed recovery on standby
    SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
    

Primary:
Connect to RMAN
rman

RMAN> connect target /

RMAN> BACKUP DEVICE TYPE DISK INCREMENTAL FROM SCN XXXXXXXX DATABASE FORMAT '/tmp/DBNameStandby_%U' tag 'DBNAMESTANDBY';
Standby:
Connect standby via RMAN and then connect to the catalog:

rman 

RMAN>connect target /

RMAN>connect catalog username/pwd@catalogname

RMAN>catalog start with '/tmp/DBNameStandby';

RMAN>REPORT SCHEMA;


RMAN>STARTUP FORCE NOMOUNT;
RMAN>RESTORE STANDBY CONTROLFILE FROM TAG 'DBNAMESTANDBY';
RMAN>ALTER DATABASE MOUNT;
RMAN>RECOVER DATABASE NOREDO;
If error occurs "ORA-19573: cannot obtain exclusive enqueue for datafile 5" when executing RECOVER DATABASE NOREDO; Run ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; via sqlplus on Standby
And finally run the following on the standby database via sqlplus:

SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;