Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 24th, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Basic Control Structure

I have situation where I need to implement logic like this:

IF condition1
  SQL block1
ELSE IF condition2
  SQL block2
ELSE
  SQL block default
END IF

However, I don't believe I can use that so simply in TSQL. There has to be a way to implement this. Any Ideas?

Thanks in advance for your comments.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old May 24th, 2004, 05:14 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

I do not clearly understand the SQL Block?

But for if statement and conditions you may want to use Case

Here is a sample from BOOKS ONLINE

Using CASE
The CASE function is a special Transact-SQL expression that allows an alternative value to be displayed depending on the value of a column. This change in data is temporary; therefore, there are no permanent changes to the data. For example, the CASE function can display California in a query result set for rows that have the value CA in the state column.

The CASE function consists of:

The CASE keyword.


The column name to transform.


WHEN clauses specifying the expressions to search for and THEN clauses specifying the expressions to replace them with.


The END keyword.


An optional AS clause defining an alias for the CASE function.
This example displays, in the query result set, the full name of the state each author lives in:

SELECT au_fname, au_lname,
   CASE state
      WHEN 'CA' THEN 'California'
      WHEN 'KS' THEN 'Kansas'
      WHEN 'TN' THEN 'Tennessee'
      WHEN 'OR' THEN 'Oregon'
      WHEN 'MI' THEN 'Michigan'
      WHEN 'IN' THEN 'Indiana'
      WHEN 'MD' THEN 'Maryland'
      WHEN 'UT' THEN 'Utah'
        END AS StateName
FROM pubs.dbo.authors
ORDER BY au_lname



 
Old May 25th, 2004, 07:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jemacc-

First off, thank you for your comments.

I did not describe the SQL blocks well. What I meant was for them to be separate SELECT statements. The structure would only execute one SELECT based on the conditions.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old May 25th, 2004, 09:40 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes you can do that in SQL, something like this:

IF condition1
  SQL block1
ELSE IF condition2
  SQL block2
ELSE
  SQL block default

if any of the three SQL blocks have more than one statement, then you need to use "begin" "end" for that block, otherwise is optional. this is the case with "begin" and "end":

IF condition1
begin
  SQL block1
end
ELSE IF condition2
begin
  SQL block2
end
ELSE
begin
  SQL block default
end
 
Old May 25th, 2004, 10:17 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys!

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.





Similar Threads
Thread Thread Starter Forum Replies Last Post
feeding tree control according to xml structure kirat Flex 1 April 24th, 2009 12:49 PM
Data Grid View Control Visual Basic 2005 dattatraydongare BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 3 November 6th, 2008 06:35 AM
Using Visual Basic behind Excel Control Buttons Danielle Beginning VB 6 0 January 10th, 2006 06:32 AM
Visuabl Basic and OLE Control kashyap_ankur Pro VB 6 0 April 26th, 2004 02:06 AM
Need help with inventory control structure watrout Access 1 March 17th, 2004 10:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.