Removing user from SQL Server database

So, have you ever been in the need to drop a user from the SQL Server database, but you stuck with the following related error?

Error: 15138 The database principal owns a schema in the database, and cannot be dropped.

If you don’t have access to the SSMS to see which schema or objects the user owns, the following SQL should do the job.

USE [DATABASENAME]
GO
SELECT so.name OBJECT, su.name OWNER
FROM sys.schemas so
INNER JOIN sysusers su ON (so.principal_id = su.uid)
WHERE su.name = 'username'
GO
SELECT so.name OBJECT, su.name OWNER, so.xtype TP
FROM sys.sysobjects so
INNER JOIN sysusers su ON (so.uid = su.uid)
WHERE su.name = 'username'
GO

Once you have the owned objects/schemas, you can change them with the following SQL (schema example):

USE [DATABASENAME]
GO
ALTER AUTHORIZATION ON SCHEMA::[db_datareader] TO [newowner] /* new owner username */
ALTER AUTHORIZATION ON SCHEMA::[db_datawriter] TO [newowner]
GO

Then you’re ready to drop the user DROP USER [username].

Bonus SQL: The following will list the schemas and their owner.

SELECT schema_name(schema_id) as SCHEMANAME,
user_name(s.principal_id) as USERNAME
FROM sys.schemas AS s

How to send Telegram messages from SQL Server

Hi folks,
You usually receive notifications from SQLServer at your e-mail, right? A nice way to receive updates from your SQL Server Schedule job, it’s sending them through telegram. To do so, you’ll have to configure one additional step and choose Operating System (CmdExec) as a type of command. And to call the Telegram API, we can use a PowerShell command like the following:

powershell "Invoke-RestMethod -Uri 'https://api.telegram.org/bot{API-KEY}/sendMessage?chat_id={CHAT-ID}&text={TEXT}'"

Just replace the following keys with the proper value (don’t forget to remove the curly brackets too).

API-KEY: Your bot API key
CHAT-ID: The ID of the chat where you’ll receive the message, could be a single user, or a chat group
TEXT: Your beloved message.

What else do you use to achieve this kind of alers? Slack? healthchecks.io? Tell us 😉

GoldenGate: Replicate data from SQLServer to TERADATA – Part 2

This steps should still be performed in SQLserver Host:

The pump process configuration is very simple, its only function is to transport the trail files to destination.

ADD extract P_MSQL, exttrailsource ./dirdat/tr

C:\goldengate> edit param P_MSQL

extract P_MSQL
SOURCEDB db0sql1, USERID ggate, PASSWORD ??????
CACHEMGR CACHESIZE 2GB
rmthost teradata1.net, mgrport 8809
rmttrail ./dirdat/td

--TABLE MAP
TABLE dbo.DLOG_ERRORS;
TABLE dbo.SAC_DATA;
TABLE dbo.SAC_LIST;
TABLE dbo.SAC_TITLE;

Still in the SQLserver Host, is need to create a definition file, wich will be used in gg-teradata.
First, create a “tables.def” file that should contain a dblogin and tables that will be replicated.

defsfile tables_sqlserver.sql purge 
SOURCEDB db0sql1, 
USERID ggate, PASSWORD ?????? 
TABLE dbo.DLOG_ERRORS; 
TABLE dbo.SAC_DATA; 
TABLE dbo.SAC_LIST; 
TABLE dbo.SAC_TITLE;

More“GoldenGate: Replicate data from SQLServer to TERADATA – Part 2”