I have the following query to check whether there are any user defined objects in my SQL DB.
DECLARE @testForEmpty BIT
if exists
(select top 1 null from dbo.sysobjects where (objectproperty(id, 'IsMsShipped') = 0))
set @testForEmpty = 0
else set @testForEmpty = 1
When I run this query as a specific user, I am aways getting testForEmpty = 1. This meant that the if exists call returns empty rows.
However if I add the user as sysadmin, then I get the testFormEmpty value as 0 and atleast one row is getting selected.
I do not want to add the user as sysadmin. What is the minimum role / permissions that I should grant so that the select from dbo.sysobjects returns the content.
Thanks
sysobjects contains one row for each table, view, stored procedure, extended stored procedure, log, rule, default, trigger, check constraint, referential constraint, computed column, function-based index key, and (in tempdb only) temporary object, and other forms of compiled objects.
For the existing view, you can go to the Properties of the view in SSMS, add users in the Permissions, and then grant select permission in the permissions list. Or use the following statement to grant user permissions: GRANT SELECT ON OBJECT::[schema]. [yourview] TO User1,User2.
The dbo schema is the default schema of every database. By default, users created with the CREATE USER Transact-SQL command have dbo as their default schema. The dbo schema is owned by the dbo user account. Users who are assigned the dbo as default schema don't inherit the permissions of the dbo user account.
First - you should use sys.objects instead of dbo.sysobjects. dbo.sysobjects is a SQL 2000 construct that is only in SQL 2008 for backward-compatibility reasons. sys.objects contains a row for each user-defined, schema-scoped object that is created within a database, so you wouldn't have to filter your query at all. sys.all_objects is a superset, that contains both system and user objects.
Second - on the permission side - in SQL Server 2005 and later versions, the visibility of the metadata in catalog views is limited to securables that a user either owns or on which the user has been granted some permission. So your user will have to be granted some permission on the items it is looking for. Granting VIEW DEFINITION to the user on the schema(s) in the database would allow the query to work, without granting access to any data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With