Can i connect to 2 database in pro c code on Linux - c

I wanted to connect to 2 database at the same time in pro c code on Linux, and run SQL queries simultaneously on both the databases in single running application. Is this possible. Please suggest.

We can use below syntax for this
EXEC SQL CONNECT :uName1 IDENTIFIED BY :passwd1 AT :DB_NAME1 USING :dbString1
EXEC SQL CONNECT :uName2 IDENTIFIED BY :passwd2 AT :DB_NAME2 USING :dbString2
EXEC SQL at :DB_NAME1 SELECT .....
EXEC SQL at :DB_NAME2 SELECT .....

Related

Connecting to two diffrent data bases using Pro* C code

how can we connect to two different databases using Pro* C code. I have entries in tnsnames.ora file. By default it is connection to one of the Databases using EXEC SQL CONNECT statement which I am not sure how it is connecting. I want to connect to another DB which is having an entry in tnsnames.ora file. I am using OpenVMS OS.
Vijay Kumar
Below outlines what I have done in the pass to accomplish this.
Default connection
If you do not have a CONNECT statement in your code, Oracle will perform an auto connect when it encounters the first SQL statement. It uses credential information configure by the person who setup the database.
Multiple connections
To connect to multiple databases in Pro C, you must make an explicit connection to all databases reference by the code.
Outline below are the 3 steps I done in the pass to accomplish this:
Declare a database alias name. This will be used to reference the
connection.
Explicitly connect to the database and assigned the alias to the connection. This is done by including an AT clause in the CONNECT statement.
In following SQL statements, reference the connection by including an AT clause.
Example code:
/*
* Declare the database aliases
*/
EXEC SQL DECLARE DB1 DATABASE;
EXEC SQL DECLARE DB2 DATABASE;
/*
* Explicitly connect to the database. Include an AT clause to name the connection
*/
EXEC SQL CONNECT :db1_uid IDENTIFIED BY :db1_pwd
AT DB1;
EXEC SQL CONNECT :db2_uid IDENTIFIED BY :db2_pwd
AT DB2;
/*
* In the SQL statements, reference the connection by including the AT clause.
*/
EXEC SQL AT DB1 SELECT COL1 INTO :var1
FROM TABLE_1
WHERE COL2 = 'some_value';
EXEC SQL AT DB2 SELECT COLUMN1 INTO :var2
FROM TABLE_2
WHERE COLUMN2 = 'some_value';
EXEC SQL AT DB1 COMMIT WORK RELEASE;
EXEC SQL AT DB2 COMMIT WORK RELEASE;
Things to note:
DB1 and DB2 are programmer define alias names.
Passwords, represented by the variables db1_pwd and db2_pwd, must be
variables and not hard coded values.
The username value, stored in variables db1_uid and db1_uid2, was in the following format: username#DatabaseName
Note I believe there are other methods for accomplish this. This is the method I was successful in getting to work.
You can find more information on connecting to databases in the Oracle Pro *C/C++ Programmer’s Guide.

How to backup linked servers with SQL Server?

I want to backup/restore my data which is distributed in some linked servers, which are connected or linked in a central server. How can I do this?
I'm using SQL Server 2012 in all my nodes.
you can use the remote machine to run sp_executesql command within the dynamic SQL.
exec server.master.dbo.sp_executesql 'BACKUP DATABASE DBNAME1 to disk='\\Server\Share\backupfilename.bak''
Or you can do this
exec server.master.dbo.sp_executesql 'BACKUP DATABASE [DBNAME1] to disk='\\Server\Share\backupfilename.bak''
Both line are almost identical , except the square bracket [] on the second syntax.

how can I enumerate the databases of a sql server

Given an instance of SQL server, what's the best way to enumerate the databases?
In SQL Server 2000+:
select * from sysdatabases
In SQL Server 2005+:
select * from sys.databases
The difference is subtle and barely worth mentioning for a one-liner like this. But depending on how much you're going to be accessing the system catalog, you may get some use out of this article:
Querying the SQL Server System Catalog
You can also execute sp_helpdb without an argument to get basic information about all databases. (Pass in a database name as an argument to get more detailed information about that database).
Try this:
SELECT [name] FROM sys.sysdatabases
You could also get this from exec sp_databases;
These commands will work from SQL 2000+.
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'

How do I connect to a database using Pro*C?

As the title says, how do I connect to a given database in Oracle's Pro C?
I don't want the connection for Oracle database but for some other database.
You use the exec sql connect statement in your C code:
EXEC SQL CONNECT :myUserId IDENTIFIED BY :myPassword;
If you want to connect to a non-Oracle database, you will probably have to use the at version of the command:
EXEC SQL CONNECT :myUserId IDENTIFIED BY :myPassword AT :myDbName;
and set up a database link in Oracle so that it can pass requests through to the other DBMS.
DBMS' like DB2 provide transparent gateways which can give you this facility without having to go through ODBC. It depends on which DBMS you're targeting as to how you'd go about setting this up.
From the documentation available here and in more detail here it looks like you can embed a CONNECT statement directly in your code.
To quote the first article, a simplified connect statement would be:
EXEC SQL CONNECT { :user IDENTIFIED BY :oldpswd | :usr_psw }
[[ AT { dbname | :host_variable }] USING :connect_string ]
[ {ALTER AUTHORIZATION :newpswd | IN { SYSDBA | SYSOPER } MODE} ] ;
Relevant answer here if you want to connect with oracle-pro-c, using an oracle-wallet.
Connecting to a database in Pro C using Oracle Wallet
Works great to have a wallet, and provide empty strings for :userId and :userPassword.
EXEC SQL CONNECT :mptyStr IDENTIFIED BY :mptyStr AT :ORACLE_SID;

Executing SQL Server stored procedure in sql squirrel

i'm in ubuntu 9.04 and using sql squirrel as my sql client. i connect to a remote SQL Server. There are some stored procedures in the db. I don't know how to execute them. No explicit gui. Earlier i was in windows and i could use management studio. I can right click on stored procedures and give Execute. You guys have any idea? Let me know. It will be helpful for me. :)
Typically, if you want to execute a SQL Server stored procedure, you'd write:
EXEC Your-stored-proc-name-here #param1 = value1, #param2 = value2
and then execute that command. Typically, you should also use the dbo.StoredProcName notation to avoid any confusion / problems.
EXEC <STOREDPROCNAME> <PARAMLIST>
EXEC dbo.GetCandy 'Red',62
Then hit execute or the equivalent in your editor.
I had to tweak it slightly for a Microsoft SQL Server database (jsqlconnect driver). This worked for me:
execute <sproc_name> <args>

Resources