0

I have a stored procedure called testSP in my SQL Server Express database.

I am able to execute it using

exec [db_owner].[testSP]

but if I use exec testSP it doesn't work.

What is the cause of this?

I have other databases which do not exhibit this behavior.

Thanks for your help.

4
  • Your user probably has a different default schema. What do you get if yout run SELECT SCHEMA_NAME()? PS. It really helps if you post the actual error though. Stops us guessing.
    – Nick.Mc
    Commented Feb 15, 2017 at 0:46
  • for that command it returns dbo
    – Zee
    Commented Feb 15, 2017 at 0:47
  • Yes. db_owner is different to dbo. Whats the actual error message? Basically your user is set up to use dbo as it's default schema so to run anything in the db_owner schema you need to prefix it
    – Nick.Mc
    Commented Feb 15, 2017 at 0:48
  • here's the error message, Could not find stored procedure 'testSP'. im trying to change the schema
    – Zee
    Commented Feb 15, 2017 at 0:48

1 Answer 1

1

Your user is set up with dbo as it's default schema. That's pretty normal

when you run

exec testSP 

it's using your default schema which is dbo, so it is running this:

exec [dbo].[testSP]

which doesn't exist.

When you run

exec [db_owner].[testSP]

it find and runs that stored procedure

I don't know the background but I guess someone has incorrectly/accidentally created and used a schema called db_owner

In all the db's that work, I guess the objects are in the dbo schema or your user is set up to use the correct schema. Go look in the object browser and compare

If you want to move the stored procedure into the dbo schema run this:

ALTER SCHEMA dbo TRANSFER [db_owner].[testSP];

If you want to change your users default schema to db_owner run this:

ALTER USER [youruser] WITH DEFAULT_SCHEMA = db_owner;

I reckon the db_owner schema is an accident though.

2
  • Yes, i created the procedure with [db_owner].[testSP] which created a custom schema for this stored procedure. I just fixed it an hour ago. Thanks for your input nick.
    – Zee
    Commented Feb 15, 2017 at 1:40
  • Good to hear its fixed. If my answer was of use, please tick it.
    – Nick.Mc
    Commented Feb 15, 2017 at 2:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.