Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cross-database query in SQLEXPRESS


Cross-database query in SQLEXPRESS

Author
Message
Tom Brown
Tom  Brown
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1342 Visits: 1466
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)
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47259 Visits: 44389
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


Tom Brown
Tom  Brown
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1342 Visits: 1466
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search