How can I combine multiple queries into 1 query

  • I have a stored procedure and with in the stored procedure I have multiple select statements. When I execute the sp, I get multiple results. I would like to combine these results into one.

    The first query gets the info. The second query returns a value based on an if statement. The third query returns multiple row records into one column. I probably can't post my sql, so that is why i'm giving a description of my queries.

  • Hello bdavis,

    You might be able to do a UNION. You can look up the topic in BOL.

    Do the queries return the same columns?

    Best Regards,

    Chris Büttner

  • probably can't post my sql, so that is why i'm giving a description of my queries.

    Extremely difficult to make any meaningfully suggestions with such meager information. Can you dummy up your T-SQL using false table / column names. Brief table constructs which will illustrate what you want to do without revealing any confidential information?

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • bdavis (9/8/2008)


    I have a stored procedure and with in the stored procedure I have multiple select statements. When I execute the sp, I get multiple results. I would like to combine these results into one.

    The first query gets the info. The second query returns a value based on an if statement. The third query returns multiple row records into one column. I probably can't post my sql, so that is why i'm giving a description of my queries.

    You can try converting the second and third queries to output parameters instead of resultsets. Here is an example:

    CREATE PROCEDURE #Procedure

    @RunDate datetime output

    ,@parm_2 varchar(20) output

    AS

    SET @RunDate = getdate();

    SET @parm_2 = 'This is parm 2';

    GO

    DECLARE @RunDate datetime;

    DECLARE @parm_2 varchar(20);

    EXECUTE #Procedure @RunDate OUTPUT, @parm_2 OUTPUT;

    SELECT @RunDate, @parm_2;

    GO

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • plz provide the dummy script...:cool:

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Hi bdavis,

    Please describe what do u want to do through these multiple select setements ? do u want to use results (from these statement)

    as a single record set?

    please make it clear?

    :Whistling:

  • Jeffrey Williams (9/8/2008)


    bdavis (9/8/2008)


    I have a stored procedure and with in the stored procedure I have multiple select statements. When I execute the sp, I get multiple results. I would like to combine these results into one.

    The first query gets the info. The second query returns a value based on an if statement. The third query returns multiple row records into one column. I probably can't post my sql, so that is why i'm giving a description of my queries.

    You can try converting the second and third queries to output parameters instead of resultsets. Here is an example:

    CREATE PROCEDURE #Procedure

    @RunDate datetime output

    ,@parm_2 varchar(20) output

    AS

    SET @RunDate = getdate();

    SET @parm_2 = 'This is parm 2';

    GO

    DECLARE @RunDate datetime;

    DECLARE @parm_2 varchar(20);

    EXECUTE #Procedure @RunDate OUTPUT, @parm_2 OUTPUT;

    SELECT @RunDate, @parm_2;

    GO

    Jeff, I did what you said and it worked perfectly. thanks a bunch for your help.

  • Glad I could help

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply