Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Insert Stored Procedure with null parameters

Course Queries Syllabus Queries
Max. 2000 characters
Replies

usr_profile.png
Amie Khalifa

User

( 5 months ago )

 

I want to create an insert stored procedure with null parameters, if pass the value for that parameter then it has to insert or update in database

My stored procedure is:

Create proc [dbo].[SP_InsertOrUpdateCourseDetails]
    @CourseID int,
    @Tab1Title nvarchar(250) = null,
    @Tab1Description nvarchar(max) = null,
    @Tab2Title nvarchar(250) = null,
    @Tab2Description nvarchar(max) = null,
    @Tab3Title nvarchar(250) = null,
    @Tab3Description nvarchar(max) = null,
    @Tab4Title nvarchar(250) = null,
    @Tab4Description nvarchar(max) = null,
    @Syllabus nvarchar(max) = null
As
Begin

    If NOT EXISTS (Select * from CourseDetail Where CourseID=@CourseID )
    Begin
        Insert into CourseDetail(CourseID, Tab1Title, Tab1Description, 
                                 Tab2Title, Tab2Description,
                                 Tab3Title, Tab3Description, Tab4Title, Tab4Description,
                                 Syllabus)
        values (@CourseID, @Tab1Title, @Tab1Description, @Tab2Title, @Tab2Description,
                @Tab3Title, @Tab3Description, @Tab4Title, @Tab4Description, @Syllabus)

        IF @@ERROR = 0 AND @@ROWCOUNT =1
        Begin
            Select top 1 CourseID from CourseDetail Order by CourseDetailID Desc
        End
        Else 
        Begin
             Select 0
        End
    End
 Else
 Begin
     Update CourseDetail 
     SET
        Tab1Title = @Tab1Title,
        Tab1Description = @Tab1Description,
        Tab2Title = @Tab2Title,
        Tab2Description = @Tab2Description,
        Tab3Title = @Tab3Title,
        Tab3Description = @Tab3Description,
								 								 
							usr_profile.png
							
Garry Buttler

User

( 5 months ago )

You can use ISNULL function passing in it SP's parameter as first parameter and default value in case of insert or actual column's value in case of update:

    Insert into CourseDetail(CourseID, Tab1Title, Tab1Description, 
                             Tab2Title, Tab2Description,
                             Tab3Title, Tab3Description, Tab4Title, Tab4Description,
                             Syllabus)
    values (@CourseID, ISNULL(@Tab1Title, 'default value'), ISNULL(@Tab1Description,'default value'), ...


 Update CourseDetail 
 SET
    Tab1Title = ISNULL(@Tab1Title, Tab1Title),
    Tab1Description = ISNULL(@Tab1Description, Tab1Description),
    ....
 Where 
    CourseID = @CourseID

But in this case you won't can to set NULL value explicitely even if you'll need

what's your interest