.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

User Defined Functions in Microsoft SQL Server

Posted By: Venkat     Posted Date: April 13, 2010    Points: 2   Category :Sql Server
User Defined Functions are compact pieces of Transact SQL code, which can accept parameters, and return either a value, or a table. They are saved as individual work units, and are created using standard SQL commands. Data transformation and reference value retrieval are common uses for functions. LEFT, the built in function for getting the left part of a string, and GETDATE, used for obtaining the current date and time, are two examples of function use. User Defined Functions enable the developer or DBA to create functions of their own, and save them inside SQL Server.

View Complete Post

More Related Resource Links

Creating a table-valued function on a user defined type in SQL Server CLR

We would like to be able to create a table-valued function on a user defined type.  We would like the syntax in SQL to look like it works for the XML nodes function:   DECLARE @myXml XML = '<a><b>1</b><b>2</b><b>3</b></a>' ; SELECT  node.query('text()') FROM    @myXml.nodes('a/b') nodes (node) ;   In other words, in the FROM clause, we can access the "nodes" method of the xml variable and it returns a rowset. Thanks!

Best practice for user defined error messages in SQL Server 2005/2008 --Need help

My requirement is to setup a standard practice for our team in handling DB erros and also  user defined messages. We are handling exceptions using Raiseerror method with in Try and Catch blocks.  For user defined message, we are using Sp_addmessage to add our application specific error/warning messages with error codess>80000.  Ex: 80001 | Company name already exists. please check. Here is the sample code ********************************************************************* CREATE PROCEDURE [dbo].[usp_CompanyProfile_insert]  -- Add the parameters for the stored procedure here  (@company_name VARCHAR(50),  @company_code VARCHAR(3),  @user_id INT  ) AS BEGIN  -- SET NOCOUNT ON added to prevent extra result sets from  -- interfering with SELECT statements.  SET NOCOUNT ON;  BEGIN TRY -- Begin Try Block      -- Validate company name   IF EXISTS (SELECT company_id FROM companyprofile WHERE company_name = @company_name)    RAISERROR(90021, 16, 1)      INSERT INTO companyprofile (company_name,       company_code,       createdby)   VALUES(@company_name,     @company_code,     @user_id)    END TRY  -- End Try Block &

User does not have permission to perform this action( Microsoft SQL Server, Error:15247)



I was able to do my work with ease on my Database server, until sometime Friday evening. Don't exactly know what transpired, only that I can now no longer run queries or even create new databases. Here is a sample of what happens when I try to create a database

Create Database PetitionSample

Msg 262, Level 14, State 1, Line 1

CREATE DATABASE permission denied in database 'master'.

I used to be able to create databases, create new users and so on. My Server is installed on a Vista machine and I am running Developer version and using Management studio 2008 to access it. Anyone have any idea what could be going on?


The login already has an account under a different user name. (Microsoft SQL Server, Error: 15063)





I have two databases xxx and yyy.

I added a new user to xxx named as "Admin". It ownes some schemas.

Afterwards I added a new user to yyy named as "Admin". It ownes the same schemas.

Then I pressed ok button, got the error:" The login already has an account under a different user name. (Microsoft SQL Server, Error: 15063)".



Could you please give me some advice?



SQL Server user-defined aggregate returns an error if aggregating 0 rows

I have this SQL Server user-defined aggregate:

[SqlUserDefinedAggregate(Format.UserDefined, Name="median", IsInvariantToDuplicates=false, IsInvariantToNulls=true, IsInvariantToOrder=true, IsNullIfEmpty=true, MaxByteSize=8000)] 
public class MedianCalculator : IBinarySerialize { 
    private List<double> values; 
    public void Init() { 
        values = new List<double>(); 
    public void Accumulate(SqlDouble value) { 
        if (!value.IsNull) 
    public void Merge(MedianCalculator other) { 
    public SqlDouble Terminate() { 
        if (values == null || values.Count == 0) 
            return SqlDouble.Null; 
        return (values[(int)Math.Floor((values.Count - 1) / 2.0)] + values[(int)Math.Ceiling((values.Count - 1) / 2.0)]) / 2.0; 
    public void Read(BinaryReader r) { 
        int c = r.ReadInt32(); 
        values = new List<double>(c); 
        for (int i = 0; i < c; i++) 
    public void Write(BinaryWriter w) { 
        for (int i = 0; i < values.Count; i++) 
After deploying this aggregate, I try to run this query:

Microsoft Accounting Professional 2009 - controlling user access on SQL Server 2005


I have MS Accounting Professional 2009 installed on Windows Small Business Server 2008 with the accounting database in SQL Server 2005

I can access the database on the server using my client installation of MS Accounting and use it as expected

When I add another user I am unable to restrict the access rights of that user by selecting a role for them and restricting their access by denying them access to certain areas or providing read-only access to certain areas

What happens when we select a user and select a role and un-tick the Administrator rights box and click ok is that the user rights are not saved without administrator rights.  When the user's rights are opened again, the Administrator box has a tick in it again.

The sequence of events is exactly as described - untick the Administrator box, click ok, then select the user in the "Manage Users" list again, click "Edit" and there's a tick in the "Administrator" box.

In every other respect, MS Accounting appears to work ok, i.e. no error messages, no crashes.

We believe that we should be able to work around this issue by manually setting user access rights directly in the SQL database, but so far we have not been able to do that.

Has anyone ever experienced anything like this, can anyone provide any help or suggestions?

Login failed for user sa Microsoft Sql Server 18456


Its unbelievebale that I am not able to login using my sa password .I am setting it by loggin into windows mode still it is giving error

Login failed for user sa Microsoft Sql Server 18456



Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456, Severity: 14, State: 1) When I us


When I use the following setting to connect server, it succeeds
Server type: Database Engine
Server name: Home-PC
Authentication: Windows Authentication

User name: Home-PC\John

However, when I try to use the following setting to connect the same server, it fails as always
Server type: Database Engine
Server name: Home-PC
Authentication: SQL Server Authentication 

The Error Message as follows
TITLE: Connect to Server
Cannot connect to Home-PC.
Login failed for user 'sa'. (Microsoft SQL Server, Error: 18456)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=18456&LinkId=20476

Then, I enter the server to change some

how to pass a user defined object as an argument to a web server?


I have the following scenario:

1. my web server is in c#, and my web server client is in Java

2. on the java side, I have a user defined object,  (it is implemented as a java bean) and i would like to pass this object, as an argument to one of the web server methods.

3. the problem however is, that on the C# side, i get a compilation error, saying that the type or namespace could not be found.

I read some forum threads saying that it should be serialised, but i have no idea of what to do, to be quite honest. Could someone please help by either explanation or a helpful link with an example?

Many thanks,


MS SQL Server: Disconnect Users From Database - Kill User Session

If you ever wanted to restore your database from a SQL backup file (.bak), but there are still users connected to your database, the backup operation will fail causing the error: Exclusive access could not be obtained because the database is in use.

CLR Integration With Microsoft SQL Server

Transact-SQL provides as much flexibility as possible but, because it is not a real programming language, it has some shortcomings about many issues (memory management, exception handling, debugging, file processing (or streaming), object-orientation (classes), or thread management). Transact-SQL also lacks features of normal libraries (arrays, collections, serialization, expressions, or string manipulations, etc).

Configuring User Profiles in SharePoint Server 2010

Note: I've added the necessary links relating to SharePoint Server 2010 prerequisites, along with instructions for configuring the user profile synchronization service, at the end of this

Essential SQL Server Date, Time and DateTime Functions

The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any formatting considerations at all.

"Type ASP.x is not defined" error for dynamic user control when publishing updateable


I have a user control (ContainerCtrl) which programmatically instantiates another user control (SimpleCtrl) in OnLoad. It builds and runs fine, but when I publish it I get the error "error BC30002: Type 'ASP.SimpleCtrl' is not defined." (If I publish without the "updateable" option there's no error, but I need the site to be updateable.)

I assume this has to do with the particulars of how the assembly(ies) are built for the different compiler options. My question is, how can I get it to work properly for "updateable" publishing?

VS 2008 / .NET 3.5 sp1

The code follows (web.config is the default from Visual Studio).

Many thanks, Dave


<%@ Control Language="VB" ClassName="SimpleCtrl" CodeFile="SimpleCtrl.ascx.vb" Inherits="_SimpleCtrl" %>

    Simple control


Partial Class _SimpleCtrl
    Inherits UserControl

    'Nothing to see here.

End Class


<%@ Control Language="VB" CodeFile="ContainerCtrl.ascx.vb" Inherits="_ContainerCtrl"

No ASPNET User in Windows Server 2008?


I have been looking everywhere online regarding this, but have not found a clear answer.


I have an ASP.Net app running on Windows Server 2008, it's basically a reporting app using Crystal Reports. I have encountered a roadblock in regards to rendering some pages. When I run the app in Visual Studio(on the same 2008 machine) everything works perfectly. Yet when I publish it and run it live the reports will not display correctly, I get a message saying Failed to render page.


I looked this error up in the Crystal Reports website and the fixes they have there refer to giving the windows "aspnet" user full access rights to certain folders on the machine. I would be happy to do this, however there is no aspnet user in Windows Server 2008.


I installed the server and set its role to be Web Server. I also checked all the options regarding ASP.Net and IIS 6 compatibility. Yet there is no ASPNET username anywhere.


Did I set the server up wrong? or do I have to do something within ASP.Net to  create this user and then give it rights? 

Security Briefs: Add a Security Bug Bar to Microsoft Team Foundation Server 2010


Take a peek inside Microsoft's strict development security structure as Bryan Sullivan describes the objective security bug classification system?the "bug bar"?used by internal product and online services teams. He will show you how to incorporate this classification system into your own development environment using Microsoft Team Foundation Server 2010.

Bryan Sullivan

MSDN Magazine March 2010

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend