Long time I have not changed active database (like USE mydbname) and created bunch of tables into a master database I think. Ever since then when new databases are created these tables appears in it.
I think one of the four default databases (master, model, msdb, tempdb) works as model for new databases and therefore the "extra" tables must be stored somewhere. Based on this description, could you please advice me how to get rid of these tables in order to create new empty databases?
Do you want the get the user tables in system database?
you can try this:
EXEC sys.sp_MSforeachdb #command1 = N'use ?;select ''?'', * from sys.tables WHERE type=''U'' and is_ms_shipped=0'
Related
I have a SQL database (lets use northwind), that has a number of tables (unknown number of tables). I would like to import these tables into a MS access database as DATA (not tables) into a MTT_Table
All standard imports, creates the table as a physical table within ms access and not as data.
I have a table in MS Access that needs to store all the names of tables in other systems - not sure if that makes sense
Is there any way to read an infinite number of tables and populate them as data, using an odbc connection all through VBA
Expected output would be to see the table names as data values, and potentially able to populate the MS access row with metadata about the table
Use information schema to create a view in SQL server:
CREATE VIEW dbo.Sample_View
AS
SELECT TABLE_NAME
FROM [Your_Database].INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Now import this view to access following the steps in this link
Your question is a bit broad (what information do you want from tables), but generally can be achieved by querying the INFORMATION_SCHEMA meta-tables over ODBC.
SELECT * INTO MTT_Table
FROM [ODBC;Driver={SQL Server};Server=my\server;Database=myDb;Trusted_Connection=Yes;].INFORMATION_SCHEMA.TABLES
I have a legacy db from an application that is no longer supported. I'm trying to deconstruct a stored procedure to make a modification, but I can't figure out where #tempimport is located. I've searched high and low through the schema, but I'm not finding anything close. This is how it's used:
SET NOCOUNT ON;
DECLARE #saleid UNIQUEIDENTIFIER
SELECT TOP 1 #saleid = [sale_id] FROM #tempimport;
Is this a T-SQL specific thing, or can I actually find the data in this table somewhere?
Tables with the # preceding the name are temporary tables.
You can find them in the tempdb database under System Databases
Tables that are prefixed with a # are temporary tables that are created in the tempdb system database.
try to find code of this table creation in you DB schema
select * from information_schema.routines
where object_definition(object_id(routine_name)) like '%#tempimport%'
try find #tempimport in schemas of neighboring DBs
try find #tempimport in your application sources if exists.
try to profile(SQL Profiler tool) your application and search #tempimport here.
Additional
#tempimport can be created by any connected application. Not only from your DB runnable code
you can research deeper and try to monitoring the moment of #tempimport creation. Example
select left(name, charindex('_',name)-1)
from tempdb..sysobjects
where charindex('_',name) > 0 and
xtype = 'u' and not object_id('tempdb..'+name) is null
and left(name, charindex('_',name)-1) = '#tempimport'
source: Is there a way to get a list of all current temporary tables in SQL Server?
We have a requirement to provide customer access to a staging database so that they can extract their data into their own servers, but every table contains all customers data. All of the tables have a 'CustomerID' column. Customers should only see rows where the customerID is the same as theirs.
I am not looking for suggestions to create separate databases or views for each customer as both suggestions are high maintenance and low efficiency.
My solution has to work with:
100GB database
400 Tables
Updates every 30 minutes from the core transaction database
Quarterly schema changes (Application is in continuous Development).
Can anyone give me a definitive answer as to why the following method is not secure or will not work?:
I've set up a database user for each customer, with their customerID as an extended property.
I've created a view of every table that dynamically selects * from the table where the customerID column is the same as the extended property CustomerID of the logged in user. The code looks like this and appears to work well:
CREATE VIEW [CustomerAccessDatabase].[vw_Sales]
AS SELECT * FROM [CustomerAccessDatabase].[Sales]
WHERE [Sales].[CustomerID]=
(SELECT CONVERT(INT,p.value) AS [Value]
FROM sys.extended_properties
JOIN sys.sysusers ON extended_properties.major_id=sysusers.[uid]
AND extended_properties.name = 'CustomerID'
AND sysusers.[SID]=(SELECT suser_sid())
);
GO
To provide access to the views I've created a generic database role 'Customer_Access_Role'. This role has access granted to all of the table views, but access to the database tables themselves is denied.
To prevent users from changing their own customerID I've denied access to the extended properties like so:
USE [master];
GO
DENY EXEC ON sys.sp_addextendedproperty to [public];
GO
DENY EXEC ON sys.sp_dropextendedproperty to [public];
GO
DENY EXEC ON sys.sp_updateextendedproperty to [public];
GO
The end result is that I only need one database, and one set of permissions.
To add a new customer all I would need to do is create a new user with their customerID as an extended attribute and add them to the Customer_Access_Role. Thats it!
I am going to reiterate what everyone is stating already and sum it up.
You are making your job harder than it has to be.
Create a View, that is just their data and then give them Security access to that View.
Alternatively, extract all their data out of the "Core" database and into their own and give them the necessary access to that data.
What's the way to query more than one database using SQL*Plus?
In MySQL it's possible to do something like this :
create table WK_LINK_JOINT_IDEOREQ AS
select k.constraint_name cn, k.table_name tl, l.column_name lc
, k.referenced_table_name tg, k.column_name cg, l.referenced_table_name td
, l.referenced_column_name cd
from information_schema.KEY_COLUMN_USAGE k
It's just an example, it's not complete : but as you can see, we are working on two databases, INFORMATION_SCHEMA and another one.
I want to do somthing like this using SQL*Plus, but the problem is that when we connect using SQL*Plus, we specify the database (SID) which means that the others are not accessible.
Is there a way to do it?
Oracle has a different interpretation of DATABASE from MySQL. In Oracle we have multiple users or schemas in the same database.
So, if what you really want is to access objects from a different schema all that has to happen is for that schema to grant you privileges. You can then reference the tables (or whatever) in your SQL.
User JOE grants you select on his table
SQL> conn JOE/SOAP
SQL> grant select on my_table to ABC;
You can then run queries on it:
SQL> conn ABC/DEF
SQL> select * from joe.my_table;
In your example you used INFORMATION_SCHEMA. The Oracle equivalent of this is the data dictionary, an enormous library of views. Find out more.
Public access is granted by default on most of them. So you can select from USER_TABLES, USER_CONSTRAINTS and USER_CONS_COLUMNS to re-create that query (assuming I have understood it correctly).
I'm starting out with Linq To SQL, fiddling around with Linqpad and I'm trying to duplicate a SQL script which joins on tables in separate databases on the same server (SQL Server 2008).
The TSQL query looks approximately like this:
using MainDatabase
go
insert Event_Type(code, description)
select distinct t1.code_id, t2.desc
from OtherDatabase..codes t1
left join OtherDatabase..lookup t2 on t1.key_id = t2.key_id and t2.category = 'Action 7'
where t2.desc is not null
I'm basically trying to figure out how to do a cross-database insertion. Is this possible with Linq To SQL (and is it possible in Linqpad?)
This is possible in LINQ to SQL if you create a (single) typed DataContext that contains table classes for objects in both databases. This designer won't help you here, so you have to create some of the table classes manually. In other words, use the VS designer to create a typed DataContext for your primary database, then manually add classes for the tables in the other database that you wish to access:
[Table (Name = "OtherDatabase.dbo.lookup")]
public class Lookup
{
...
}
Edit: In LINQPad Premium edition, you can now do cross-database queries with SQL Server - in one of two ways.
The simplest is the drag-and-drop approach: hold down the Ctrl key while dragging additional databases from the Schema Explorer to the query editor. To access those additional databases in your queries, use database.table notation, e.g., Northwind.Regions.Take(100). The databases that you query must reside on the same server.
The second approach is to list the extra database(s) that you want to query in the connection properties dialog. This dialog also lets you choose databases from linked servers. Here's how to proceed:
Add a new LINQ to SQL connection.
Choose Specify New or Existing Database and choose the primary database that you want to query.
Click the Include Additional Databases checkbox and pick the extra database(s) you want to include. You can also choose databases from linked servers in this dialog.
You can now do cross-database queries. These are properly optimized insofar as joins will occur on the server rather than the client.
Use linked servers with fully qualified names to query another database from the current DB. That should work.
using MainDatabase
go
insert Event_Type(code, description)
select distinct t1.code_id, t2.desc
from <Linked_Server>.OtherDatabase..codes t1
left join <Linked_Server>.OtherDatabase..lookup t2 on t1.key_id = t2.key_id and t2.category = 'Action 7'
where t2.desc is not null