Results 1 to 5 of 5

Thread: [RESOLVED] SQL Stored procedures in VB & ADO

  1. #1

    Thread Starter
    Lively Member nvierros's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    110

    Resolved [RESOLVED] SQL Stored procedures in VB & ADO

    Hey guys,

    Wondering if someone could help me out

    basically i have..

    VB Code:
    1. rs.open "SELECT customerid, custfname, datefrom, dateto, delstats, status FROM Invoices WHERE datefrom='" & getdate & "' And not status= 'Cancelled' order by customerid", con, adOpenKeyset, adLockReadOnly

    Now, how could i convert that to a stored procedure in MS SQL and then execute that procedure from VB using ADO?

    Help greatly appreciated

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Stored procedures in VB & ADO

    First - with ado you use a command object with parameter objects to talk to SPROCS.

    Code:
        With objCmd
            .CommandText = "GetSqlSP"
            .ActiveConnection = gCn
            .CommandType = adCmdStoredProc
            .Parameters.Append .CreateParameter("@StoredProc", adVarChar, adParamInput, 50, strSP)
            Set rsParam = .Execute()
        End With
    The SPROC is defined basically as:

    Code:
    USE Acctfiles
    GO
    SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GO
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSqlSP]') 
    and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[GetSqlSP]
    GO
    CREATE PROCEDURE GetSqlSP
    	 @StoredProc varchar(50)
    AS
    Select Parameter_Name,Data_Type,Parameter_Mode,Character_Maximum_Length
    From INFORMATION_SCHEMA.Parameters
    Where SPECIFIC_NAME = @StoredProc 
    Go
    GRANT EXECUTE ON GetSqlSP TO AcctfilesUser
    Go
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
    That script does a lot of other stuff before and after creating the SPROC - but you should get the idea...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member nvierros's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    110

    Re: SQL Stored procedures in VB & ADO

    Is this ok?

    I created a SPROC

    Code:
    CREATE PROCEDURE [dbo].[getcal] @myinput smalldatetime as 
    
    SELECT customerid, custfname, datefrom, dateto, delstats, status FROM Invoices 
    WHERE datefrom=@myinput And not status= 'Cancelled' order by customerid
    GO
    and my vbcode:

    VB Code:
    1. set rs = new recordset
    2. Set rs = con.Execute("EXEC getcal @myinput=" & "'" & getdate & "'")

    This appears to do what i want it to do, but is the correct way of doing it?

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Stored procedures in VB & ADO

    That is acceptable.

    The CONNECTION does have an EXECUTE - you can use it. I believe that you have less options for type of CURSOR and location of CURSOR.

    Also, you are not using the PARAMETERS object - which means you can only pass parameters. When you use the COMMAND and PARAMETER objects you can have both input and output parameters.

    Maybe others on the board can shed some light on other differences or limitations.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Stored procedures in VB & ADO

    Maybe others on the board can shed some light on other differences or limitations.
    If you needed to execute a stored procedure several times within a loop. It would be more efficient to use the Command object.

    Using Parameters you don't have to worry about things like formatting date strings correctly or replacing single quotes with two single quotes, like you do when creating in-line sql statements.

    FYI - your code may fail on computers whose regional date setting is not mm/dd/yyyy.
    Using Set rs = New Recordset line is pointless when calling the Execute method. The Execute method creates a new recordset destroying any instance that the rs variable may already contain.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width