What I am trying to do is verify a URL. I just need to be able to select that single value from all databases that we have currently in SQL Server 2008. All the databases are the same, just multiple instances of the same database for different users.
I am looking to pull one item from one table in each database.
Each database contains a table SETTINGS and within that table a value for MapIconURL. I need that value from each table from within each database. I am looking at around 30 or so databases that would have this value.
So I found the "undocumented" Stored Proc sp_MsForEachDb and have working....to a point.
The code I am using is this:
EXEC sp_MsForEachDb 'use ?; SELECT "?" as databasename,SETTINGSKEYID, SECTION, NAME, INIVALUE, DESCRIPTION
FROM ?.dbo.SETTINGS
WHERE [NAME] = "MapIconURL"'
I have noticed that it is not selecting all the databases, but that it is also selecting the master table as well as other system tables, and am thinking that may be why it is not selecting all tables. Is there a way to exclude the system related tables?
If the number (and name) of the databases is fixed, then you could simply do:
SELECT MapIconUrl FROM db1.dbo.SETTINGS
UNION ALL
SELECT MapIconUrl FROM db2.dbo.SETTINGS
...
However, if either the number or names of the databases is not fixed, then you have to build the query dynamically.
First, run a query such as SELECT name FROM master..sysdatabases to get the names of the databases.
Then, loop over the result set (in T-SQL, use a CURSOR) and build your query and execute it (in T-SQL, use sp_executesql).
Related
I need to get complete list of external tables in db2 database. I have defined schema called DB2INST1. How to get complete list of external tables information using system tables?
The information lives in syscat.tables (documentation here) for Db2-LUW databases that support external tables, which have their PROPERTY column with value Y in position 27 of that column.
Example query returns the fully qualified name of external tables:
select trim(tabschema)||'.'||rtrim(tabname)
from syscat.tables
where substr(property,27,1)='Y'
with ur;
In general, the best and most reliable way to retrieve all information and to recreate the DDL statements for tables is to use the db2look tool. If you want to extract the metadata on your own, there are some catalog views to start with:
SYSCAT.TABLES holds the table information. Look for the PROPERTY column and check there if it is an external table.
SYSCAT.COLUMNS has the basic column information. But there are more related tables depending on types and attributes.
SYSCAT.EXTERNALTABLEOPTIONS shows the actual options for an external table, the things in addition to what makes a regular table.
There are many more tables to hold table properties, depending on the complexity of the table and column definitions.
Assume I don't know which tables have been written to (not queries, I mean writes). Can I find out names of tables that were last written to?
Don't want constant reporting. Just a query I can run after testing newly added code.
Once I know the names, I'm set; I can just query them using normal sql and see the records. But need to know which tables in a 200 table database. Something like:
select names of last 10 tables that have been written to
I have a data anonymization process that takes a production copy of a database and turns it into an anonymized copy by UPDATE-ing some columns.
Some of the tables contain several million rows so instead of UPDATE-ing the columns, which is very log intensive, I went down the way of
SELECT
Id,
CAST('Redacted' AS NVARCHAR(255)) [ColumnRequiringAnonymization]
INTO MyTable_New
FROM MyTable
EXEC sp_rename MyTable, MyTable_old
EXEC sp_rename MyTable_new, MyTable
DROP TABLE MyTable_old
The problem with this approach is that the "new" table no longer has any of the keys, indices and other dependent objects. I have figured out the keys and indices using SPs to generate the DROP and CREATE scripts. The SPs are based on manually written SQL as can be seen e.g. in this answer.
The next problem is that we have a schemabound view on top of this table, which has indices and a full-text index on its own. The number of SPs to generate scripts is growing and I am sure there will be mistakes.
Is there a way to completely script a table/view by using SQL commands only? ie. just like SSMS does when you click "Script table as - CREATE to" but within a stored procedure?
Right-click on the database, select Tasks; there is Generate Scripts there. Just follow prompts or Google for additional information.
I have a two databases that contain the exact same tables and are on the same server. I want to be able to create a report that will allow me to "merge" these databases so that when a user queries they will query BOTH databases at the same time. Is that even possible?
The simplest way to achieve this would be to create database views that UNION ALL the values from the same tables in both databases - something like:
CREATE VIEW CombinedSalesTable AS
SELECT * FROM database1.SalesTable
UNION ALL
SELECT * FROM database2.SalesTable
- and design the reports to query the views.
You may want to add an additional column to the views to show which database each record comes from, as a key value that is unique in one table may have a "duplicate" in the equivalent table in the other database.
An SQL table has hundreds of tables, stored procedures and functions.
I am trying to put together an SQL query that will return all the dependencies of a given set of tables. Is there a way to accomplish this using SQL Server Management Studio without writing queries?
Updated: Simplified the question to the point.
In SSMS, just right click on the table and choose "View Dependencies". As far as scripting, take a look at this article.
EDIT: In SSMS, you can only see it for one. The reason why is because of the stored procedure that is run to view them only takes one database object. So to script multiple, you'd simply need to use multiple lines of EXEC sp_depends #objname = N'DATABASE.OBJECT'; for the tables/views/stored procedures/functions that you want to get dependencies for. One approach would be to use a script like the following to get the unique list of all dependent objects that will have to be included:
CREATE TABLE #dependents (obj_name nvarchar(255), obj_type nvarchar(255))
-- Do this for every primary object you're concerned with finding dependents for
INSERT INTO #dependents (obj_name, obj_type)
EXEC sp_depends #objname = N'DATABASE.OBJECT'
-- ...
SELECT DISTINCT obj_name, obj_type
FROM #dependents
DROP TABLE #dependents
I just blog something similar to this that might help:
Knowing What to Test When Changing a SQL Server Object.
Another approach would be to right click the database and select "Tasks" and then "Generate Scripts...", check the checkbox "Script all objects in the selected database". This will give you a giant text file that you can then search.