Remove SQL Server Managed Backup Entry - sql-server

Is there a way to remove a SQL Server Managed Backup entry entirely? (Using Microsoft SQL Server 2017 Enterprise Edition)
We have a database that was renamed (the database name, not the files). Managed Backup configuration was setup again against the new database name. We now have two entries for the same db_guid that show up when we execute:
select * from managed_backup.fn_backup_db_config (null)
db_name
db_guid
...
is_managed_backup_enabled
DB1
e3ff68..
1
DB2
e3ff68..
1
In this example DB1 and DB2 are the same database, DB1 was renamed to DB2.
Now trying to disable the backup for DB1 using:
EXEC msdb.managed_backup.sp_backup_config_basic
#enable_backup = 0,
#database_name = 'DB1'
We get the error: SQL Server Managed Backup to Microsoft Azure cannot configure the database, 'DB1', because it either does not exist or is offline.
We tried creating a new database called DB1, configuring the managed backup to disable the backups, but that's only exacerbated the problem. We now have a new entry in the managed databases with a different db_guid:
db_name
db_guid
...
is_managed_backup_enabled
DB1
e3ff68..
1
DB2
e3ff68..
1
DB1
8ee1ca..
1
Upon dropping the 'new' DB1, it's disappeared from the managed backup.
How do I remove the old duplicate DB1 entry though (without dropping the database which we don't want to do).

delete d
FROM msdb.[dbo].[autoadmin_managed_databases] d
where d.drop_date is not null

Related

SQL Server: Backup location

I am working on a SQL Server database and ran the following command to figure out where the database is backing up to:
SELECT TOP 1 M.physical_device_name
FROM msdb..backupset S
JOIN msdb..backupmediafamily M ON M.media_set_id=S.media_set_id
WHERE S.database_name = 'MyDB'
The physical_device_name says: HTSQLServerMyDB
I don't see any maintenance plans or jobs that I can figure out that do this backup. I am trying to figure out where it is doing this backup. If I go into Server Objects then Backup Devices it is empty.
How can I determine where are my backups?
I am running SQL Server 2016 (13.0.4001.0).

Copy Oracle table to SQL Server

I need to copy data from a linked server Oracle table and append to SQL Server table in a regular time interval.
Without using SSIS or any external etl, can this be done by using open query? I have read in forums that the table has to be exported to csv and then imported back again into SQL Server . Is there an alternative as this will be continuous process ?
Certainly. As long as you've configured the Linked Server properly within SQL Server, and your user account has the right permissions in Oracle, getting and sending data between the two is fairly trivial.
Selecting data from Oracle into SQL Server:
IF OBJECT_ID('tempdb..#myTemp') IS NOT NULL DROP TABLE #myTemp
SELECT *
INTO #myTemp
FROM OPENQUERY(MyLinkedServer, 'SELECT col1, col2
FROM OracleTableName
WHERE SomeColumn = ''Human Resources''')
Inserting data from SQL Server into Oracle:
-- note: please make sure you have your columns in the exact same order!
INSERT INTO OPENQUERY(MyLinkedServer, 'SELECT col1
FROM OracleTableName ')
SELECT myPK
FROM AdventureWorks2014.dbo.SomeTable
And you can even drop or recreate a table in Oracle from SQL Server, and run stored procedures.
To create a linked server, I use the below script and these settings:
-- make sure you have Oracle drivers on your SQL Server, and an up-to-date TNSNames.ora file
EXEC master.dbo.sp_addlinkedserver #server = N'MyLinkedServer', #srvproduct=N'Oracle', #provider=N'OraOLEDB.Oracle', #datasrc=N'TNS_NAME_HERE'
-- disable any logins not explicitly mapped
EXEC master.dbo.sp_droplinkedsrvlogin #rmtsrvname = N'MyLinkedServer', #locallogin = NULL
-- login mapping between SQL Server user and their appropriate Oracle account
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'MyLinkedServer',#useself=N'False',#locallogin=N'domain\username',#rmtuser=N'ORACLE_USERNAME',#rmtpassword='ORACLE_PASSWORD'
-- add a mapping for your SQL Agent account if these will run under jobs,
-- and your SQL Engine account if you still get errors on things that are automated,
-- unless those automated jobs are explicitly running under user accounts
Honestly, the hardest part is making sure you get Oracle drivers installed right on the SQL Server. After that, it's very straightforward.

Restoring error database in use in SQL Server 2008 R2

I want to restore a database with this query, but an error occurs telling that the database is in use.
What should I do?
This is my code:
RESTORE DATABASE db_languageLab
FROM DISK = N'E:\E\db_languageLab_1394_8_15.bak'
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
Database is 'in use' error occurs when some other session/user is connected. Looking simple? But not.
Whenever this happens, I just restart my database service, from services, or by right click on server name in object explorer.
But some times, it doesn't work. I realized after striking head several times that I set my database as default. By default 'master' is your default database. Restoring database process uses default database to restore you own database, so if you have same database as default, it will always pop the same 'in use' error.
To set master back to default (if your current db is set to default mistakenly or by need) run the following command
Exec sp_defaultdb #loginame='sa', #defdb='master'
Now it will have 'master' database to 'plan and process' restoring for 'YourDB'.

Update SAS 9.1 Program to Connect to SQL Server DB instead of Access mdb

At my job there's a SAS script that connects to a Microsoft Access database to retrieve data to a flat file. I've been tasked with adapting it to connect to a SQL Server database instead. We have SAS 9.1 and I cannot update the version because that's not my decision. Here's the code that connects to the Access database (I've replaced the server name and folder names with generic tags for this post):
proc import out=SPONSOR
datatable="SPO_LOAD_UNBLIND"
dbms=ACCESS2000 replace;
database="\\<Server>\<Folder>\<Subfolder>\PROCESS.mdb";
memosize=2048;
run;
What do I need to do to connect this to a SQL Server database named "DM_C0000" with the same table name as in the Access database ("SPO_LOAD_UNBLIND")?
This ended up working:
proc sql;
connect to odbc (dsn=<dsn> user=<userid> pwd=<password>);
create table SPONSOR as select * from connection to odbc(select * from <database>.<schema>.<table>);
quit ;
Alternatively, you can connect to all current tables of a SQL Server database with a libname via ODBC.
* WITH DSN;
libname mydata odbc datasrc="DSN Name" user="username" password="password";
* WITH DRIVER (change driver name to your current installation);
libname mydata odbc complete="driver=SQL Server; Server=servername;
user=username; pwd=password; database=databasename;";
Just be aware changes to tables in SAS affects live database tables. Additionally, deleting entire library deletes all tables in database (not database itself). Instead, be sure to unassign libname.

Cannot get Linked Servers to work in Sql Azure

We are using a trial version of Azure. We are trying to perform cross server queries from our SQL Server 2012 in-house.
We seem to have our local 2012 linked with Azure. When I go into Server Object -> Linked Servers in management studio, I see our Azure database.
But if I try to open the catalog and tables, I get an error message saying
Reference to database and/or server name in 'Perseus.sys.sp_tables_rowset2' is not supported in this version of SQL Server
** Perseus is the name of our catalog in Azure Sql.
Running a query from local connection:
SELECT * FROM [azureDBServer].[Perseus].[dbo].[accounts]
Tesult is:
OLE DB provider "SQLNCLI11" for linked server "azureDBServer" returned message
"Unspecified error". Msg 40515, Level 16, State 2, Line 1 Reference to database and/or
server name in 'Perseus.sys.sp_tables_info_90_rowset' is not supported in this version of
SQL Server.
This same in house SQL 2012 Server is able to connect to our in-house 2008 by cross server queries and by viewing its structure through Linked Servers.
I know from this article Azure supports Linked Servers.
So I'm lost about what is wrong. Our Admin thinks it may be that we have a Web-Sql account vs a business SQL account. This Azure Web vs Business SQL outdated Stack link implies that SQL version is NOT the problem, but pre-dates when Azure offered Linked Servers.
So, I'm trying to understand if
a) we didn't set up something right to provide SQL Linking?
b) we are limited by trial?
c) are we limited by Web SQL version?
d) anything else?
Need to execute below mentioned three stored procedures to add SQL Azure. Using below these stored procedure I was able to query SQL azure.
EXEC sp_addlinkedserver
#server='PROD',
#srvproduct='',
#provider='sqlncli',
#datasrc='azureserver.database.windows.net',
#location='',
#provstr='',
#catalog='database name'
EXEC sp_addlinkedsrvlogin
#rmtsrvname = 'PROD',
#useself = 'false',
#rmtuser = 'Azure login',
#rmtpassword = 'password'
EXEC sp_serveroption 'PROD', 'rpc out', true
While adding linked server from SQL Management, you are not given option to set default database. So use something like below
EXEC sp_addlinkedserver
#server='name for referring locally', -- here you can specify the name of the linked server
#srvproduct='',
#provider='sqlncli', -- using SQL Server native client
#datasrc='AzureMachineName.database.windows.net', -- add here your server name
#location='',
#provstr='',
#catalog='yourdatabasename'
I figured this works.
Did you actually setup connection to perseus database? By looking at the error message your are sending a query with 3 part or 4 part name to Azure which doesn't work as is in Azure. Please check your query and set it to use 2 part name and only three part name if it is connecting to the same database
This works for me:
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'mypassword';
CREATE DATABASE SCOPED CREDENTIAL MySecurity
WITH IDENTITY = 'mylogin',
SECRET = 'mypassword';
CREATE EXTERNAL DATA SOURCE MyDbAccess
WITH (
TYPE=RDBMS,
LOCATION='server name',
DATABASE_NAME='db_name',
CREDENTIAL= MySecurity);
CREATE EXTERNAL TABLE MyExtTable (
[Id] [int] NOT NULL,
[Name] [varchar(20)] NULL)
WITH
(DATA_SOURCE = MyDbAccess);
After that you can just use it:
SELECT * FROM MyExtTable
I got this error and my issue ended up being a type in the database name SERVER.Database.dbo.tableName.

Resources