CodeSmith Community
Your Code. Your Way. Faster!

Custom stored procedures with dynamic sql returning void

Latest post 08-07-2007 8:43 AM by Grier. 5 replies.
  • 10-18-2006 9:12 PM

    • Dean_ve
    • Not Ranked
    • Joined on 10-19-2006
    • Posts 7
    • Points 125

    Custom stored procedures with dynamic sql returning void

    Hi Guys,

    I've just upgraded from the version 2 beta 2 to version 2.0.0.387.

    I have several custom stored procs in my app that contain construct sql dynamically and retrun the result by calling EXEC sp_executesql...

    In the beta 2 version for my procedure "usp_Address_Lookup" .netTiers generated the corresponding method "IDataReader Lookup(...)" which is what I want.

    In version 2.0.0.387 for the same procedure the return type is void instead of IDataReader i.e. "void Lookup(...)".

    This seems like a bug to me.  Below is one of the stored procs that I'm having this problem with:

    -------
    ALTER procedure [dbo].[usp_Address_Lookup]
    (
    @Info varchar(100) = NULL,
    @Unit varchar(50) = NULL,
    @Lot varchar(50) = NULL,
    @Number varchar(50) = NULL,
    @Street varchar(100) = NULL,
    @Suburb varchar(100) = NULL,
    @State varchar(10) = NULL,
    @Postcode varchar(10) = NULL,
    @MaxRows int = 10,
    @Debug bit = 0
    )
    as
    declare
    @sql nvarchar(max), @paramlist nvarchar(max)

    select @sql = 'SELECT TOP ' + CONVERT(varchar(10), @MaxRows) + ' '

    select @sql = @sql + 'AddressId, Info, Unit, Lot, Number, Street, Suburb, StateProvince, Postcode from Address'

    select @sql = @sql + ' WHERE 1 = 1'

    if (@Info IS NOT NULL) AND (@Info <> '')
        select @sql = @sql + ' AND Address.Info LIKE @xInfo + ''%'''

    if (@Unit IS NOT NULL) AND (@Unit <> '')
       
    select @sql = @sql + ' AND Address.Unit LIKE ''%'' + @xUnit + ''%'''

    if (@Lot IS NOT NULL) AND (@Lot <> '')
       
    select @sql = @sql + ' AND Address.Lot LIKE ''%'' + @xLot + ''%'''

    if (@Number IS NOT NULL) AND (@Number <> '')
       
    select @sql = @sql + ' AND Address.Number LIKE ''%'' + @xNumber + ''%'''

    if (@Street IS NOT NULL) AND (@Street <> '')
       
    select @sql = @sql + ' AND Address.Street LIKE @xStreet + ''%'''

    if (@Suburb IS NOT NULL) AND (@Suburb <> '')
       
    select @sql = @sql + ' AND Address.Suburb LIKE @xSuburb + ''%'''

    if (@State IS NOT NULL) AND (@State <> '')
       
    select @sql = @sql + ' AND Address.StateProvince LIKE @xState + ''%'''

    if (@Postcode IS NOT NULL) AND (@Postcode <> '')
       
    select @sql = @sql + ' AND Address.Postcode LIKE @xPostcode + ''%'''

    select @paramlist = '@xInfo varchar(100), @xUnit varchar(50), @xLot varchar(50), @xNumber varchar(50),
        @xStreet varchar(100), @xSuburb varchar(100), @xState varchar(100), @xPostcode varchar(100)'

    if @Debug = 1 print @sql

    EXEC sp_executesql @sql, @paramlist, @Info, @Unit, @Lot, @Number, @Street, @Suburb, @State, @Postcode
    -------

    Any ideas?  It seems to me that netTiers is assuming that this proc does not return any rows.  Is there something else I need to specify in the proc to let netTiers know what to expect as a result?

    Cheers,
    Dean

    • Post Points: 35
  • 10-18-2006 9:37 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Custom stored procedures with dynamic sql returning void

    When CodeSmith tries to discover the type of resultset returned from stored procs, it does so by issuing the SET FMTONLY ON command to SQL Server.  When this option is activated, the procedures are run with limited rights and therefore your call to an external stored procedure is not executed.  See this thread for more info:

    http://community.codesmithtools.com/forums/permalink/17330/17330/ShowThread.aspx#17330

    You can check for a user_name() is null to determine if this is the case and return a sample record so that the proper metadata is returned to CodeSmith.  For example, you can add this to the begining of your procedure:

    if user_name() is null
    begin
        select AddressId, Info, Unit, Lot, Number, Street, Suburb, StateProvince, Postcode from Address where 1=0
        return
    end

    Hope that helps!


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 10-19-2006 1:03 AM In reply to

    • Dean_ve
    • Not Ranked
    • Joined on 10-19-2006
    • Posts 7
    • Points 125

    Re: Custom stored procedures with dynamic sql returning void

    That fixed it!
    Apreciate your help Bobby!

    Dean

    • Post Points: 35
  • 04-07-2007 9:27 PM In reply to

    Re: Custom stored procedures with dynamic sql returning void

    I have a very similar sp, but wen i try to generate the code with nettier codesmith tell me that the sp is not in the expected format.

    I really don't know what to do...
     

    elfederiko
    • Post Points: 35
  • 08-05-2007 10:41 PM In reply to

    Re: Custom stored procedures with dynamic sql returning void

    Still seeing this issue with Net Tiers 2.2 ... is this a 'live one'?

    Returning a dummy parameter list doesn't fix it for me...

    • Post Points: 35
  • 08-07-2007 8:43 AM In reply to

    • Grier
    • Not Ranked
    • Joined on 02-08-2007
    • Posts 6
    • Points 156

    Re: Custom stored procedures with dynamic sql returning void

    I used the dummy code and I am now returning an IDataReader.  I also want this method to be able to return a @TotalRowCount variable as well.  The value is being set properly in the SPROC and the SQL Profiler looks like this:

    declare @p6 int
    set @p6=33
    exec dbo._Listing_GetSearchResults @VisitorID='D35E2D4B-CA34-4DBB-A0C0-47DA1C8DAF9F',@WhereClause='Listing.BoardID = 1 AND AreaID IN(480)',@OrderBy='listprice DESC',@PageIndex=0,@PageSize=20,@TotalRowCount=@p6 output
    select @p6

    Again, it IS populating the DataReader, but is not returning the @TotalRowCount... which, in this case is 33.

    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems