Multiple Schema Permissions In SQL Server 2008 R2 - sql

We are migrating a database from SQL Server 2000 to SQL Server 2008 R2 and have run into a problem with our schemas. In SQL Server 2000, if you didn't prefix your object names with a owner/schema (e.g. SELECT * FROM blah rather than dbo.blah) SQL Server would still figure it out. This worked even if your object was in a non-dbo owner.
In SQL Server 2005 this was changed as described here: http://blogs.msdn.com/b/mssqlisv/archive/2007/03/23/upgrading-to-sql-server-2005-and-default-schema-setting.aspx.
I can get around a database having 2 schemas by making a user's default schema xyz instead of dbo. The problem I run into is in a database where there is a 3rd schema (abc.blah). When I make the users default xyz, they can get to dbo without a hitch but get an Invalid Object error when trying to select something from abc without a schema prefix (abc.blah works fine).
Of course, it shouldn't have been coded this way to begin with but that is a different battle. I've verified that permissions are not an issue.
I'm sure others have run into this. Has anyone found a workaround other than fixing the code?

There isn't a whole lot of magic here; fix the code.
Assuming you don't have same-named objects in both schemas, you may be able to create a slew of synonyms like:
CREATE SYNONYM xyz.foo FOR dbo.foo;
...but that is going to be as much of a mess as fixing the code.

Related

Ignoring schema name on sql select?

using SQL server 2014 I am able to select without specifying the schema name when it is NOT DBO.
We are now switching over to SQL 2016 and I am no longer able to select without adding the schema name?
Problem: Going back into each stored proc to add the schema name in would take a lot of time so I was wondering if it is possible to ignore the schema name somehow? I have been searching google but haven't found anything..
The procs are used in our SSRS reports, which there are over 100 and some are embedded into the reports so if there is a way to avoid having to change each one that would be great!
It seems the default schema is a property of the connecting user. Maybe you should check how the new "migrated" user has been defined.

For a Front end access user interface connected to a backend SQL server, do I create new queries for tables in the SQL server or Access frontend?

I am a new database intern working with a access front end and SQL server backend database. The database was custom made for the company. One of my assignments is to take scripts and apply them to make four new tables. I am aware that I need to make a new query for each new table but I don't know if I should make the query in SQL server management studio or the frontend access program. I have tried copying and pasting the given scripts into a new query in access but I get an error message "invalid SQL statement expected 'DELETE', 'INSERT'...". I decided to try to break done the program a little bit and tested the first line
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =OBJECT_ID(N'[dbo] .[FK_tblInstrumentInterfaceLog_tlkpInstrument]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblInstrumentInterfaceLog]'))
but the same error message keeps popping up. I even tried just SELECT * FROM sys.foreign_keys, and I got the error message "could not find file...". I am very much a beginner and any guidance would be appreciated.Basically am I supposed to be applying these scripts the server SQL database or on the front end access program?
Are you using a pass-through query? i.e. not just a select query. Access needs to know where to send the query and since you are using TSQL not Access SQL this needs to be executed on the server.
Normally when you query a linked table the information of how to get the data (the connection string) is tied to the table. But for this kind of query you'll probably need to tell Access explicitly. Unless you are using an ADP/ADE, then the connection info travels with the program not the table.
As a general rule, you use SQL management studio (SSMS) to create and run those scripts. So the general accepted approach here is such scripts will not be placed in the front end. As noted such scripts if for some reason must be placed in the front end, then you have to create them as pass-though, but EVEN in this case you want to use the SSMS to create such quires.
So the answer here is you create the new scripts and make table queries in the back end, or in this case using the SQL server management studio.
The syntax checking, query editor etc. in recent versions of SSMS now has auto-complete etc. and you can test/write/update those scripts in SQL server. Once you have such a query or even several of them, then the resulting “several” statements can be pasted into a front end query that been created as pass-though. If you do not use a pass-though query, then you are creating and using and assuming client side SQL (JET (now called ACE)).
The client side has it own version of SQL syntax, and it is NOT 100% compatible with the SERVER SIDE. If you writing SQL in the client that is NOT pass though, then you using a linked table to SQL server. These linked tables thus will use local (JET/ACE) based SQL queries. The ODBC driver thus translates this SQL into server side compatible syntax. However the JET/ACE sql syntax is very limited when compared to SQL server and no server side commands exist in this SQL syntax for the client data engine (JET/ACE)
So for many quires, you will and can simply build such queries using the Access query builder.
However for SQL that needs to run 100% server side then such quires has to be setup as pass-though and are in most cased built + tested using SSMS.

Oracle Database Users in synced database

Ok so I have a little problem...
In my project we have a Oracle SQL Server. In the database I have access to some of an other users tables:
Tables:
|-bla
|-bla
Users:
|-otherUser (let's just call him that)
|-Tables:
|-aTable
In Oracle, to access the aTable table I use SELECT * FROM otherUser.aTable
Now, we also have a MS SQL CE database to which I sync the data from the OracleDB using the MS Sync f/w. And in the CE db - after sync - I get a table otherUser.aTable. This sounds good, so even though the CE doesn't have the User concept it just adds the same table.
BUT the problem is that when calling the same SQL query on CE as on Oracle I get a The table name is not valid error. Instead if I want to get the content of the table, the two ways that I have found to work is surrounding the otherUser.aTable with either [] or "".
However neither of them seem to work with Oracle. The [] seem to be an illegal name, and the "" seem to search for a table called just that (not an other user).
So why don't I just use the one way on Oracle and the other on CE? well I also use NHibernate as a ORM and it kind of needs the same table name for both the databases...
Is there a third way to encapsulate the table name that works with users in Oracle and just works in CE? or do you have any other ways to fix this issue?
I have no experience with MS SQL, but it seems like a problem that might be solved with synonyms on Oracle side.
Try to create synonym "otherUser.aTable" for otherUser.aTable in Oracle.

Invalid Object Name in mssql 2005

We currently have an issue with our MSSQL 2005 database. We've recently undergone a migration from windows server 2003 to 2008. SQL Server has remained as 2005. I'm using SQL Server Management Studio to work on.
All the tables in the database follow the format: [SCHEMA].[TABLE_NAME]
When I enter the SQL:
SELECT * FROM [table_name]
I get the error message: Invalid object name '[table_name]'.
If I type in:
SELECT * FROM [schema].[table_name]
This works fine. The username we log into the database with owns the schema which is prepended to all the table names.
The problem is, we have 3rd party companies which have software which exports data from the database. 2 companies have now tried to run their exporters, however are getting the error 'Invalid object name'. The software is logging in with the username which owns the schema.
My question is exactly the same as this one I've found on the internet:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=99802
However the answer on that question seems to be full of keywords rather than actual sentences which makes it very hard to understand.
If anyone could help, it'd be greatly appreciated.
Many thanks
Phil
Set the default schema for the 3rd party's username to the schema their un-prefixed references should point to:
ALTER USER foo WITH DEFAULT_SCHEMA = [bar];
(This is different from owning the schema.)
And tell them to write software correctly. In SQL Server you should always be specifying the schema name, even if you always use dbo.

How do i get the names of all the tables inside a database?

EDIT2: Found a fix! I used the number of the desired schema instead of the name. Should've thought of that before, really! And i think the error messages could've been a bit better aswell. Thanks for all your time!
How can i get the names of all tables inside a database through sql inside asp classic?
The server is running windows 2008, iis7.5 and microsoft jet. I've tried all the querys i could find on the internet (and here) but none have worked.
If i add a ; to the query to run a set of querys at the same time it gives me an error because the statement isn't over at the semicolon.
The master.mdf database cannot be accessed because it's of unknown format.
The sysobjects variable apparently doesn't exist.
I am using mssql 2000 format. (.mdf)
The connection is made through classic asp with the Microsoft.Jet.OLEDB.4.0 provider and ADODB connection/recordset.
How do I get list of all tables in a database using TSQL?
Query to get the names of all tables in SQL Server 2008 Database
EDIT:
I've found two folders containing databases. One is in C:\program files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\mssql\binn\templates and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, msdbdata.mdf and msdblog.ldf. The other one is also in the \binn\template data directory and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, MSDBData.mdf, MSDBLog.ldf, mssqlsystemresource.ldf, mssqlsystemresource.mdf, tempdb.mdf and templog.ldf. Maybe these is of some interest?
How can i tell if i have permission? Does it give a permission denied error?
Please help! No, don't. Read the 2nd edit at the top.
USE YOUR_DATABASE
GO
SELECT *
FROM sys.Tables
GO
Have you tried the example from:
http://www.kamath.com/codelibrary/cl002_listtables.asp
I almost always use the INFORMATION_SCHEMA views:
SELECT * FROM INFORMATION_SCHEMA.TABLES
If this isn't working for you, the SQL user your site is running under may not have access to the system objects. This is actually a good thing, as giving your site access to the underlying database schema can leave you vulnerable to SQL injection.
So if you do go this route, proceed with caution.
The mdf by itself is useless: you need a database engine (a.k.a. a SQL Server instance) to "run" it. As I understand the question, this is your problem.
Then you can use sysobjects in your database: unless you have added your tables to the master database
There is no practical way to use an mdf directly: if nothing else download MSDE