Showing posts with label defined. Show all posts
Showing posts with label defined. Show all posts

Friday, March 30, 2012

How to exec SQL user defined function?

Hi,
How to exec a SQL user defined function in query analyzer when it accepts parameters.. I know for a stored procedure we can write
EXEC nameofstored procedure abc (@.abc is the parameter passed).. But How to run a SQL function ?
Thanks

assume function GetUser takes ID as input parameter , write the following in query analyzer and execute

BEGIN
DECLARE @.ID int
set @.ID = 45
SELECT dbo.GetUser(@.ID) AS 'User Name'
END

|||You need to either use it in a stored procedure and then call thestored procedure, or use it in some other statement, such as a SELECTstatement:
SELECT MyFunc(someField) FROM MyTable
There's no way to call it directly as you want.
Don
sql

Friday, March 9, 2012

How to drop a user defined database role in 2005?

Using Studio, I created a user defined database role but I can not delete it because

"TITLE: Microsoft SQL Server Management Studio

Drop failed for DatabaseRole 'test1'. (Microsoft.SqlServer.Smo)

ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

I am quite annoyed because the "owned schema" is db_owner, which can not be unselected. Quite an innovation. How do I drop this relationship?

i think the DB_Owner schema is owned by this role

run this statement to see the schema and owner ... check whether this role is the owner of any schema

SELECT s.name SchemaName, d.name SchemaOwnerName FROM sys.schemas s INNER JOIN sys.database_principals d ON s.principal_id= d.principal_id

if this role owner of DB_Owner Schema run the below statment to transfer the owner ship..

ALTER AUTHORIZATION ON SCHEMA::[db_owner] TO [db_owner]

Drop role test1

Madhu

|||Nice and simple. Does the trick. Thanks Madhu.

Cheers,

Sameer.

How to drop a user defined database role in 2005?

Using Studio, I created a user defined database role but I can not delete it because

"TITLE: Microsoft SQL Server Management Studio

Drop failed for DatabaseRole 'test1'. (Microsoft.SqlServer.Smo)

ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

I am quite annoyed because the "owned schema" is db_owner, which can not be unselected. Quite an innovation. How do I drop this relationship?

i think the DB_Owner schema is owned by this role

run this statement to see the schema and owner ... check whether this role is the owner of any schema

SELECT s.name SchemaName, d.name SchemaOwnerName FROM sys.schemas s INNER JOIN sys.database_principals d ON s.principal_id= d.principal_id

if this role owner of DB_Owner Schema run the below statment to transfer the owner ship..

ALTER AUTHORIZATION ON SCHEMA::[db_owner] TO [db_owner]

Drop role test1

Madhu

|||Nice and simple. Does the trick. Thanks Madhu.

Cheers,

Sameer.