In SQL Server, like use <<DatabaseName>>, how to give use <<ServerName>> query command?
Right Click | Change Connection must be associated to some command. Isn't it. Just curious..
If you are attempting to fully switch connections1 within a single script and work with a new connection as opposed to sharing data between connections, then you can do this with SQLCMD mode. This ability / feature is specific to SQL Server Management Studio (SSMS) and the SQLCMD.EXE command-line utility as SQLCMD mode is directives to the client tool (either SSMS or SQLCMD.EXE) and not something that the database engine will be executing (or will even see or ever know about).
You can enable SQLCMD mode in SSMS on a per-session basis by going to the Query menu and selecting SQLCMD Mode. Once SQLCMD mode is enabled, you can change connections be using the :connect command:
SELECT ##SERVERNAME AS [ServerName], DB_NAME() AS [DbName]
GO
:connect DifferentServerName
SELECT ##SERVERNAME AS [ServerName], DB_NAME() AS [DbName]
Notes:
SQLCMD directives are processed at the beginning of each batch (since the entire batch is then sent to SQL Server for execution). Without using GO to separate the batches in the above example, the :connect command will take effect before the first SELECT. Comment out the GO and run again to see the effect.
When using SQLCMD Mode, Intellisense will not work.
SQLCMD Mode cannot be toggled on or off programmatically, but you can have SQLCMD Mode enabled for all new query windows by going to Tools | Options | Query Execution and checking the box for "By default, open new queries in SQLCMD mode".
There is no SQLCMD mode for the SQLCMD.EXE command-line utility; these commands always work in that tool.
1 Changing the connection logs you out of the current connection. This will drop temporary objects, rollback uncommitted transactions, etc., and local variables will obviously be out of scope (i.e. cannot cross connection boundaries). If statements executed on both servers need to share any info or objects, then you will need to create a Linked Server on one of them and use that Linked Server to connect to the other server on the current connection. Variables and temp objects still can't transfer between them, but you would then at least have the ability to construct Dynamic SQL containing that info to then execute on the remote server, and/or use the local resources in queries that specify four-part names for the remote objects.
When you connect to Sql Server, you are connecting to a specific server instance. A server instance can host a number of different databases. The USE Database command allows you to tell Sql Server which database in that instance to use.
A corresponding USE Server command does not make sense. Other server instances are not connected to or part of this instance. When you connected to this server, you had to provide credentials that may or may not be valid on other servers, and authentication best practices recommend against preserving those credentials throughout a session, meaning it has no way to re-authorize your connection with a different server.
What you can do is create a linked server, using the sp_addlinkedserver procedure. Then you must include the server name as part of fully-qualified table name with each query.
As far as I know there isn't any way to use ServerName in SQL Server. You can create a linked server and specify that as part of the table name.
Example:
SELECT id, name from MyLinkedServer.MyDBName.dbo.TableName
If this is something that will work for you take a look at these articles which go into linked servers in more detail:
http://technet.microsoft.com/en-us/library/ms188279.aspx
http://msdn.microsoft.com/en-us/library/ff772782.aspx
http://www.codeproject.com/Articles/35943/How-to-Config-Linked-Servers-in-a-Minute
yes You Must connect with the mandatory Following::
SELECT ##SNERVERNAME AS [ServerName], DB_NAME() AS [DbName] FROM sys.tables.connections AGV
GROUP BY DB_NAME()
GO
:connect DifferentServerName
SELECT ##SERVERNAME AS [ServerName], DB_NAME() AS [DbName]
FROM AGV
WHERE CASE WHEN ##SERVERNAME = AGV.SERVER.CONNECTION, 168.192.11.1, 'null'
and
AVG.RECORD_ID between -Inf and Inf
AND
(avg.RECORD_ID is not null or avg.RECORD_ID is null)
Related
Well I am a college student and I have a database project to build on Oracle 19c (SQL Developer) just like a management system. When I click on new connection in SQl Developer , then enter database name (my project name) and enter user name as system and pswd. and is SID I write 'orclpdb' and when I connect it gives an error that database is not open. How can I start making tables and stuff and complete my project on oracle 19c sql developer. Please guide me.
Update: While creating new database connection in SQL Developer, In Service name if I write 'orcl' and test then the connection is successful but when I write 'orclpdb' it says 'database not open'
So it sound like the container database (orcl) is started but the pluggable database (orclpdb) is not. You need to start it.
In order to actually manage a database you are going to have to learn to work without SQL Developer, and use the command-line utility sqlplus.
From a command line:
C:> set ORACLE_SID=orcl
C:> sqlplus / as sysdba
SQL> alter pluggable database orclpdb open;
In the above, the sqlplus command is launching the command line utility 'sqlplus'. the '/' indicates to make a local connection to the database specified by the env variable ORACLE_SID, using OS authentication (os user is a member of the ORACLE_DBA group), and connect with 'sysdba' authority. On the next line the 'SQL>' is just indicating you are now at the sql prompt within sqlplus, you actually enter the 'alter' command, whose purpose should be self-evident.
The listener is a totally separate process. It is like a telephone switchboard. It 'listens' (hence, its name) for connection requests coming across the network, and set up the connection, then is out of the picture. You check its status at the command line:
C:> lsnrctl status
One last bit of useful (for us) information. What this the connection 'type' you've defined in SQL Dev? Is it 'basic' or 'tns'? Either way, what did you specify for the values? Please name the field(s) AND the value you supplied.
I have a stored procedure with the exact same path on multiple servers (live, development and test) and I would like to modify/alter it simultaneously. I was thinking this would happen via altering one and then copying that and overwriting it over the other two servers.
Would this be easily achieved?
If you can connect to all the environments from a single SSMS (unlikely in many companies because of security), you can register all 3 SQL Server instances in a single group and execute the same script on all instances simultaneously
(refer to: https://msdn.microsoft.com/en-us/library/bb964743.aspx)
You can script out your stored procedure and use SQLCMD tool to execute it from file. Again, if you can connect to all instances from a single server, you can just duplicate the command in the script, but connect to multiple instances i.e. just use 3 SQLCMD lines
You can use any schema compare tool, Such as Red Gate SQL Compare
Also check alternative tools to Red Gate: Here
Also youo can check this opensource tool: Open DBdiff
The setup:
I have two different machines, one with MS SQL Server 2008 which is my main machine and a secondary with MS SQL Server 2000. Newly generated data are stored in a specific table on the main server(2008).
The problem:
My main machine has limited storage, whereas my secondary one with the older SQL version(2000), doesn't have such kind of limitations.
A possible solution:
At least as a temporary solution, i could try to move some data, on a daily schedule, from the main machine to the secondary, using sqlcmd, run by a Windows Task Scheduler.
The largest portion of my data are stored on a single table so the idea is to "cut" them from the table on the main server and append them on a "backup/depot/storage" table on my secondary server.
What i have tried so far:
So far, i haven't been able to simultaneously connect to both servers from the main one, at least using sqlcmd. Is there a limitation for the sqlcmd to the connections it can create simultaneously?
Other possible ways:
Is there a suggested practice for that case? Would it be a good idea to write a vbs script to export from the main server and import to the secondary?
All corrections and suggestions are welcome. And thanks for your time.
First, link the servers. From the server you want to INSERT into, run this SQL (using any tool you want to run SQL with... SQL Server Management Studio or SQLCMD or OSQL or whatever):
EXEC sp_addlinkedServer N'remoteSqlServer\Instance', N'SQL Server'
EXEC sp_addlinkedsrvlogin #rmtsrvname = N'remoteSqlServer\Instance', #useself = 'FALSE', #rmtuser = N'user', #rmtpassword = N'password'
Replace "remoteSqlServer\Instance" with the actual host-name\instance of the SQL Server you want to copy data FROM. Also replace the "user" and "password" with appropriate login credentials to that server.
After that is done, you can execute SQL like the following against this server (from anywhere, including SQLCMD):
INSERT INTO [LocalTable]
SELECT * FROM [remoteSqlServer\Instance].[DatabaseName].[schema].[TableName]
Again, this is just an example... you'd replace all those values with values appropriate to your source and destination databases (everything in the square brackets). For instance, it might look something like this:
INSERT INTO [HistoricalData]
SELECT * FROM [DBServer\SQL2008].[ProductionDatabase].[dbo].[CurrentData]
I hope this helps.
I'm making a call to odbc32.dll (SQLBrowseConnect) to return a list of databases on a sql server.
From running a trace I can see the query being executed is
select name from master..sysdatabases where has_dbaccess(name)=1
If the credentials I pass aren't the sa user it returns just the system databases. Is there anyway I can use SQLBrowseConnect with another user (whose default database is also not guarenteed to be the master database) to return all databases on the server?
Also I want to avoid smo objects
In our ETL tools we do use SQLBrowseConnect to get a list of available SQL servers.
We do not use it for getting list of the databases
SQLExecDirect(FHSMT,PAnsiChar ('select name from MASTER.dbo.sysdatabases order by name'), SQL_NTS)
We use different ODBC driver for different versions of SQL server.
I have two Sysbase servers , server1 and server2 . I have a stored procedure declared and running on server1. In that SP i want to access table from server2. How can i do that? Also my both instances are running in a UNIX box
You can do that, read more about "Proxy tables" at Sybase Infocenter (search for: create proxy_table statement) Afaik, there has to be remote server declared as well. I think it's documented well.
First, create a 'Remote Server' in the database that has the stored procedure.
You will need to tell it what the 'Server Type' is and how to connect (ODBC, for instance), then in the connection information, you can simply put the DSN name.
Now, you can create proxy tables to reference tables in your other server.
Try using the four part name. See here: http://www.dbforums.com/sybase/1001475-call-sql-server-stored-proc-sybase.html