How can I remove the dbo prefix from my table names when I write my sql statements in SQL Server?

前端 未结 6 1489
一向
一向 2020-12-15 07:51

All the tables in my schema have a prefix of dbo. Now in my sql statements, I don\'t like using dbo.tablename all the time. Any workarounds or configuration changes?

相关标签:
6条回答
  • 2020-12-15 08:28

    It's actually beneficial to leave the dbo. prefix in place - after all, in SQL Server, you could have several schemas (the "dbo." thingie) with the same table name, e.g. dbo.MyTable, joe.MyTable, frank.MyTable.

    If you then issue a SELECT (list of fields) FROM MyTable, SQL Server has to first figure out which of the "MyTable" tables you really mean --> this costs time, specifying right off the bat you want "dbo.MyTable" will SAVE you time.

    OK, not a lot on a single query - but SELECT queries are QUITE frequent and it all adds up!

    Marc

    0 讨论(0)
  • 2020-12-15 08:28

    The bounty question isn't exactly the original question.

    The current answers do not contain enough detail.

    None of the answers actually address the question of how to remove the dbo prefix or change it to something else.

    dbo is the schema. You can't remove the schema from a table. You can alter it. There are many examples. Here is one: How do I move a table into a schema in T-SQL.

    0 讨论(0)
  • 2020-12-15 08:32

    I think the user in which you connect have to belong to dbo schema, and then you won't have to type the prefix, it will infer it from the schemas the user belong.

    0 讨论(0)
  • 2020-12-15 08:33

    Unfortunately if you want to get best use out of Microsoft SQL Server you need to put the dbo. in many places. When creating a view you can choose to make it a schema-bound view which means the object names will be checked at compile time, and as long as the view exists the objects it references can't be dropped. (You would have to explicitly drop the view first.) If a view is schema-bound it can also become an indexed view. Newer MSSQL versions also support schema-bound stored procedures, which have other advantages.

    If you want you can create other schemas apart from dbo. But you still need to reference objects explitly, as myschema.mytable and not just mytable.

    You also need the dbo. for user-defined functions.

    Personally, I would like to tell MSSQL that I don't intend to use schemas, I am quite happy with the classical SQL way of a single top-level namespace for tables (at least within a given database), and could you please just assume dbo. if not specified otherwise. But I haven't found a way.

    0 讨论(0)
  • 2020-12-15 08:36

    Actually you should leave those dbo. statements because you SQL will be faster since the optimizer doesn't have to lookup the schema

    Check out this link also Performance Impact of Procedure Calls without Owner Qualification

    0 讨论(0)
  • 2020-12-15 08:44

    just like this, if all your table are in dbo schema

    use mydatabase
    go
    select * from mytable
    

    if you have multiple databases you can do it too:

    select * from mydatabase..mytable
    
    0 讨论(0)
提交回复
热议问题