Results 1 to 10 of 10

Thread: return select from dynamic sql

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    return select from dynamic sql

    Hi All

    I have a concatenated sql string:

    set @sqlString = 'Select * from blah'

    I can then of course execute it with :

    exec (@sqlString)

    I need to return this select somehow. How can I do something like :

    Select * from (exec (@sqlString))


    Thanks In Advance

    Parksie

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

    Re: return select from dynamic sql

    The exec() does return the SQL - look at this image - both recordsets appear at the bottom - one from the "pure select" and one from the "dynamic select"
    Attached Images Attached Images  

    *** 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
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: return select from dynamic sql

    I dont get any result set from within a stored procedure.

    I just get '1 row affected'

    Parksie

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

    Re: return select from dynamic sql

    Do you have a SET NOCOUNT ON at the top of that SPROC?

    *** 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
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: return select from dynamic sql

    Someone here gave me this tip and I'm assuming this is MS SQL. Run the query in Enterprise Mangaer or a native query window and direct the output to text. In my environment its on the Query menu, Results to, Results to text. Sometimes you will see things are returning that do not show in the results grid. Also, although it doesn't look like it here, shutting ANSI warning off will eliminate warning messages you might not care about.


    Definately set NO COUNT ON as szlamany posted.

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

    Re: return select from dynamic sql

    Good point - if you are getting an error (like you might be doing a SUM() and you have NULL's in the field you are summing) that error message might get "in front of the return data" when called from code.

    I always create my SPROCS in a QUERY WINDOW - usually with an EXECUTE of that SPROC at the bottom to test it.

    Then I save my CREATE SPROC as a .SQL text file...

    *** 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

  7. #7

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: return select from dynamic sql

    The problem I am having guys is that when I right click on my sproc and choose execute I get 2 returned sets of data.

    The top result set is what is returned from the select but below that I get

    Return Value
    0

    Been a while since I've used sql server. Am I doing something wrong?

    Parksie

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

    Re: return select from dynamic sql

    Are you using SQL Server Management Studio?

    If so - are you making a NEW QUERY WINDOW?

    If not - do so and put the name of the SPROC in the window - without anything in front of it.

    Put each parameter following that name.

    Click the EXECUTE button.

    What do you see in the RESULTS window?? One or two recordsets?

    What is in the MESSAGES window?

    *** 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

  9. #9

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: return select from dynamic sql

    Sorry for a late reply but I've been ill.

    Anyways I still find that when I run this procedure from code I get no return set at all.

    I must have to select from the executed sql string but really cant figure out how to do it.
    Do you think I have to create a temp table?

    Parksie

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

    Re: return select from dynamic sql

    The problem you are having seems very odd - it should work just as you have it.

    Please do the steps I asked for in my last post - they are required for me to assist you in discovering the reason for your issue.

    *** 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

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