How to retrieve LinqPad's connection list - linqpad

Is there a way to get connection objects list (servers AND databases) as objects so I can query them.
Scenario
Let's say in my connection list there are
10 databases
5 servers
each server has at least 3 databases
I would like to be able to locate a table or stored procedure name ImReallyDifficultToLocate in those connections
at the end I would like to be able to use something like this...
this.Mapping.GetTables()
.Where(t=>t.TableName.Equals("[ImReallyDifficultToLocate]"))
.Select (t => t.TableName).Dump();

Perhaps you would parse your ConnectionsV2.Xml file (which should be located in your %appdata%\LINQPad\ folder) to determine the list of servers and databases and then manually create a SqlConnection and SqlCommand to issue something like
select * from Sysobjects where name like '%ImReallyDifficultToLocate%'

Related

List visible databases in Teradata SQL Assistant?

I am working w the Teradata SQL Assistant. What I am trying to do, is to list all databases visible in the 'Database Explorer'. At first, I have tried simple query:
select DatabaseName from DBC.Databases;
but this returned thousands of databases I don't even have an access to. The DBC.Databases table seems not to have any field allowing me to filter results to only visible databases, or I am just unable to find it. And I am almost sure there is no such field in any table like '%database%'.
Is there any way to list those visible databases with SQL query?
Never use a system view without a trailing V or VX, those are old legacy views which will truncate object names over 30 characters.
In your case you need to switch to dbc.DatabasesVX, the X indicates that all objects where you don't have any access right are filtered automatically. But this might still return more databases than Database Explorer where you can add databases to the list manually.

How to manage a HSQLDB in JBoss AS7.1.x

It's my first try to use EJB3.1 entities with a JBoss AS7.1.1 server. I figured out that the HSQLDB is no longer included in the version 7 of JBoss. First I added the hsqldb.jar through the Administration Console --> Deployments --> Manage Deployments. After that I added a new Data Source through Profil --> Connector -> Datasources
My first example code works fine:
[...]
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:/DefaultDS");
con = ds.getConnection();
stmt = con.createStatement();
stmt.execute("drop table timers;");
stmt.execute("Create table timers(id char(10));");
stmt.execute("INSERT INTO timers (id) VALUES (20)");
stmt.execute("INSERT INTO timers (id) VALUES (21)");
ResultSet number = stmt.executeQuery("select * from timers");
[...]
My question is how I can manage (=create, drop and update new tables) the DB which is created in the folder jobss\standalone\data\hypersonic. At the moment I have no "overview" which tables are created, the structure of them and the data.
Does someone have a tip or a tutorial for me which deal with the problem? Thank you.
In my case it was easier as I thought at the beginning. I needed to manage the DB which is stored in the AS 7.1.x Server. Because of the missing JMX-Console I wasn't able to get access to that DB over the administration. I've added the Datasource and the Manage Deployments as described in the first post.
To manage such a DB you can use the 'HSQLDB Database Manager', select 'HSQL Database Engine Standalone' as the type and 'jdbc:hsqldb:file:«MY_PATH_TO_DB_FOLDER_IN_JBOSS»' for the URL. Now I can manage the DB outside the server and EJB environment.
Thanks you fredt for your help and your inspirations.
You always query INFORMATION_SCHEMA tables to find out which tables already exist. Once you know a table exists, you can execute a query that you know does not take a long time, in order to find out about the state of the data.
For example, the first query will show you if there is a 'TIMERS' table, and the second query will show if there is any data in the table:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TIMERS'
SELECT * FROM TIMERS LIMIT 1
Try always to define a primary key on each table in order to speed up queries.
To access a database embedded in an app server, you should add the org.hsqldb.server.Servlet class as a servlet then connect to the app server using a database management tool and a URL such as jdbc:hsqldb:http:<your servlet url here>

AS400 / DB2 cross-library query problem

I'm querying an Iseries from ODBC in my app and am trying to perform a query that returns results from 2 tables. I need to join the tables but the tables are in different libraries. I don't want to use library identifiers in my query as my libraries change as I move from dev>qa>prod. However, I am certain that these tables will only be in these libraries and there will be no duplicates in other libraries. Is there a way to do this without specifying the Iseries library?
In essense, I want this:
select DISTINCT GIDNBR, VSL00100.GRNAME
FROM **QACOMMON**.CMPGRID2 CMPGRID2 INNER JOIN **QAVISLIVE**.VSL00100 VSL00100 ON VSL00100.GRNO=CMPGRID2.GIDNBR AND
VSL00100.GRSUB=CMPGRID2.GIDSUB AND
VSL00100.GRLOC=CMPGRID2.GIDLOC AND
VSL00100.GRPOOL=CMPGRID2.GIDPOL
to look like this:
select DISTINCT GIDNBR, VSL00100.GRNAME
FROM CMPGRID2 CMPGRID2 INNER JOIN VSL00100 VSL00100
ON
VSL00100.GRNO=CMPGRID2.GIDNBR AND
VSL00100.GRSUB=CMPGRID2.GIDSUB AND
VSL00100.GRLOC=CMPGRID2.GIDLOC AND
VSL00100.GRPOOL=CMPGRID2.GIDPOL
Any ideas?
You need to go into the configuration for your ODBC connection. On the connection you are using click on "Configure", then select the "Server" tab. The library list option is what you'll need to play with.
Based on your examples it looks like you need to have it say "QACOMMON QAVISLIVE". (with no quotes)
Also you will need to change the naming convention to *SYS instead of *SQL and make the default collection field blank. Make sure your system administrator sets the CURLIB parameter on the user profile you are using to where ever you want new files to go.
Then just save it and try it again. Unfortunately you'll have to either have different data sources or change the ODBC connection when you are switching around between test, production, and whatever else you have.

How to create Sql Synonym or "Alias" for Database Name?

I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.
[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]
It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.
Is there a way to do this that i missed in the docs?
Any one have any suggestions on a workaround?
use 3 part notation and alias up to the table, example
select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.
I've done something similar to this using another config file.
The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.
db.config:
DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass
connection code:
db_connection get_connection(string prefix) {
db_connection db_conn = new db_connection;
string db_name = get_config_value(config_path, prefix + "_DB_NAME");
string db_user = get_config_value(config_path, prefix + "_DB_USER");
string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
db_conn.connect(db_name, db_user, db_pass);
return db_conn;
}
Then you just call get_connection() with your db alias as the argument.
I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.
Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.

Accessing 'Global' Variables In An ExecuteSQL Task

I have an SSIS package that does the following:
Selects the connection strings from a table of servers. The connection string is either the name of the server along with the domain (i.e. Dalin.myhouse.com) or is the direct IP to a server.
The package iterates through each connection string and populates a defined 'global' variable. This variable was created with in the Variable designer. For each connection string, the package will retrieve information about the server.
The problem I'm having is when you add a server IP to the list, the 'Friendly Name' may not be known at that time so I would just have the server IP in both the connection string column and the friendly name column of the table. I want to, after the end of an iteration, update that the Friendly Name column of the server entry within that table so that it has the server name pulled from the server using SERVERPROPERTY('Servername')
To do this, I would like to use an ExecuteSQL Task with the following code:
UPDATE [myDB].[mySchema].[myServers]
SET [ServerName] = VarA
WHERE ConnectionString = VarB
The previous code is using a static connection to the server where the myServers table resides.
VarA represents the global value I want to set the ServerName to which would be set in a separate SQLTask by using SERVERPROPERTY('Servername'). It needs to be in a separate task because it would have to connect to a server by using the same server the current iteration is using.
VarB is set at the beginning of every iteration to the next Connection String in the list.
I've seen examples on how to use this for the Script Task and Script Components but I would like to simply use the ExecuteSQL Task to accomplish this.
Summary:
Connect to ServerA and fill two global variables.
Connect to ServerB and use the two global variables to update a specific row in a table.
Any ideas?
I can't see how this can be accomplished without the variables being set within a Script Task, since ExecuteSQL tasks have to be set to a database connection. Script Tasks work for this because their connection is within the context of the server that's executing them. That being said, you could use a Script Task prior to this ExecuteSQL task that sets the variables to the local server instance.
So you need the Execute SQL task to take parameters?
http://msdn.microsoft.com/en-us/library/ms187685.aspx
Maybe I've misunderstood...