MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Getting started with SQL Server stored procedures -

in Search

Getting started with SQL Server stored procedures

Last post 09-18-2008 7:54 AM by aprato. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 05-09-2008 12:30 AM

    Getting started with SQL Server stored procedures

    This post is related to this tip: Getting started with SQL Server stored procedures

    http://www.mssqltips.com/tip.asp?tip=1495

  • 09-18-2008 12:08 AM In reply to

    • mateia
    • Top 100 Contributor
    • Joined on 09-18-2008
    • Posts 3

    Re: Getting started with SQL Server stored procedures

    Hello,

    Related to this code:

    CREATE PROCEDURE uspGetContact
    AS
    SELECT
     ContactIDFirstNameLastName
    FROM Person.Contact
    EXEC uspGetContact

    After calling EXEC uspGetContact, I would like in Transact-SQL to get the actual rows (dataset) selected by the uspGetContact stored procedure. Is it possible to have something like this:

    EXEC uspGetContact

    SELECT * FROM DataSetReturnedByuspGetContact ??

    If it's not possible to do this in Transact-SQL, then how can application programs written in any language: e.g. C++, Delphi, Visual Basic receive a recordset/dataset without any modification of the 'uspGetContact' stored procedure ?? 

     

    Thank you very much

     

     

  • 09-18-2008 6:42 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 190

    Re: Getting started with SQL Server stored procedures

    There are a few ways.  Below is one approach.

     

     CREATE PROCEDURE dbo.uspGetContact
    AS
    SET NOCOUNT ON

    SELECT ContactID, FirstName, LastName
    FROM Person.Contact

    return @@rowcount
    go

    declare @rows int
    exec @rows = dbo.uspGetContact
    select @rows

  • 09-18-2008 6:51 AM In reply to

    • mateia
    • Top 100 Contributor
    • Joined on 09-18-2008
    • Posts 3

    Re: Getting started with SQL Server stored procedures

    Hello,

    Thank you for your reply. 

    However, maybe I wasn't clear enough, after calling the stored procedure I  need the actual selected rows, not the number of selected rows.

    I'm afraid is not possible without an OUTPUT parameter returning the selected rows in a cursor.

     

     

  • 09-18-2008 6:57 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 190

    Re: Getting started with SQL Server stored procedures

     Your language of choice does not have a ResultSet type object?

    In T-SQL, you can create a temp table/table variable to capture outputted rows.

  • 09-18-2008 7:11 AM In reply to

    • mateia
    • Top 100 Contributor
    • Joined on 09-18-2008
    • Posts 3

    Re: Getting started with SQL Server stored procedures

    Hello

    >In T-SQL, you can create a temp table/table variable to capture outputted rows.

    Please see the example below:

    USE [Northwind]
    GO

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE PROCEDURE SelectCustomers
    AS
    BEGIN  
       SELECT * FROM Customers
    END
    GO


    EXEC [SelectCustomers]


    How can I access here in Transact-SQL, the rows returned by the  SelectCustomers stored procedure ? 

  • 09-18-2008 7:54 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 190

    Re: Getting started with SQL Server stored procedures

     You create a temp table

     

    CREATE PROCEDURE SelectCustomers
    AS
    BEGIN  
       SELECT * FROM Customers
    END
    GO

    create table mytable(column_list....)

    insert into mytable(column_list....)
    EXEC [SelectCustomers]

     

Page 1 of 1 (7 items)