Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:43:13+00:00 2026-05-31T14:43:13+00:00

I have several databases that each have a table called, say, products . One

  • 0

I have several databases that each have a table called, say, products.

One of these databases (the “master”) contains products that should be visible from all databases.

I do this by having an allproducts view as follows:

CREATE VIEW allproducts AS
   SELECT col[...] FROM products
   UNION ALL SELECT col[...] FROM master.dbo.products;

This works well, except that the master database’s allproducts view needs a definition that doesn’t bring in its own products:

CREATE VIEW allproducts AS
   SELECT col[...] FROM products;

To simplify things a little, I’d like to have a common view definition, something like the following:

CREATE VIEW allproducts AS
   SELECT col[...] FROM products
   UNION ALL SELECT col[...] FROM master.dbo.products
     WHERE (DB_NAME() <> 'master');

This works, but I’m concerned about performance. So, is the DB_NAME() function deterministic, and if so, will SQL Server shortcut the entire right side of the UNION ALL based on the comparison?

If DB_NAME() is evaluated for each row of master.dbo.products, is there a cleaner way to do this, or must I maintain separate view definitions?

UPDATE: “All metadata functions are nondeterministic. This means these functions do not always return the same results every time they are called, even with the same set of input values.” – http://msdn.microsoft.com/en-us/library/ms187812.aspx

I found another question where someone had a similar situation and found a reasonable workaround:

  • Is there any way to make this UDF deterministic?

So the main part of the question has been answered. Even though DB_NAME() is nondeterministic, the execution plans are the same for calling a deterministic UDF versus calling DB_NAME(), and performance indicates that the DB_NAME() result does short-cut the other branches of the query. Neither are quite as fast as a scalar comparison like “0=1” in the WHERE clause, so even deterministic functions don’t fully optimize the plan. I can live with a UDF, so I’m going to go that direction for this situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:43:14+00:00Added an answer on May 31, 2026 at 2:43 pm

    “All metadata functions are nondeterministic. This means these functions do not always return the same results every time they are called, even with the same set of input values.” –
    http://msdn.microsoft.com/en-us/library/ms187812.aspx

    I ended up not switching on DB_NAME() and just maintaining two versions of each DDL. Annoying, but even a UDF returning a constant bit value wasn’t as efficient as I hoped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an edit view that is strongly-typed to a table called
In my application I have several DataContexts that connects to different databases with different
We have several SQL Server 2000 databases (I know, we need to upgrade) that
I have a table with several million rows. Each row represents a user session
I have a project that requires us to maintain several MySQL databases on multiple
I have a SQL Server that houses Several Databases. I have a Main Database
I have a table called People one of the columns is an xml field
We're designing a database that has a simple table called Person. Each person can
I have several databases I'm managing, and each needs a copy of a particular
I have several databases that have tables with the same name. I mean, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.