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. UDF, SPROC in SQL Server Express 2008

UDF, SPROC in SQL Server Express 2008

Scheduled Pinned Locked Moved Database
databasequestioncsharpasp-netsql-server
3 Posts 2 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.
  • D Offline
    D Offline
    Dirso
    wrote on last edited by
    #1

    Hi, I'd like to put all my database management in StoredProcedures or Functions, but I'm having a small problem: 1) I know I can't use INSERT, UPDATE or DELETE in UDFs, right? 2) So i created a SPROC that inserts a record to a simple table (id, name) and returns the ID in an output parameter. It's working fine. 3) I create a UDF that executes this SPROC and returns the ID... and here is my problem... looks like I can't execute my own SPROCs from a UDF. :( 4) So.. how do I achieve this goal? Inserting a record and returning the auto generated id back to my web browser (asp.net application). Here is some of my code:

    CREATE PROCEDURE finsTeste
    @snome varchar(50),
    @nid int output
    AS
    BEGIN
    SET NOCOUNT ON;
    INSERT INTO teste (nome) VALUES (@snome)
    SELECT @nid = SCOPE_IDENTITY()
    END
    GO
    CREATE FUNCTION insTeste
    (
    @snome varchar(50)
    )
    RETURNS int
    AS
    BEGIN
    DECLARE @nid int
    EXECUTE finsTeste @snome, @nid output
    RETURN @nid;
    END
    GO

    And then in the asp.net page I run a ExecuteScalar(StoredProcedure, "insTeste", params);

    Thanks, Dirso

    I 1 Reply Last reply
    0
    • D Dirso

      Hi, I'd like to put all my database management in StoredProcedures or Functions, but I'm having a small problem: 1) I know I can't use INSERT, UPDATE or DELETE in UDFs, right? 2) So i created a SPROC that inserts a record to a simple table (id, name) and returns the ID in an output parameter. It's working fine. 3) I create a UDF that executes this SPROC and returns the ID... and here is my problem... looks like I can't execute my own SPROCs from a UDF. :( 4) So.. how do I achieve this goal? Inserting a record and returning the auto generated id back to my web browser (asp.net application). Here is some of my code:

      CREATE PROCEDURE finsTeste
      @snome varchar(50),
      @nid int output
      AS
      BEGIN
      SET NOCOUNT ON;
      INSERT INTO teste (nome) VALUES (@snome)
      SELECT @nid = SCOPE_IDENTITY()
      END
      GO
      CREATE FUNCTION insTeste
      (
      @snome varchar(50)
      )
      RETURNS int
      AS
      BEGIN
      DECLARE @nid int
      EXECUTE finsTeste @snome, @nid output
      RETURN @nid;
      END
      GO

      And then in the asp.net page I run a ExecuteScalar(StoredProcedure, "insTeste", params);

      Thanks, Dirso

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      Why do you need to create a UDF? You can get the value that you need directly from the SP.

      D 1 Reply Last reply
      0
      • I i j russell

        Why do you need to create a UDF? You can get the value that you need directly from the SP.

        D Offline
        D Offline
        Dirso
        wrote on last edited by
        #3

        I didn't know that... I thought I had to execute. Thank you very much! I just tried and it worked great!

        Thanks, Dirso

        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