Blog Pages

Property Owner is not available for Database

Error message:
Property Owner is not available for Database '[...]'. This property may not exist for this object, or may not be retrievable due to insufficient access rights.  (Microsoft.SqlServer.Smo)

Explanation:
For some reason, the owner of the DB was set to 'UNKNOWN'.
Check it using sp_helpdb:


Solution:
Change the DB owner to a valid login, using sp_changedbowner.

USE DBNAME
GO
sp_changedbowner 'sa'
GO

Join to the first row in other table

Join to the first row in other table - using CROSS APPLY:

SELECT aId, aInt, Adress, AbitInt 
FROM dbo.aaa

SELECT bId, DateCreated, aId
FROM dbo.bbb
ORDER BY aId, DateCreated

-- for each row in aaa - join to the earliest row in bbb
SELECT a.aId, a.aInt, a.Adress, a.AbitInt, bb.bId, bb.DateCreated
FROM dbo.aaa a
CROSS APPLY
( SELECT TOP 1 bId, DateCreated, aId
FROM dbo.bbb b
WHERE b.aId = a.aId
ORDER BY aId, DateCreated
) bb