Wednesday, 9 February 2011

How to write Stored Procedure in SQL Server 2005 ?


Hi Friends,
This is a very small thing for a developer but as beginner some time we hang up into write a stored procedure in sql server 2005 or sql server 2008. So I am giving a example of how to write stored procedure for insert data into database:

Set ANSI_NULLS ON
Set QUOTED_IDENTIFIER ON
Create Procedure [dbo].[sp_RegInsert]
                @RegID int,
                @FullName nvarchar(50),
                @Address nvarchar(MAX),
                @ImageUrl nvarchar(MAX)
AS
Begin
                Select @RegID = IsNull(max(RegID),0) + 1 from tblReg
                Insert Into tblReg (
                RegID,
                FullName,
                Address,
                ImageUrl)
Values (
                @RegID,
                @FullName,
                @Address,
                @ImageUrl)
END

Thanks to All...


4 comments:

  1. you can also use like this which is use for insert and update both and how to find max id of that table.

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    create PROCEDURE [dbo].[sp_RegInsert]

    @RegID int,
    @FullName nvarchar(50),
    @Address nvarchar(MAX),
    @ImageUrl nvarchar(MAX)
    @vRegOutputID int Output

    AS
    if @RegID =0
    Begin
    Declare @vNewId int
    Select @vNewId=Coalesce(Max(RegID),0)+1 from tblReg
    Insert Into tblReg(RegId, FullName,Address,ImageUrl)
    Values (@vNewId, @FullName,@Address,@ImageUrl)
    Set @vRegOutputID =@vNewId
    End
    else
    Begin
    Update tblReg Set FullName=@FullName,Address=@Address,ImageUrl=@vImageUrl Where Regid=@RegID
    Set @vRegOutputID =@RegID
    End
    it very nice
    thanks
    gurukulsoftwares.com

    ReplyDelete
  2. nice article find more info on http://www.way2resources.com

    ReplyDelete