473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Cases in Select, Where, and OrderBy Dynamic Clause

dcharnigo
Well I am at it again with my complex statements, I need some guidance on this one, in case you cannot tell I am a C++/C# programmer and not used to SQL, I think it is clear what I am trying to accomplish by what I have but ask if you need me to clarify:

Expand|Select|Wrap|Line Numbers
  1. PROCEDURE [dbo].[spGetCIFSearch] (
  2.     @type int,
  3.     @max int,
  4.     @data varchar(25),
  5.     @flag int
  6. )
  7. AS
  8. SET NOCOUNT ON
  9.  
  10. SELECT * INTO ##Temp FROM zCIFRecord AS C
  11. WHERE InsertTime = (SELECT MAX(InsertTime) FROM zCIFRecord WHERE CIFPan = C.CIFPan)
  12. AND CIFUpdateActivity NOT LIKE 'D'
  13.  
  14. BEGIN
  15. IF @flag = 0
  16.     BEGIN
  17.         SELECT COUNT(*)
  18.     END
  19. ELSE
  20.     BEGIN
  21.         SELECT (@max)
  22.             CIFPan, 
  23.             CIFMemNum, 
  24.             CIFLName, 
  25.             CIFFName, 
  26.             CIFInitial 
  27.     END
  28.  
  29. FROM ##Temp
  30.  
  31. WHERE 
  32.     CASE
  33.         WHEN @type = '1' THEN CIFFName LIKE @data
  34.         WHEN @type = '2' THEN CIFLName LIKE @data
  35.         WHEN @type = '3' THEN CIFFName+' '+CIFLName LIKE @data
  36.         WHEN @type = '4' THEN CIFPan LIKE @data
  37.     END
  38. ORDER BY
  39.     CASE
  40.         WHEN @type = '1' THEN CIFFName DESC
  41.         WHEN @type = '2' OR '3' THEN CIFLName DESC
  42.         WHEN @type = '4' THEN CIFPan ASC
  43.     END
  44. END
  45.  
  46. DROP TABLE ##Temp
  47.  
Clearly I have several syntax errors, is it even possible to do what I want?
Apr 22 '08 #1
2 1554
ck9663
2,878 Expert 2GB
Well I am at it again with my complex statements, I need some guidance on this one, in case you cannot tell I am a C++/C# programmer and not used to SQL, I think it is clear what I am trying to accomplish by what I have but ask if you need me to clarify:

Expand|Select|Wrap|Line Numbers
  1. PROCEDURE [dbo].[spGetCIFSearch] (
  2.     @type int,
  3.     @max int,
  4.     @data varchar(25),
  5.     @flag int
  6. )
  7. AS
  8. SET NOCOUNT ON
  9.  
  10. SELECT * INTO ##Temp FROM zCIFRecord AS C
  11. WHERE InsertTime = (SELECT MAX(InsertTime) FROM zCIFRecord WHERE CIFPan = C.CIFPan)
  12. AND CIFUpdateActivity NOT LIKE 'D'
  13.  
  14. BEGIN
  15. IF @flag = 0
  16.     BEGIN
  17.         SELECT COUNT(*)
  18.     END
  19. ELSE
  20.     BEGIN
  21.         SELECT (@max)
  22.             CIFPan, 
  23.             CIFMemNum, 
  24.             CIFLName, 
  25.             CIFFName, 
  26.             CIFInitial 
  27.     END
  28.  
  29. FROM ##Temp
  30.  
  31. WHERE 
  32.     CASE
  33.         WHEN @type = '1' THEN CIFFName LIKE @data
  34.         WHEN @type = '2' THEN CIFLName LIKE @data
  35.         WHEN @type = '3' THEN CIFFName+' '+CIFLName LIKE @data
  36.         WHEN @type = '4' THEN CIFPan LIKE @data
  37.     END
  38. ORDER BY
  39.     CASE
  40.         WHEN @type = '1' THEN CIFFName DESC
  41.         WHEN @type = '2' OR '3' THEN CIFLName DESC
  42.         WHEN @type = '4' THEN CIFPan ASC
  43.     END
  44. END
  45.  
  46. DROP TABLE ##Temp
  47.  
Clearly I have several syntax errors, is it even possible to do what I want?
Possible? yes. Not that way.

Build your query dynamically like:

Expand|Select|Wrap|Line Numbers
  1. declare @strquery varchar(500)
  2.  
  3. set @strquery = 'select count(*) from .....'
  4.  
  5. exec (@strquuery)
  6.  
  7.  
-- CK
Apr 22 '08 #2
Thanks, that was a real "Duh" after I read your post. I was even going to title it dynamic query building! Thanks again.
Apr 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
9
by: Rodusa | last post by:
I am trying to assign @sql variable to @total, where @sql is a sql statement stored on the database, however what I am getting is its string value and not its calcuation. Could anybody help? ...
5
by: 73blazer | last post by:
Hello, I'm looking for a SQL syntax to put a variable name into the from clause. Specifically I have a colume in a table that is table name, and I want to use that result in the from clause in a...
1
by: Greg Moll | last post by:
I need to know the key strokes to insert fields into a select query. I know how to use the mouse but I want to know the key stroke. Can anyone help?
6
by: alex.kemsley | last post by:
Hi guys, I have the following sql statemant to search a mysql database that gets if values from a form with combo box's in. SELECT * FROM hottubs, manufacturers WHERE manufacturers.manid =...
0
FishVal
by: FishVal | last post by:
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox. The usual way is the following. Private...
9
by: Thom Little | last post by:
Using C# 3.5 I want to select a value from an XML file. The intent of the following is to print "EpsilonGamma". It does not work. Can you point out my probably very obvious error? XmlDocument...
2
by: shapper | last post by:
Hello, I have the following LINQ: List<Tagtags = (from t in database.Tags select t).ToList(); I need to restrict my results and for that I have two parameters: StartWidth and N
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.