Is there a way to set the schema for a query so that in the rest of the query I can refer to tables just by their name without prepending them with a schema name?
For instance, I would like to do something like this:
Use [schemaName]
select * from [tableName]
as opposed to this:
select * from [schemaName].[tableName]
8条答案
按热度按时间raogr8fs1#
A quick google pointed me to this page . It explains that from SQL Server 2005 onwards you can set the default schema of a user with the
ALTER USER
statement. Unfortunately, that means that you change it permanently, so if you need to switch between schemas, you would need to set it every time you execute a stored procedure or a batch of statements. Alternatively, you could use the technique described here.If you are using SQL Server 2000 or older this page explains that users and schemas are then equivalent. If you don't prepend your table name with a schema\user, SQL Server will first look at the tables owned by the current user and then the ones owned by the
dbo
to resolve the table name. It seems that for all other tables you must prepend the schema\user.0mkxixxg2#
I do not believe there is a "per query" way to do this. (You can use the
use
keyword to specify the database - not the schema - but that's technically a separate query as you have to issue thego
command afterward.)Remember, in SQL server fully qualified table names are in the format:
[database].[schema].[table]
In SQL Server Management Studio you can configure all the defaults you're asking about.
database
on a per-user basis (or in your connection string):Security > Logins > (right click) user > Properties > General
schema
on a per-user basis (but I do not believe you can configure it in your connection string, although if you usedbo
that is always the default):Security > Logins > (right click) user > Properties > User Mapping > Default Schema
In short, if you use
dbo
for your schema, you'll likely have the least amount of headaches.qyswt5oh3#
Very old question, but since google led me here I'll add a solution that I found useful:
Step 1. Create a user for each schema you need to be able to use. E.g. "user_myschema"
Step 2. Use EXECUTE AS to execute the SQL statements as the required schema user.
Step 3. Use REVERT to switch back to the original user.
Example: Let's say you have a table "mytable" present in schema "otherschema", which is not your default schema. Running "SELECT * FROM mytable" won't work.
Create a user named "user_otherschema" and set that user's default schema to be "otherschema".
Now you can run this script to interact with the table:
The revert statements resets current user, so you are yourself again.
Link to EXECUTE AS documentation: https://learn.microsoft.com/en-us/sql/t-sql/statements/execute-as-transact-sql?view=sql-server-2017
ifmq2ha24#
What I sometimes do when I need a lot of table names I'll just get them plus their schema from the
INFORMATION_SCHEMA
system table: valueaydmsdu95#
SETUSER could work, having a user, even an orphaned user in the DB with the default schema needed. But SETUSER is on the legacy not supported for ever list. So a similar alternative would be to setup an application role with the needed default schema, as long as no cross DB access is needed, this should work like a treat.
xpszyzbs6#
Try setuser. Example
gojuced77#
Another way of adding schema dynamically or if you want to change it to something else
No need to cast @ID if it is nvarchar or varchar
s1ag04yj8#
For Oracle, please use this simple command:
ALTER SESSION SET current_schema = your-schema-without-quotes;