Cross-database query in SQLEXPRESS

  • The site I am working works on SQL Server 2008, but is being adapted to work on SQLEXPRESS.

    I am using the Microsoft ASPNETDB.mdf for membership profile roles and personalization, and the main data is stored in another SQLEXPRESS database file: MAINDB.mdf

    in some stored procedures I need to check the roles of the current user - which is achieved on 2008 using cross database querying - I can't find a way to make this work in SQLEXPRESS.

    e.g.

    USE [MAINDB]

    GO

    CREATE FUNCTION [dbo].uf_GetAccessLevel (

    @MyUserID UniqueIdentifier,

    @OwnerID UniqueIdentifier )

    RETURNS INT

    BEGIN

    DECLARE @Access INT;

    -- Users share a role

    IF EXISTS (SELECT *

    FROM aspnetDB.dbo.aspnet_UsersInRoles AS UIR

    JOIN aspnetDB.dbo.aspnet_UsersInRoles AS R2 ON UIR.RoleId = R2.RoleId

    WHERE UIR.UserId = @MyUserID

    AND R2.UserId = @OwnerID )

    SET @Access = 10;

    ELSE -- public access only allowed

    SET @Access = 0;

    END

    RETURN @Access;

    END

    The function creates OK when connected but will not run:

    Msg 208, Level 16, State 1, Line 1

    Invalid object name 'aspnetDB.dbo.aspnet_UsersInRoles'.

    My question is: Are cross-database queries allowed in SQLEXPRRESS? and if so, how do you reference one from the other, (both database files are in the same App_Data directory)

  • There's no difference in basic querying in Express edition. Providing both databases are on the same instance, that code will work. Check permissions, check that the database name is correct, the schema is correct and the object really is there.

    Connect to the instance from management studio, it's just like any other SQL 2008 instance, with a few feature and scale restrictions.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks Gail,

    For some strange reason in SSMS the dbname was [C:\inetpub\wwwroot\website1\App_Data\ASPNETDB.mdf] I renamed it to ASPNETDB - now the cross database query works.

    So I guess I don't have to use Integrated Security either.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply