How can I get all the database names in a sql server instance using tsql? - sql-server-2005

How can I get all the database names in a sql server instance using tsql?

SELECT * FROM sys.databases

----SQL SERVER 2005 System Procedures
EXEC sp_databases
EXEC sp_helpdb
----SQL 2000 Method still works in SQL Server 2005
SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases
----SQL SERVER Un-Documented Procedure
EXEC sp_msForEachDB 'PRINT ''?'''
to know more about database : http://blog.sqlauthority.com/2007/05/12/sql-server-2005-list-all-the-database/

this should work on pretty much any version of sql server
USE master;
SELECT NAME FROM sysdatabases;
[edit : it could be SELECT NAME FROM sys.databases too, microsoft's website says both and i'm not on my windows box to test, sorry!]
you could also use (sql 2005 only)
USE master;
EXEC sp_databases;

And for practical use added couple of common filters:
select database_id, [name] database_name
from master.sys.databases
WHERE state <> 6 -- skip offline
AND database_id > 4 -- skip system dbs
AND HAS_DBACCESS([name]) = 1 -- with User Access

SQL 2000
use master
select name from sysdatabases
or
(no need for use master, includes database_size)
exec sp_databases

Related

How to find linked_server name in sql server management studio?

I am trying to query the following in sql server management studio:
select colname from openquery (linked_server, 'exec XX.YY.PrcName #Parameter')
When i try to give XX as linked server name i am getting an error.
Is that right or how do i find the linked_server name ?
To check linked server name :
select * from sys.sysservers
you can easily run any procedure in another server through linked server as:
exec <procedure name><parameneters> at <linked server name>
To get a list of linked servers use:
EXEC SP_LINKEDSERVERS
If you want to SELECT something from Linked server:
SELECT *
FROM [SOMESERVER\SOMEINSTANCE].somedatabase.dbo.sometable;
To EXECUTE something on linked server:
EXEC [SOMESERVER\SOMEINSTANCE].somedatabase.dbo.somestoredprocedure
SELECT *
FROM OPENQUERY([SOMESERVER\SOMEINSTANCE].somedatabase.dbo.somestoredprocedure)
More info:
sqlauthority
MSDN
You also can use sys.servers
Select * From SYS.SERVERS
When server_id = 0, this is the server name.
When server_id >0 , this is the local name of linked server.

How to INSERT INTO SQLite with SELECT FROM SQL Server

I want to SELECT a table in a SQL Server database and then INSERT it into a local SQLite database.
IS it possible to do this entirely with a query in the vein of:
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
but passing connection information?
Setup a local odbc data source, DSN, for SQLite file. Call it SQLite_DataSource
Then setup a linked server in SSMS:
USE [master]
GO
EXEC sp_addlinkedserver
#server = 'SQLite', -- the name you give the server in SSMS
#srvproduct = '', -- Can be blank but not NULL
#provider = 'MSDASQL',
#datasrc = 'SQLite_DataSource'
GO
Then you can use it like any other db:
INSERT into SQLite_DataSource (column1)
SELECT col1
FROM table2
Lookup SQL Server's documentation for sp_addlinkedserver for more details.
You need to create a SQL Server Linked Server to SQLite, take a look at this post for more information .

How to get ID of a newly created SQL Server database

Say, I run the following statement to create a new SQL Server database:
CREATE DATABASE [testdb1] COLLATE SQL_Latin1_General_CP1_CI_AS;
How do I get it's ID?
SELECT database_id FROM sys.databases WHERE name = N'testdb1';
select DB_ID (N'testdb1')
Source

How do you reference a second SQL Server

How can I reference a second server in SQL.
SELECT A.datasetid,
A.dsdate,
B.datasetid AS Expr1,
B.dsdate AS Expr2
FROM we_ci_db.tblopportunitydatasets AS A
INNER JOIN we_ci_db.tblopportunitydatasets AS B
ON A.datasetid = B.datasetid
Assume that table 'B' is on a different server, what would the syntax be. I've tried putting the server name before the schema, but it doesn't recognise it
You must configure a linked server. Once that is configured, the linked server may be referenced as
server.database.schema.object
You'll first need to create a Linked Server by running the addlinkedserver stored procedure:
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver
#server = N'SRVR002\ACCTG',
#srvproduct=N'SQL Server' ;
GO
After that, you can refer to the linked server with the syntax:
select *
from [SRVR002\ACCTG].[database name].[owner name].[table name]
More Info

SQL Server: how to use the same procedure in different instances?

I have a stored procedure in a test instance of a sql server (named sql2008test\sql2008_test) and it is located in one database. I would like to use this procedure also in production instance (named sql2008prod\sql2008_prod). Should I copy this procedure into this prod instance or can I modify my procedure somehow to query data from prod instance? The procedure is shown below.
CREATE PROCEDURE dbo.PROC_getDbInfo
AS
SET NOCOUNT ON
TRUNCATE TABLE dbo.dbinfo
EXECUTE sp_msforeachdb 'insert into dbo.dbinfo
select ''?'' as name,
type_desc,
physical_name,
state_desc,
size * 1.0/128 as size_in_mb,
max_size,
growth * 1.0/128 as growth_in_mb,
is_percent_growth,
is_read_only
from [?].sys.database_files'
SELECT ##SERVERNAME as instance_name,
f.name,
d.create_date,
d.compatibility_level,
d.collation_name,
d.user_access_desc,
d.state_desc,
d.recovery_model_desc,
d.page_verify_option_desc,
d.log_reuse_wait_desc,
f.type_desc,
f.physical_name,
f.state_desc,
f.size_in_mb,
f.max_size,
f.growth_in_mb,
f.is_percent_growth,
f.is_read_only
FROM dbo.dbinfo AS f INNER JOIN
sys.databases AS d
ON f.name = d.name
ORDER BY f.name
GO
Run the creation script on your production instance (of course, going through any release processes you have). You could set up linked servers, if you can access your production server from your test instance, but I wouldn't head down that route without a good reason.
BTW, you still have the extra GO statements in that script meaning the sproc will ONLY be created with SET NOCOUNT ON in and won't do anything.
Use Server Objects>Linked Servers so you can select between different instances.
Copy your procedure into Production db.