Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Cross-database query in SQLEXPRESS Expand / Collapse
Author
Message
Posted Monday, May 13, 2013 10:30 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 11:44 AM
Points: 1,293, Visits: 1,427
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)

Post #1452211
Posted Monday, May 13, 2013 10:36 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:17 PM
Points: 42,450, Visits: 35,505
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 2008, MVP
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

Post #1452214
Posted Monday, May 13, 2013 11:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 11:44 AM
Points: 1,293, Visits: 1,427
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.

Post #1452231
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse