Check database access (SQL Server 2005 / 2008) - sql-server

What is the best way to check database access from remote machine.
Here is the situation:
SQL Server 2005 (or 2008) is running on MACHINE1 (Windows 2003 or 2008). There is a database called ENTERPRISEDB.
Another server, called MACHINE2 (Windows 2003 or 2008). It has ODBC connection to MACHINE1.
Now, what is the best and simplest SQL query to check if MACHINE2 can connect and access the ENTERPRISEDB in MACHINE1?
Thanks.

If your ODBC connection is to database ENTERPRISEDB then you could run
SELECT top 1 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'
which will return "1" if the database exists or nothing if it doesn't.
If you need a boolean/int return then try
SELECT case when COUNT(*) >0 then 1 else 0 end
FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'
which will return "1" if it exists, "0" if it doesn't.

I always use
Select ##servername, ##SERVICENAME
Returns Server Name, Instance Name to check basic server connection. To check database connectivity itself is correct you can't beat sys.objects.
select DB_NAME(), * from sys.objects
Returns a table with current database name as the first column and a list of object names as the general data list.

Related

Linked servers (MySQL in SQL Server) brings tables without columns

I have an instance running SQL Server 2014 Express Edition (64-bit Build 19044). I have the same machine running a MySQL server (innodb_version: 5.7.33, protocol version: 10, version_compile_machine: x86_64, version_compile_os: Win64), and I need to write some information in this MySQL Server through a SQL Server script (I need to connect multiple servers and I would like to center everything in this SQL Server. This MySQL server I'm talking about is one of them).
I've found many tutorials teaching to use linked servers, like the following: https://gunnarpeipman.com/mssql-mysql-linked-server/
I've configured the ODBC connection as a System DSN using both MySQL ODBC 8.0 Unicode Driver and ANSI Driver;
I could connect to the MySQL server, and list the tables in this server through SSMS's object explorer. Unfortunately, I spent A LOT of time trying to query data from this server, since I always get errors pointing as if I don't have enough permission to see columns from this table:
When trying to [right click table > Script table as > Select to > clipboard]
[DWGLUO].[dw_vtiger]..[dleads] contains no columns that can be selected or the current user does not have permissions on that object.
When trying to query some data with OPENQUERY:
SELECT * FROM OPENQUERY(DWGLUO_ANSI,'SELECT idlead FROM DWGLUO_ANSI.dw_vtiger..dleads')
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT idlead FROM DWGLUO_ANSI.dw_vtiger..dleads" for execution against OLE DB provider "MSDASQL" for linked server "DWGLUO_ANSI".
I can see all tables, but i can't see any columns :(
Also, by running the query below, I managed to find the column names, so they're there somewhere:
EXEC ('SELECT TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ''dleads''') AT DWGLUO_ANSI ;
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
dw_vtiger
dleads
idlead
dw_vtiger
dleads
nome
... (continues) ...
If someone could help me I would really appreciate it. Thank you all in advance.

SQL Server : double hop linked server running SELECT Query

I'm quite new to SQL Server. I have a question: I have 3 SQL servers, 2 on domain Domain1 and 1 on Domain2.
SQL Server 1 & 2 are on Domain1
SQL Server_3 is on Domain2.
SQL Server_1 - linked to SQL Server_2 and SQL Server 2 is linked to SQL Server 3.
My Windows authentication is Admin on all Windows servers and SQL Servers across all 3 servers. I'm trying to run a SELECT query on SQLServer_1 to retrieve data from SQLServer_3 using SQLServer_2 as a middle jump point (connecting directly between SQLServer_1 and SQLServer_3 unfortunately is out of options)
I've tried running these query on SQLServer_1 :
SELECT *
FROM [SQLServer_2].[SQLServer_3].[DBName_1].[Table_Name1]
But I get this error:
The object name 'SQLServer_2.SQLServer_3.DBName_1.Table_Name1' contains more than the maximum number of prefixes. The maximum is 3.
SELECT *
FROM [SQLServer_3].[DBName_1].[Table_Name1]
results in:
Could not find server 'SQLServer_3' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
So both didn't work.
Please HELP. Is it even possible?
Thank you ZLK 23. He's suggestion worked. I dont know how to post it
as an answer if any one does please help me out.
Perhaps use OPENQUERY on the second server to run a query on the third? e.g. on server 1 select * from openquery(server2, 'select * from server3.database.schema.table;'); Alternatively, you might consider executing a stored procedure on server2 (that connects to server3) from server1. – ZLK 23 hours ago
I ran this query from SQLServer_1. In my understanding it runs query
SELECT on SQLServer_2 the SELECT QUERY in the second parameter and
retrieves the results back. Which has worked perfect for me.
SELECT * FROM OPENQUERY(SQLSERVER_2,'SELECT * FROM 'SQLSERVER_3.DBNAME.TABLENAME')
Thank you again.

Data visibility between linked server

Please consider the following situation
I have Prod instance of SQL server 2012
Also Archive instance of SQL server express 2012
Prod sees Archive as linked server and is able to write data with similar query from some .net code that creates a transaction and the transaction is committed at the end.
insert into <ArchiveServer>.<database>.<schema>.<table>
Select * from <ProductionServer>.<database>.<schema>.<table> Where <some conditions>
Now after the transaction finishes I am able to execute the following query
select count(1) from <ArchiveServer>.<database>.<schema>.<table>
and it returns correct number of records in the context of production server.
Same query
select count(1) from <database>.<schema>.<table>
in the context of Archive server returned 0 records.
What might be the problem? I am out of clues.
Thanks

SQL Server Management Studio Express not showing recently added data

I recently installed and have been using Microsoft SQL Server Management Studio Express 9.00.4035.00 to manage a remote database on my hosts' SQL Server 2005.
Until now I had been using EMS SQL Manager 2011 Lite and that was working fine.
2 new rows where inserted into one of my tables this morning; one by a customer signing up for a service and the other as a test signup by me.
When I run a typical select query:
[Select top 20 * From tblNotary Order By ID Desc]
I don't see the the 2 most recent rows. But when I run the same query from EMS SQL Manager Lite I see the records.
I also verified when connecting using MS Access 2010 I see the 2 new rows in the table. I have checked and double-checked the connection settings and they match EMS.
Is there a setting or something obvious I am missing? Why can't I see the most recent record insertions? I am on a Windows 7 desktop machine.
The most likely reason is you are connecting to a different database than you expected. You can select ##servername to verify both queries are running against the same server.
If the records are stil being inserted as part of an open transaction and have not been committed, they are called "phantom" rows. You will not see phantom rows if your query runs at transaction isolation level read committed or higher. It may be that EMS SQL Manager Lite is running at read uncommitted, in which case it will include phantom rows in the query.
Sometimes connection strings are a tricky thing. Run this to double check:
select ##servername servername, name, crdate
from sys.sysdatabases
where name = db_name()
if you have an issue running that, just do a simpler version:
select ##servername, db_name(), ##version
Select top 20 * From tblNotary Order By ID Desc

On SQL Server 2008, how to find out when was any database offline/online?

I have to include one report in my application showing offline/online activity of few databases on SQL Server 2008.
Could you please suggest how can I collect teh same information from sql server?
SELECT DATABASEPROPERTYEX('YOURDATABASE', 'Status')
DatabaseStatus_DATABASEPROPERTYEX
GO
SELECT state_desc DatabaseStatus_sysDatabase
FROM sys.databases
WHERE name = 'YOURDATABASE'
GO
This will tell you the status of the database.
In order to find out when your database was taken OFFLINE, you can use the SQL that I posted before, or the easiest way is to check the Event Viewer and it will tell you when the Database was taken OFFLINE. I have just tested this on my local machine and SQL Server writes out an Information message to the Application log.
You can also use below query to check database status.
SELECT Name, state_desc FROM sys.databases

Resources