473,730 Members | 3,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cases in Select, Where, and OrderBy Dynamic Clause

dcharnigo
20 New Member
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 1565
ck9663
2,878 Recognized Expert Specialist
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
dcharnigo
20 New Member
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
11506
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 { CarsSoldID int (primary key) MonthID int DealershipID int NumberCarsSold int
9
6611
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? DECLARE my_cursor CURSOR FOR SELECT sqlstatement from Sn_SalesReport declare @sql varchar(255), @total varchar(20) OPEN my_cursor FETCH NEXT FROM my_cursor INTO @sql
5
3325
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 select subquery ideally I'd like: ----- select coid,coid_ref from physical.ext ext where coid_ref not in (select coid from ext.coname) -----
1
1248
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
2043
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 = hottubs.manid AND hottubs.type = '%s' AND hottubs.dimlength <= '%s' AND hottubs.dimwidth <= '%s' AND hottubs.dimhight <= '%s' AND hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY $thesearchtype_search.
0
5626
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 Sub Combo1_AfterUpdate() Me.Combo2.RowSource = "SELECT ... FROM ... WHERE Table2.keyID=" & Me.Combo1 & ";" Combo2.Requery
9
3874
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 document = new XmlDocument(); document.Load("sample.xml"); XmlElement root = document.DocumentElement; XmlNode node = root.SelectSingleNode("//menu"); Console.Write( node.SelectSingleNode("//menu/row/vis").Value ); Console.Write(...
2
3705
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
5598
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 cal_date, holiday_ID from Calendar, holiday_tbl WHERE (((calendar.cal_Date) Between . And .)) And Email_sent=0 AND Staff_ID=" & Staff_ID This works fine.
0
8770
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9442
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9229
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4543
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.