Unix Technical Forum

run 3 dynamic selects from stored proc

This is a discussion on run 3 dynamic selects from stored proc within the SQL Server forums, part of the Microsoft SQL Server category; --> I am trying to run 3 dynamic selects from stored proc, really only the table name is dynamic.. Anway ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:24 AM
dave
 
Posts: n/a
Default run 3 dynamic selects from stored proc

I am trying to run 3 dynamic selects from stored proc, really only
the table name is dynamic.. Anway I'm kinda lost on how I can
accomplish this.. this is what I have but it only returns the first
result.. that being basic

CREATE PROCEDURE email_complexity

@TableName VarChar(100)

AS
Declare @SQL VarChar(1000)
Declare @SQL1 VarChar(1000)

Set nocount on

SELECT @SQL = 'SELECT Count(complexity) AS basic FROM '
SELECT @SQL = @SQL + @TableName
SELECT @SQL = @SQL + ' WHERE len(complexity) = 5'

Exec ( @SQL)

SELECT @SQL1 = 'SELECT Count(complexity) AS moderate FROM '
SELECT @SQL1 = @SQL1 + @TableName
SELECT @SQL1 = @SQL1 + ' WHERE len(complexity) = 8'

Exec ( @SQL1)


Return

Is there a better way of doing this??

tia

Dave
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:25 AM
Ross Presser
 
Posts: n/a
Default Re: run 3 dynamic selects from stored proc

On 30 Sep 2004 10:12:28 -0700, dave wrote:

> I am trying to run 3 dynamic selects from stored proc, really only
> the table name is dynamic.. Anway I'm kinda lost on how I can
> accomplish this.. this is what I have but it only returns the first
> result.. that being basic
>
> CREATE PROCEDURE email_complexity
>
> @TableName VarChar(100)
>
> AS
> Declare @SQL VarChar(1000)
> Declare @SQL1 VarChar(1000)
>
> Set nocount on
>
> SELECT @SQL = 'SELECT Count(complexity) AS basic FROM '
> SELECT @SQL = @SQL + @TableName
> SELECT @SQL = @SQL + ' WHERE len(complexity) = 5'
>
> Exec ( @SQL)
>
> SELECT @SQL1 = 'SELECT Count(complexity) AS moderate FROM '
> SELECT @SQL1 = @SQL1 + @TableName
> SELECT @SQL1 = @SQL1 + ' WHERE len(complexity) = 8'
>
> Exec ( @SQL1)
>
>
> Return
>
> Is there a better way of doing this??
>
> tia
>
> Dave


If your client isn't prepared to accept multiple resultsets, then you'll
only see the first one. You could join them together with a union:

CREATE PROCEDURE email_complexity

@TableName VarChar(100)

AS
Declare @SQL VarChar(1000)
Declare @SQL1 VarChar(1000)

Set nocount on

SELECT @SQL = 'SELECT Count(complexity) AS basic FROM '
SELECT @SQL = @SQL + @TableName
SELECT @SQL = @SQL + ' WHERE len(complexity) = 5'

SELECT @SQL = @SQL + ' UNION ALL '

SELECT @SQL1 = 'SELECT Count(complexity) AS moderate FROM '
SELECT @SQL1 = @SQL1 + @TableName
SELECT @SQL1 = @SQL1 + ' WHERE len(complexity) = 8'

Exec ( @SQL1)


Return
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:25 AM
Damien
 
Posts: n/a
Default Re: run 3 dynamic selects from stored proc

Ross Presser <rpresser@imtek.com> wrote in message news:<nt2t9zlcjwzx.dlg@rpresser.invalid>...
> On 30 Sep 2004 10:12:28 -0700, dave wrote:
>
> > I am trying to run 3 dynamic selects from stored proc, really only
> > the table name is dynamic.. Anway I'm kinda lost on how I can
> > accomplish this.. this is what I have but it only returns the first
> > result.. that being basic
> >
> > CREATE PROCEDURE email_complexity
> >
> > @TableName VarChar(100)
> >
> > AS
> > Declare @SQL VarChar(1000)
> > Declare @SQL1 VarChar(1000)
> >
> > Set nocount on
> >
> > SELECT @SQL = 'SELECT Count(complexity) AS basic FROM '
> > SELECT @SQL = @SQL + @TableName
> > SELECT @SQL = @SQL + ' WHERE len(complexity) = 5'
> >
> > Exec ( @SQL)
> >
> > SELECT @SQL1 = 'SELECT Count(complexity) AS moderate FROM '
> > SELECT @SQL1 = @SQL1 + @TableName
> > SELECT @SQL1 = @SQL1 + ' WHERE len(complexity) = 8'
> >
> > Exec ( @SQL1)
> >
> >
> > Return
> >
> > Is there a better way of doing this??
> >
> > tia
> >
> > Dave

>
> If your client isn't prepared to accept multiple resultsets, then you'll
> only see the first one. You could join them together with a union:
>
> CREATE PROCEDURE email_complexity
>
> @TableName VarChar(100)
>
> AS
> Declare @SQL VarChar(1000)
> Declare @SQL1 VarChar(1000)
>
> Set nocount on
>
> SELECT @SQL = 'SELECT Count(complexity) AS basic FROM '
> SELECT @SQL = @SQL + @TableName
> SELECT @SQL = @SQL + ' WHERE len(complexity) = 5'
>
> SELECT @SQL = @SQL + ' UNION ALL '
>
> SELECT @SQL1 = 'SELECT Count(complexity) AS moderate FROM '
> SELECT @SQL1 = @SQL1 + @TableName
> SELECT @SQL1 = @SQL1 + ' WHERE len(complexity) = 8'
>
> Exec ( @SQL1)
>
>
> Return


But if you do that, you should be aware that what you'll receive in
return is two rows, under the single column "basic". There are ways to
improve this (to either add a second column with the texts "basic" and
"moderate", or by converting it into a single row, with columns
"basic" and "moderate").

If the OP is interested in either of these approaches, reply back
here, and I'll post more.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com