Scalar user defined functions in sql server Part 30

Sdílet
Vložit
  • čas přidán 21. 07. 2024
  • In this video we will learn about
    1. User Defined Functions in sql server
    2. Types of User Defined Functions
    3. Creating a Scalar User Defined Function
    4. Calling a Scalar User Defined Function
    5. Places where we can use Scalar User Defined Function
    6. Altering and Dropping a User Defined Function
    Text version of the video
    csharp-video-tutorials.blogspo...
    Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our CZcams channel. Hope you can help.
    / @aarvikitchen5572
    Slides
    csharp-video-tutorials.blogspo...
    All SQL Server Text Articles
    csharp-video-tutorials.blogspo...
    All SQL Server Slides
    csharp-video-tutorials.blogspo...
    All Dot Net and SQL Server Tutorials in English
    czcams.com/users/kudvenka...
    All Dot Net and SQL Server Tutorials in Arabic
    / kudvenkatarabic

Komentáře • 92

  • @wasimbader9170
    @wasimbader9170 Před 8 lety +13

    THANK YOU SIR, I HOPE U DO FULL TUT ON SAS/SPSS OR DATA ANALYSIS WITH FEW PROGRAMS

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  Před 8 lety +10

      +Wasim Bader Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful.
      At the moment, we don't have videos on the technologies you mentioned. I will record and upload as soon as I can. Thank you very much for your patience.
      Dot Net & SQL Server training videos for web developers
      czcams.com/users/kudvenkatplaylists?view=1&sort=dd
      You can order DVDs for offline viewing using the link below
      www.pragimtech.com/Order.aspx
      Code Samples & Slides are on my blog
      csharp-video-tutorials.blogspot.com
      Tips to effectively use our channel
      czcams.com/video/y780MwhY70s/video.html
      To receive email alerts, when new videos are uploaded, please subscribe to our channel
      czcams.com/users/kudvenkat
      Please click that THUMBS UP button below the video, if you like the videos
      Thanks a million for sharing these resources with your friends
      Best
      Venkat

  • @ouruzbekhomeschool1334
    @ouruzbekhomeschool1334 Před rokem +6

    11 years later, and yet still benefiting from his lessons immensely. Thank you a lot!

  • @gawiga
    @gawiga Před 5 lety +10

    Your indian accent is so cool.
    Thank you for give this knowledge for free. Greetings from Brazil.

  • @lizravenwood5317
    @lizravenwood5317 Před 5 lety +7

    I am so in love with your teaching! I cannot believe how brilliant you are with all of these different technologies as well as the ability and willingness to teach and share. Thank you so much. Namaste.

  • @anuragvashishtha6660
    @anuragvashishtha6660 Před 11 lety +3

    I will recommend these videos to every SQL Server student. Thanks a lot Venkat.

  • @amantlemashele5719
    @amantlemashele5719 Před 4 lety

    You have been improving my coding since 2015, from C# to MVC to SQL

  • @devikasiddani
    @devikasiddani Před 4 lety

    Hi Venkat, All your videos are simple and clear which makes anyone easy to understand, your tutorials are like a Gift, Thank you so much.

  • @rgarayia
    @rgarayia Před 8 lety

    Hi Venkat, Want to thank you for your wonderful videos. They are simply great!!

  • @prabdeepdhaliwal9725
    @prabdeepdhaliwal9725 Před 3 lety +5

    So I was trying to create the CalculateAge() function myself and display the table with Name, DOB and Age before you showed us how to. I got the age values that were 10 years higher than your values and was confused on what I did wrong. Then I remembered the video is like 10 years old so all those people aged by 10 😂

  • @hongshen1702
    @hongshen1702 Před 7 lety +1

    Thank you Kudvenkat so much for creating this series of SQL training videos for beginners. They are so easy to understand with the tables and query demonstrations. You are very good at teaching. I learn a few videos each day and decide to finish them all. Again, many thanks.
    Do you teach Python? Hope you have some videos too.

  • @Csharp-video-tutorialsBlogspot

    Hi zainzoom,
    To invoke a scalar function prefix dbo. before the name of the function. For example, if you have order with Id=101, then invoke the function as shown below.
    Select dbo.GetTotalOrderValue(201), SomeOtherColumn
    from SomeTable
    where SomeCondition
    I hope this will resolve your issue. If not let me know.

  • @tksafi6075
    @tksafi6075 Před 6 lety +2

    thanks man your sql videos are the best on youtube

  • @mdfaysal8233
    @mdfaysal8233 Před 5 lety

    Thank you Sir for a nice tutorial. All of your tutorials are so much easy to understand for every level of learners.

  • @mdasadujjaman5342
    @mdasadujjaman5342 Před 6 měsíci

    12 years later, and still benefiting from his lessons immensely. Thanks a lot Sir.!

  • @RPTech-ic2ed
    @RPTech-ic2ed Před 8 lety +1

    very help full stuff, great job

  • @xswooshx
    @xswooshx Před 6 lety

    Thanks for the video! Just became a subscriber.
    Question: Is it possible to create a function in a 'general' location so that it can be called from any database that you might be using?

  • @vaniagarwal5800
    @vaniagarwal5800 Před 3 lety +1

    Your videos are just amazing. THANK YOU VERY MUCH SIR.

  • @brahmanathtripathi2213
    @brahmanathtripathi2213 Před 10 lety

    Hi Venkat, Your video help me a lot. I like all your video. A small request this video blur some part so that not clearly visible of creating function so please upload again with good visibility

  • @dbin805
    @dbin805 Před 8 lety

    another clear and concise explanation. thanks for this.

  • @QuietLumina
    @QuietLumina Před 4 měsíci

    This is so incredibly clear and helpful. Thank you so much!

  • @AdrianFusco
    @AdrianFusco Před 6 lety

    Thank you for the video tutorials. You're wonderful.

  • @nerellapydiraju2320
    @nerellapydiraju2320 Před 10 lety

    Very helpful videos for beginners, Thank you very much

  • @maheshgore1066
    @maheshgore1066 Před 3 lety

    Great session...very informative and complete

  • @pucca9090
    @pucca9090 Před 10 lety

    I love your session so much. Thanks for help me

  • @yamaya5136
    @yamaya5136 Před 2 lety

    Watching you in 2022. Your teaching style is very easy to grasp. God bless!

  • @nandadk314
    @nandadk314 Před 6 lety

    Hi Sir, you said the returned value can not be of cursor.. But we are using a function and returning p_recordset which is a sys ref cursor.. can you explain... my function is as below:
    Create or replace Function fn_name (v_param in varchar2)
    Return sys_refcursor as p_recordset sys_refcursor;
    Begin
    Open
    OPEN p_recordset FOR
    SELECT
    FROM
    WHERE
    WHERE
    (V1."param" = V_param);
    RETURN p_recordset;
    END;

  • @gracechan3039
    @gracechan3039 Před 4 lety +1

    In Korea that actually count you as 1 year old as soon as you are born, so Koreans didn't need to add the case statement. haha.
    Seriously though, thanks for these videos. They help me so much with my job.

  • @naodagere8210
    @naodagere8210 Před 4 lety

    You are exceptional!

  • @gauranshbhutani592
    @gauranshbhutani592 Před 3 lety

    I really like your way of teaching.....Can you please provide a SAS tutorial basic to advanced??

  • @RudraPratapSinghLearning

    Awesom Video, Please tell that is there any performance issue with functions in comparison to procedures?

  • @Idhikapabi
    @Idhikapabi Před 9 lety +1

    Thanku sir... your are really great teacher.I want to meet you Sir & say many many thnx^^.... Sir please upload AJAX tutorials....

  • @danielofosuasare7757
    @danielofosuasare7757 Před 7 lety

    good work done, very useful

  • @kaushikbhadani
    @kaushikbhadani Před 10 lety

    thanks Venkat... really very very post..

  • @daliladali1268
    @daliladali1268 Před 7 lety

    thank you very much sir,very good explanation

  • @ahsansiddiqui4263
    @ahsansiddiqui4263 Před 7 lety +1

    Tony the Tigerrrr: "This is Grrrreat!"

  • @ishwakv1171
    @ishwakv1171 Před 2 lety

    Thank you sir.. U r way teaching superb sir...

  • @sarathchandhra1284
    @sarathchandhra1284 Před 10 lety

    nice explanation thanks venkat

  • @soniasadeque5950
    @soniasadeque5950 Před 7 lety

    can you give me the name of a good SQL book for databse testing? thanks for your videos Sir

  • @Superman-hr4xl
    @Superman-hr4xl Před 10 lety

    Venkat, this video on your Calculating Age Function is so Advanced and Essential. Will you (can you please make) be making a mini misc. tutorials on advanced topic such as this?

    • @artokilponen6989
      @artokilponen6989 Před 7 lety +1

      If someone else (since this question is three years old) has same problem with programming, remember that you should take these in pieces, not swallow as whole at once. In this case, make sure you understand CASE-WHEN-THEN-ELSE-END first and then advance from there.

  • @The_Alco
    @The_Alco Před 5 lety +20

    SQUARE, BRING ME MY SWORD.

  • @Nadeeraasanka
    @Nadeeraasanka Před 10 lety

    thanks for help.....I learn something ..:D

  • @kirannadukula5247
    @kirannadukula5247 Před 11 lety +1

    Hi, venkat could you please make videos on AJAX Controls. I watched a lot of videos but could not understand any of them because they are not a match to the way you explain. So, please make some videos on Ajax controls venkat. I request please...................

  • @MO-er7xp
    @MO-er7xp Před 4 lety

    you are the best bro thank you

  • @omotayoogunnika6865
    @omotayoogunnika6865 Před rokem

    Thanks for this tutorial. Can you please explain the logic behind subtracting 1 from DATEDIFF(YEAR, DOB, GETDATE)) ) when the conditions are met. Thanks..

  • @evvavinash
    @evvavinash Před 8 lety +2

    +kudvenkat, you created the function called CalculateAge and said that we can call this function in future to calculate the age. How different is it from creating a stored procedure for calculating the age and using this stored procedure to calculate the age?

    • @saurabhs4743
      @saurabhs4743 Před 8 lety +1

      +sai avinash edara the function can be used as a new column while stored procedure wont do that

    • @SuperGojeto
      @SuperGojeto Před 8 lety +1

      +sai avinash edara if you watched his videos carefully then you will find out he has explained the difference.

  • @DriveWithMunna
    @DriveWithMunna Před 5 lety

    just excellent sir

  • @deepro__
    @deepro__ Před rokem

    Thank you, sir.

  • @amitkishor7338
    @amitkishor7338 Před 7 lety +3

    make a video for different between stored procedure and function it a very important question in an interview

    • @artokilponen6989
      @artokilponen6989 Před 7 lety +2

      Check this one to begin with: stackoverflow.com/questions/2039936/difference-between-stored-procedures-and-user-defined-functions
      Procedure can return zero or n values whereas function can return one value which is mandatory.
      Procedures can have input/output parameters for it whereas functions can have only input parameters.
      Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
      Functions can be called from procedure whereas procedures cannot be called from function.
      Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
      We can go for transaction management in procedure whereas we can't go in function.
      Procedures can not be utilized in a select statement whereas function can be embedded in a select statement.
      UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be.
      UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.
      Inline UDF's can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

  • @krismaly6300
    @krismaly6300 Před 9 lety +1

    Thanks

  • @neo77jb
    @neo77jb Před 10 lety

    Great, thanx

  • @shikhilbhalla969
    @shikhilbhalla969 Před 4 lety

    Why we are using .dbo as prefix in scalar user defined function? What is the reason behind this?

  • @tarikzaman9096
    @tarikzaman9096 Před 2 lety

    Happy birthday !

  • @rdg515
    @rdg515 Před 10 lety +2

    I guess my real question is, when should you use output parameters vs functions?

  • @freddylouza7874
    @freddylouza7874 Před 8 lety

    very helpful

  • @honzekpavlicek4044
    @honzekpavlicek4044 Před 10 lety

    Nice video and explanation is great. Thx...but you can get example not about data or date of birth...:-)

  • @rdg515
    @rdg515 Před 10 lety

    Couldn't you have used spCalculateAge if you had created an output parameter?

  • @santhoshzany1451
    @santhoshzany1451 Před 2 lety

    I'm not able to run this function. Can not find column or user defined function or aggregate or name ambiguous.
    Please answer

  • @dattatrayarathi1014
    @dattatrayarathi1014 Před 4 lety

    How do I calculate age in year and month and date format?

  • @mshahzeb8926
    @mshahzeb8926 Před 4 lety

    what is the difference between functions and stored procedure.

  • @kinjalthumar814
    @kinjalthumar814 Před 3 lety

    I WRITE SAME QUERY TO CREATE FUNCTION IN MY LAPTOP SSMS BUT NOT ABLE TO CREATE FUNCTION PLEASE SIR CAN YOU EXPLAIN MORE THANK YOU

  • @aamirali8114
    @aamirali8114 Před 2 lety

    I am getting an error in the first line in @DOB Date of Create Function CalculateAge(@DOB Date), even I am getting an error when I using declare keyword: is not valid position, expecting.

  • @santoshdachepalli
    @santoshdachepalli Před 10 lety

    great!!!!!!!!!!!!!!1

  • @EmpoweringPerson
    @EmpoweringPerson Před 4 lety

    Thanx

  • @Amritha8
    @Amritha8 Před 9 lety

    Hi Venkat - Can you please write a function to find the number of Sundays in a month when you pass a date value?

    • @zackziss
      @zackziss Před 8 lety +2

      Hi Amritha, PFB script ALTER FUNCTION
      --ALTER PROC
      fn_SundayCount(@dtDate DATE)
      RETURNS INT
      AS
      BEGIN
      DECLARE @Count INT, @StartOfMonth DATE, @EndOfMonth INT, @MonthNo INT, @YearNo INT
      SET @Count = 0
      SET @EndOfMonth = 0
      SET @MonthNo = DATEPART(MM, @dtDate)
      SET @YearNo = DATEPART(YYYY, @dtDate)
      SET @StartOfMonth = CONVERT(DATE, CONVERT(CHAR(4), @YearNo) + '-' + CONVERT(VARCHAR(2), @MonthNo) + '-01')
      --SELECT CONVERT(CHAR(2), @MonthNo) AS [MonthNo], CONVERT(CHAR(4), @YearNo) AS [YearNo], @StartOfMonth AS [StartOfMonth]
      --PRINT @StartOfMonth
      IF @MonthNo = 2
      BEGIN
      IF (@YearNo % 4) = 0
      SET @EndOfMonth = 29
      ELSE
      SET @EndOfMonth = 28
      END
      ELSE IF @MonthNo >= 8
      BEGIN
      IF (@MonthNo % 2) = 0
      SET @EndOfMonth = 31
      ELSE
      SET @EndOfMonth = 30
      END
      ELSE
      BEGIN
      IF (@MonthNo % 2) = 1
      SET @EndOfMonth = 31
      ELSE
      SET @EndOfMonth = 30
      END
      WHILE (DATEPART(DD, @StartOfMonth)

    • @devexpost8508
      @devexpost8508 Před 7 lety +1

      Amritha S / zackziss,
      When finding number of Sundays in a month, the days of the month from 01 --> 28 will ALWAYS be 28 days, and will always contain 4 Sundays, no matter what. For your solution, start looping from day 29 instead of day 01 and save yourself 28 unnecessary WHILE Loop iterations on each procedure invocation...
      *Instead of doing this:*
      SET @Count = 0
      ...
      SET @StartOfMonth = CONVERT(DATE, CONVERT(CHAR(4), @YearNo) + '-' + CONVERT(VARCHAR(2), @MonthNo) + '-01')
      *do this:*
      SET @Count = 4
      ...
      SET @StartOfMonth = CONVERT(DATE, CONVERT(CHAR(4), @YearNo) + '-' + CONVERT(VARCHAR(2), @MonthNo) + '-29')
      Best Regards.

  • @adityasamanta8621
    @adityasamanta8621 Před 10 lety

    I am trying to write a function that spits out a random number. - basically I have a table in which I want to add a column with has random numbers assigned to each record
    Create Function fnRand()
    returns int
    as
    begin
    declare @num as int
    set @num= round(rand()*100,0)
    Return @num
    end
    I am getting the following error : please can you help:
    Msg 443, Level 16, State 1, Procedure fnRand, Line 7
    Invalid use of a side-effecting operator 'rand' within a function.

    • @hellofriend2012
      @hellofriend2012 Před 9 lety

      I think you cannot use RAND() function in user-defined functions

    • @archrodney
      @archrodney Před 9 lety

      Aditya Samanta You could just use built-in functions like this:
      select MyColumnList, abs(checksum(newid())) % 100 as [Random numbers] from MyTable
      OR
      select MyColumnList, cast(rand(checksum(newid())) * 100 as int) + 1 as [Random numbers] from MyTable

  • @SimpleScratch9
    @SimpleScratch9 Před 4 lety

    Hi Sir, I would like to take Microsoft 70-761 Exam. Just advise me how to pass the exam.

  • @saniyasaleem6434
    @saniyasaleem6434 Před 5 měsíci

    Sir can u upload SSRS and SSIS pls

  • @shashankpulijala3378
    @shashankpulijala3378 Před 8 lety +1

    Can we give two parameters to a function?

  • @souravkundu7051
    @souravkundu7051 Před 6 měsíci

    😊

  • @variniaquisbert4212
    @variniaquisbert4212 Před 4 lety +1

    Why do you use "dbo." before a function's name? What's dbo? Is a database name?

    • @tanaygupta5133
      @tanaygupta5133 Před 3 lety

      DBO(database owner) full form. It is a schema name usually we use before table name or fumctions.

  • @sudhapandian6577
    @sudhapandian6577 Před 7 lety

    I GOT ERROR // Conversion failed when converting date and/or time from character string.///

  • @stutitehri993
    @stutitehri993 Před 10 lety

    good videos,however, fonts for sql server appears to be small;hence less viewable

  • @coding3438
    @coding3438 Před 2 lety

    instead of case statement:
    declare @dob date
    declare @age int
    set @dob = '2021-08-28'
    select datediff(month,@dob,getdate())/12

  • @penchalababuallam2717
    @penchalababuallam2717 Před 3 lety

    Please share sample database

  • @Creator07-sx2gl
    @Creator07-sx2gl Před 2 lety

    can we please connect? I need help in one problem..please if you need this comment do connect once

  • @c-jay8500
    @c-jay8500 Před 4 lety