Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Calling Stored SQL Functions from Visual Basic

Calling Stored SQL Functions from Visual Basic

Scheduled Pinned Locked Moved Database
helpcsharpdatabasesql-server
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Cabbage
    wrote on last edited by
    #1

    Hi Everyone, Wasn't sure if this was a VB or SQL question and where to post so I am trying here. I read it is better to use stored procedures and functions on the SQL server to centralise and re-use code, as well as allowing us to restrict rights to EXECUTE rather than full rights. I wanted to retrieve (and maybe in another function SET), some data so I wrote the following basic function and stored it on the server. My problem is that no matter how I try, I can't seem to find a way to call the function from my VB.NET 2005 application. Please help ALTER FUNCTION dbo.GetEmployeeFont ( @EmployeeID int ) RETURNS TABLE AS RETURN (SELECT FontName, FontSize FROM tblEmployees WHERE (EmployeeID = @EmployeeID))

    P P 2 Replies Last reply
    0
    • M Mark Cabbage

      Hi Everyone, Wasn't sure if this was a VB or SQL question and where to post so I am trying here. I read it is better to use stored procedures and functions on the SQL server to centralise and re-use code, as well as allowing us to restrict rights to EXECUTE rather than full rights. I wanted to retrieve (and maybe in another function SET), some data so I wrote the following basic function and stored it on the server. My problem is that no matter how I try, I can't seem to find a way to call the function from my VB.NET 2005 application. Please help ALTER FUNCTION dbo.GetEmployeeFont ( @EmployeeID int ) RETURNS TABLE AS RETURN (SELECT FontName, FontSize FROM tblEmployees WHERE (EmployeeID = @EmployeeID))

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      You can't call the function directly. You should write a stored procedure that acts as a wrapper for the function to call it from your VB application. Paul

      M 1 Reply Last reply
      0
      • P pmarfleet

        You can't call the function directly. You should write a stored procedure that acts as a wrapper for the function to call it from your VB application. Paul

        M Offline
        M Offline
        manuo5
        wrote on last edited by
        #3

        You cannot use sqlserver function in vb.net .Create one sp in sqlserver and call this fun with in sp . -- modified at 5:30 Friday 7th September, 2007 Manu

        1 Reply Last reply
        0
        • M Mark Cabbage

          Hi Everyone, Wasn't sure if this was a VB or SQL question and where to post so I am trying here. I read it is better to use stored procedures and functions on the SQL server to centralise and re-use code, as well as allowing us to restrict rights to EXECUTE rather than full rights. I wanted to retrieve (and maybe in another function SET), some data so I wrote the following basic function and stored it on the server. My problem is that no matter how I try, I can't seem to find a way to call the function from my VB.NET 2005 application. Please help ALTER FUNCTION dbo.GetEmployeeFont ( @EmployeeID int ) RETURNS TABLE AS RETURN (SELECT FontName, FontSize FROM tblEmployees WHERE (EmployeeID = @EmployeeID))

          P Offline
          P Offline
          Paddy Boyd
          wrote on last edited by
          #4

          I'm not sure what these people are talking about, but it's just the same as executing any other bit of sql: SELECT FontName, FontSize FROM dbo.GetEmployeeFont(@EmployeeId) Just run this with a sql command and return a reader (or whatever suits your code). (Although, that said, it seems to be a more useful thing to have as a stored procedure. Functions are generally more useful when you will re-use them in other bits of sql.)

          P M 2 Replies Last reply
          0
          • P Paddy Boyd

            I'm not sure what these people are talking about, but it's just the same as executing any other bit of sql: SELECT FontName, FontSize FROM dbo.GetEmployeeFont(@EmployeeId) Just run this with a sql command and return a reader (or whatever suits your code). (Although, that said, it seems to be a more useful thing to have as a stored procedure. Functions are generally more useful when you will re-use them in other bits of sql.)

            P Offline
            P Offline
            pmarfleet
            wrote on last edited by
            #5

            My statement is still correct. A function cannot be called directly. The call must be made from either a stored procedure or inline SQL statement. Paul

            P 1 Reply Last reply
            0
            • P pmarfleet

              My statement is still correct. A function cannot be called directly. The call must be made from either a stored procedure or inline SQL statement. Paul

              P Offline
              P Offline
              Paddy Boyd
              wrote on last edited by
              #6

              Yes, but what benefit is gained from wrapping the function in a stored procedure? It's just another level of code to go wrong...

              P 1 Reply Last reply
              0
              • P Paddy Boyd

                Yes, but what benefit is gained from wrapping the function in a stored procedure? It's just another level of code to go wrong...

                P Offline
                P Offline
                pmarfleet
                wrote on last edited by
                #7

                I never use inline SQL statements. They may be quicker to code, but the advantages of using stored procedures (maintainability, robustness, security) more than outweigh the extra code overhead.

                P 1 Reply Last reply
                0
                • P pmarfleet

                  I never use inline SQL statements. They may be quicker to code, but the advantages of using stored procedures (maintainability, robustness, security) more than outweigh the extra code overhead.

                  P Offline
                  P Offline
                  Paddy Boyd
                  wrote on last edited by
                  #8

                  I've been through this discussion a few times with different people and I think that it's more of a religious type decision than anything else. We may just have to agree to differ.

                  1 Reply Last reply
                  0
                  • P Paddy Boyd

                    I'm not sure what these people are talking about, but it's just the same as executing any other bit of sql: SELECT FontName, FontSize FROM dbo.GetEmployeeFont(@EmployeeId) Just run this with a sql command and return a reader (or whatever suits your code). (Although, that said, it seems to be a more useful thing to have as a stored procedure. Functions are generally more useful when you will re-use them in other bits of sql.)

                    M Offline
                    M Offline
                    Mark Cabbage
                    wrote on last edited by
                    #9

                    Thanks Paddy. I need to return some values and use them so I think the function would be best, but there seems to be some debate here if it can be achieved. Excuse my ignorance but my previous coding was for MSAcess so I"m trying to learn SQL server issues, and move from VBA to VB.NET. I have put in the SELECT statement you sugessted but the compiler tells me "Select CASE must end with a matching END SELECT", and hovering over FROM in the statement gives me "name not declared" . Obviously I need more code to setup the SQL statement - could you kindly give me an example please. Many thanks Mark

                    P M 2 Replies Last reply
                    0
                    • M Mark Cabbage

                      Thanks Paddy. I need to return some values and use them so I think the function would be best, but there seems to be some debate here if it can be achieved. Excuse my ignorance but my previous coding was for MSAcess so I"m trying to learn SQL server issues, and move from VBA to VB.NET. I have put in the SELECT statement you sugessted but the compiler tells me "Select CASE must end with a matching END SELECT", and hovering over FROM in the statement gives me "name not declared" . Obviously I need more code to setup the SQL statement - could you kindly give me an example please. Many thanks Mark

                      P Offline
                      P Offline
                      Paddy Boyd
                      wrote on last edited by
                      #10

                      Do you have a case statement in your sql somewhere?

                      1 Reply Last reply
                      0
                      • M Mark Cabbage

                        Thanks Paddy. I need to return some values and use them so I think the function would be best, but there seems to be some debate here if it can be achieved. Excuse my ignorance but my previous coding was for MSAcess so I"m trying to learn SQL server issues, and move from VBA to VB.NET. I have put in the SELECT statement you sugessted but the compiler tells me "Select CASE must end with a matching END SELECT", and hovering over FROM in the statement gives me "name not declared" . Obviously I need more code to setup the SQL statement - could you kindly give me an example please. Many thanks Mark

                        M Offline
                        M Offline
                        Mark Cabbage
                        wrote on last edited by
                        #11

                        No, and I don't want to either. It issues these errors when I hover over the "select fontname, fontsize....." In the old VBA I used to call the functions like: Dim Fontsize as integer fontsize = GetFontSize(strCurrentUser) ---- Function GetFontSize(CurentUser as string) as Integer ...... ...... ----- This meant I was able to use a single like to get my values and use them in lots of places easily. When I read I should store these server-side I was very happy. I just need to call the function and return it to my current sub easily without having to do too much coding to setup and call the function otherwise it defeats the purpose FYI I spent all weekend playing with the new error provider - very nice but it took me the longest time to work it out nicely....MS documentation is horrible....I found Google far better than MS own doccu... Mark

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups