Sys - Databases and Sys - Sysdatabases
Sys - Databases and Sys - Sysdatabases
Sys - Databases and Sys - Sysdatabases
sysdatabases
Just wanted to write a short note on what is the difference between sys.Databases and
sys.sysDatabases. Well, both of them return some metadata related to databases hosted
on the instance. However, sys.Databases returns 56 columns and sys.sysDatabases
returns 12 columns. But why? whats the difference?
SQL Server 2005 introduced System Catalog Views, the preferred way of working with SQL
Server meta data. sys.Databases is a system catalog view and there are a number of such
views under the sys schema. sys.sysDatabases, howeever, are like the old meta data
tables that existed in SQL Server 2000 which is still there for backword compatibility
reasons. These are again views but return much less information compared to catalog
views (12 columns vs 56 columns). These are called as compatibility views.
Catalog views exist in every user database under the Views -> System Views node and
there a dozens of them allowing you to work with every type of metadata. They all exist
under sys schema and need to be addressed with the schema. SO if you run select * from
Databases (without prefixing sys schema), you will get an error. On the other hand,
sys.sysDatabases can be referred as sysDatabases (without prefixing any schema) or
dbo.sysDatabases. Simply becuase you might have a number of scripts referring to them in
that way written for SQL Server 2000 so nothing breaks.
It is preferred that you work with new system catalog views (not really new though) you
can observe that there is almost one column in sys.Databases for every database option
First question: If the IPALL TCP Dynmaic Ports setting is a specific number (say 1971) does
that signify that you have a static port of 1971 or a dynamic port that is currently 1971 and
may change at some point in the future.
Second question: This is the one I'm most curious about. We have an instance that has
had the same port (the value in the IPALL TCP Dynmaic Ports setting) for several years
through dozens of instance restarts. What actually causes the dynamic port to actually
change after an instance restart?
Dynamic Port Allocation
If you configure an instance of SQL Server to use dynamic port allocation, and you have not
yet restarted the instance of SQL Server, the registry values are set as follows:
TCPDynamicPorts = Blank
TCPPort = 0
However, if you configure an instance of SQL Server to use dynamic port allocation, and
you restart the instance of SQL Server, the registry values are set as follows:
TCPDynamicPorts = Current port that is used
Connection Problem
So let me give you step-by-step approach in troubleshooting connectivity issues with SQL Server.
Here is a flow of how connection to SQL Server is made:
1. Protocol used for making connections to SQL Server
Connecting is made using a protocol based on the "Client Protocols" order specified in your local box.
In the case of screenshot given above, the connection made to any SQL Server from this client machine will use
"Shared Memory" protocol first and if that connection fails, it will try to use "TCP/IP" protocol and if that fails,
connection request will be made using "Named Pipes" protocol. This is because I have all three protocols "Enabled"
and I have specified the order in this way.
Shared Memory protocol can be used only for local server connections whereby SQL Server should be running in the
same box where you are trying connect. This protocol will not help you to connect to a Clustered SQL Server instance
because the IP address of SQL Server is different from your local node.
You can also force to use specific protocol using syntax:
TCP:SQLSRVRNAME\INSTANCE for forcing connections to use TCP/IP protocol
NP:SQLSRVNAME\INSTANCE for forcing connections to use Named Pipe protocol instead you can also
use\\.\pipe\instancename\sql\query
LPC:SQLSRVNAME\INSTANCE for forcing connections to use Shared Memory protocol. LPC stands for Local
Procedure Call.
If you force these protocols, then connectivity will happen only using specific protocol and if that fails, connection will
fail without trying with further protocols. If you are connecting using a SQL Server alias created in the local client, then
the protocol specified in the alias will only be used.
Once authorization completes, you can submit your queries to execute in the SQL Server instance.
So by now, hope you have the complete flow of how connectivity happens from a client to SQL Server.
Here are some Troubleshooting tips:
1. To make sure SQL Server Browser is able to start (Port 1434 is available for SQL Server Browser to grab), you can
try start SQL Server Browser from command line :
C:\Program Files\Microsoft SQL Server\90\Shared>sqlbrowser.exe -c
You should see something like:
SQLBrowser: starting up in console mode
SQLBrowser: starting up SSRP redirection service
SQLBrowser is successfully listening on ::[1434]
SQLBrowser is successfully listening on 0.0.0.0[1434]
[9088]: Waiting for next request...
This step will ensure that SQL Browser works fine. If you are able to start SQL Server Browser from command line
and not as a service, check the service account configured for SQL Server Browser. To shutdown SQL Browser
started from command line, you have to press CTRL + C.
2. If SQL Server Browser failed to start, run TCPView from http://technet.microsoft.com/enus/sysinternals/bb897437 to make sure that no other process is already listening on 1434 UDP. For example if you
have SQL Server 2000 already installed on the same node where SQL Server Browser fails to come online, it could
be very well because SQL 2000 SSRP service already listening on 1434 and TCPView will help you to troubleshoot
this.
3. If connectivity to SQL Server instance fails, first thing to check is whether it is a Clustered SQL Server instance or a
Standalone.
If it is a clustered, try connecting from Active node of SQL Server to the SQL Server Instance. If this works then it
could be a firewall blocking connections from passive node and other clients.
To confirm that firewall is blocking the connectivity, use PortQry
fromhttp://www.microsoft.com/downloads/en/details.aspx?FamilyID=8355e537-1ea6-4569-aabbf248f4bd91d0&displaylang=en
Here is a sample output when I connect to a server name SQLMOSS (This should be a node name if it is a
standalone instance and virtual server name if it is clustered instance)
=============================================
Starting portqry.exe -n SQLMOSS -e 1434 -p UDP ...
Querying target system called: SQLMOSS
Attempting to resolve name to IP address...
Name resolved to 192.168.100.3
querying...
UDP port 1434 (ms-sql-m service): LISTENING or FILTERED
Sending SQL Server query to UDP port 1434...
Server's response:
ServerName SQLMOSS
InstanceName MSSQLSERVER
IsClustered No
Version 9.00.4035.00
tcp 1433
==== End of SQL Server query response ====
UDP port 1434 is LISTENING
portqry.exe -n SQLMOSS -e 1434 -p UDP exits with return code 0x00000000.
If you don't get any instance names returned in the result set, it should be firewall blocking the traffic. It will be a good
step to stop Firewall service and give it a try. If you are interested in adding Exception to firewall, add exception for
Port 1434-UDP for SQL Browser and Port xxxx-TCP for SQL Server. You can get the port in which SQL Server is
listening from SQL Server Errorlog.
If you don't have permission to install/download PortQry, try using telnet program available inbuilt in Windows (For
Win > 2008, you need to add Telnet Client Feature from Server Manager). If SQL Server is listening on port 1488
then when I run telnet SQLMOSS 1448 from command prompt, I got a blank screen like below which indicates that
SQL Server is listening to my request:
If this step fails, contact your system administrator with this data so that he can check the firewall logs.
http://support.microsoft.com/kb/887057
it is a good idea to learn how to use PSSDIAG anyways because if you need to open a
case with Microsoft PSS, they will ask you to use it to collect info.
Learn which items can be cleared to avoid overly large data collection
3. General information
exec xp_msver
GO
exec sp_configure
GO
provide the results of each
4a. Slowness not attributed to System CPU/disks or SQL queries
Was a SQL data/log file growth involved, I usually don't look for this
Check the SQL Server error logs, usually in the directory:
C rogram FilesMicrosoft SQL ServerMSSQLLog
Look at the ERRORLOG.X and SQLDumpXXXX.txt files
if there are indications of errors generated by the SQL Server engine itself, it is a good
idea to open a case with Microsoft PSS
4b. Mysterious slow down
-If your system runs well initially, but over time becomes horribly slow over time
-Intermitent severe slow downs that clear with no obvious reason
with a corresponding drop in the perf counter: Process->Sqlserv->Virtual Memory
-And the problem cannot be traced to CPU, disk or network, bad query plans, compiles
etc
-Queries with low CPU that normally run quickly, now run slow (but cpu is still low)
-Sysprocesses show many queries with wait time, but eventually going through
-The SQL logs show messages like:
reserve contiguous memory of Size=65536 or 131072 bytes failed
(this could be any power of 2, but failure to allocate 4-8MB is probably not a severe
problem
-A restart of SQL Server clears this problem or a while
What
When you use the SchemaBinding keyword while creating
a view or function youbind the structure of any underlying tables or views. So what
does that mean? It means that as long as that schemabound object exists as a
schemabound object (ie you dont remove schemabinding) you are limited in changes
that can be made to the tables or views that it refers to.
That still sounds a bit confusing. This may be easier with an example (I like examples).
1
2
FROM Bound.Table2
10
JOIN Bound.Table3
ON Table2.Id = Table3.Id;
11
12
GO
So Ive created three tables and two views under the schema Bound. The first view is
unbound and references Table1 and the second view references tables Table2 and
Table3 and is schemabound. I do want to point out a couple of things.
Because I created all the objects under the CREATE statement for the schema
they are all contained within that schema.
When creating a schemabound view or function you have to use the two part
name (include the schema name) for any tables or views you reference within it.
GO
This one works fine. Bound.Table1.Col2 is not referenced by any schemabound objects.
GO
ALTER TABLE Bound.Table2 ADD CONSTRAINT df_Tb2_Col1 DEFAULT 'A' FOR Col1
You can not change the collation of a database with schemabound objects.
You can not run sp_refreshview on a schemabound view. You do get a rather
unhelpful error though.
You can make any change to the table that do not affect the structure of the
bound columns.
Objects that are bound (tables/views) can not be dropped while a schemabound
object references them
Why
Schemabinding isnt a commonly used tool unless you are setting up an indexed
view and then it can get lost in the crowd of other required restrictions. It does have
uses outside of indexed views however. I could see using it if there is a mission critical
view/function that just CANT break. By including the SCHEMABINDING clause you
protect the view/function from unexpected changes to the tables underneath them. In
fact if all of the data access in an application is through views and TVFs I might consider
schemabinding all of them. It might be less important in a small shop with only a
couple of developers and/or DBAs where everyone knows what changes are being made
and what effect they will have. However if you are in a big shop with dozens of
applications, may of which use the same databases, you can easily make a change to a
table that breaks code in another application that you were completely unaware of.
So in the end SCHEMABINDING isnt a world changing clause but still one that you
should be aware of.