Unix Technical Forum

TSQL: conditional union statement

This is a discussion on TSQL: conditional union statement within the SQL Server forums, part of the Microsoft SQL Server category; --> Is it possible to have a conditional union statement in a stored proc? Here's an example on the northwind ...


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-28-2008, 06:44 PM
Thomas Baxter
 
Posts: n/a
Default TSQL: conditional union statement

Is it possible to have a conditional union statement in a stored proc?

Here's an example on the northwind database. If says there's a syntax
error near the UNION statement. Looks like it doesn't like having the
BEGIN directly in front of it.

Is the only solution to create a dynamic sql string then call exec on
it?

Any help appreciated.

Tom.

CREATE PROCEDURE usp_test
(
@both int = 1
)
AS

SET NOCOUNT ON

SELECT * FROM territories WHERE regionid = 1

IF @both = 1
BEGIN

UNION

SELECT * FROM territories WHERE regionid = 2

END
GO
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:44 PM
Simon Hayes
 
Posts: n/a
Default Re: TSQL: conditional union statement

Thomas Baxter <qwe@ert.zxc> wrote in message news:<MPG.1a1979235adff74398973a@freenews.iinet.ne t.au>...
> Is it possible to have a conditional union statement in a stored proc?
>
> Here's an example on the northwind database. If says there's a syntax
> error near the UNION statement. Looks like it doesn't like having the
> BEGIN directly in front of it.
>
> Is the only solution to create a dynamic sql string then call exec on
> it?
>
> Any help appreciated.
>
> Tom.
>
> CREATE PROCEDURE usp_test
> (
> @both int = 1
> )
> AS
>
> SET NOCOUNT ON
>
> SELECT * FROM territories WHERE regionid = 1
>
> IF @both = 1
> BEGIN
>
> UNION
>
> SELECT * FROM territories WHERE regionid = 2
>
> END
> GO


This is one possible solution:

CREATE PROCEDURE usp_test
(
@both int = 1
)
AS

SET NOCOUNT ON

if @both = 1
SELECT * FROM territories WHERE regionid = 1
UNION
SELECT * FROM territories WHERE regionid = 2

else
SELECT * FROM territories WHERE regionid = 1

GO

Simon
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:44 PM
David Portas
 
Posts: n/a
Default Re: conditional union statement

SELECT *
FROM territories
WHERE regionid = 1
OR (regionid = 2 AND @both = 1)

--
David Portas
------------
Please reply only to the newsgroup
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:44 PM
Gert-Jan Strik
 
Posts: n/a
Default Re: TSQL: conditional union statement

As you saw from the answers, it is not possible to do a condition UNION

However, here is another work around:

SELECT * FROM territories WHERE regionid = 1
UNION
SELECT * FROM territories WHERE regionid = 2 AND @both = 1

HTH,
Gert-Jan


Thomas Baxter wrote:
>
> Is it possible to have a conditional union statement in a stored proc?
>
> Here's an example on the northwind database. If says there's a syntax
> error near the UNION statement. Looks like it doesn't like having the
> BEGIN directly in front of it.
>
> Is the only solution to create a dynamic sql string then call exec on
> it?
>
> Any help appreciated.
>
> Tom.
>
> CREATE PROCEDURE usp_test
> (
> @both int = 1
> )
> AS
>
> SET NOCOUNT ON
>
> SELECT * FROM territories WHERE regionid = 1
>
> IF @both = 1
> BEGIN
>
> UNION
>
> SELECT * FROM territories WHERE regionid = 2
>
> END
> GO

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 06:44 PM
Thomas Baxter
 
Posts: n/a
Default Re: TSQL: conditional union statement

Thanks Simon,

While that'a good idea, my actual script is HUGE, so maintenance would
become a problem having two copies of the same stuff.

Thanks for the reply though.

Tom

In article <60cd0137.0311100028.2dc4b7f7@posting.google.com >,
sql@hayes.ch says...
> Thomas Baxter <qwe@ert.zxc> wrote in message news:<MPG.1a1979235adff74398973a@freenews.iinet.ne t.au>...
> > Is it possible to have a conditional union statement in a stored proc?
> >
> > Here's an example on the northwind database. If says there's a syntax
> > error near the UNION statement. Looks like it doesn't like having the
> > BEGIN directly in front of it.
> >
> > Is the only solution to create a dynamic sql string then call exec on
> > it?
> >
> > Any help appreciated.
> >
> > Tom.
> >
> > CREATE PROCEDURE usp_test
> > (
> > @both int = 1
> > )
> > AS
> >
> > SET NOCOUNT ON
> >
> > SELECT * FROM territories WHERE regionid = 1
> >
> > IF @both = 1
> > BEGIN
> >
> > UNION
> >
> > SELECT * FROM territories WHERE regionid = 2
> >
> > END
> > GO

>
> This is one possible solution:
>
> CREATE PROCEDURE usp_test
> (
> @both int = 1
> )
> AS
>
> SET NOCOUNT ON
>
> if @both = 1
> SELECT * FROM territories WHERE regionid = 1
> UNION
> SELECT * FROM territories WHERE regionid = 2
>
> else
> SELECT * FROM territories WHERE regionid = 1
>
> GO
>
> Simon
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 06:44 PM
Thomas Baxter
 
Posts: n/a
Default Re: conditional union statement

Hi David,

Thanks, that will do what I'm after with a bit of fiddling.

Tom

In article <GNKdnYq-FZS3MTKi4p2dnA@giganews.com>,
REMOVE_BEFORE_REPLYING_dportas@acm.org says...
> SELECT *
> FROM territories
> WHERE regionid = 1
> OR (regionid = 2 AND @both = 1)
>
>

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 01:35 PM.


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