Results 1 to 4 of 4

Thread: Want to use CASE WHEN in a typed dataset (xsd) Update query

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,460

    Want to use CASE WHEN in a typed dataset (xsd) Update query

    Hi. Pretty sure I can't do this in which case I will write 3 different queries, but I'd like to do it in one.
    This is a C# program using Dataset xsd files to query my db tables.
    I wrote this...
    Code:
    UPDATE Trips 
    SET PrevailingWage =  
    	CASE
    	WHEN @pwValue = 'Yes' THEN 1
    	WHEN @pwValue = 'No' THEN 0
    	WHEN @pwValue = 'Unknown' THEN null
    	END
    WHERE Control = @Control
    and named it qryUpdatePrevailingWage. When I invoke qryUpdatePrevailingWage from the code, it thinks there's only one parameter to pass it.
    qryTrips.qryUpdatePrevailingWage(newPWValue, tripControl);

    No overload for method 'qryUpdatePrevailingWage' takes 2 arguments
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,321

    Re: Want to use CASE WHEN in a typed dataset (xsd) Update query

    Bern there, done that ... you can certainly do that .... although I'd do it like this:
    Code:
    UPDATE Trips 
    SET PrevailingWage =  
    	CASE @pwValue
                WHEN 'Yes' THEN 1
    	    WHEN 'No' THEN 0
                ELSE null
    	END
    WHERE Control = @Control
    EDIT - I'm getting old ... I totally missed what you're doing there... What does the full definition of the sproc look like? There's two variabes inuse there, but are they parameters to it as well?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,079

    Re: Want to use CASE WHEN in a typed dataset (xsd) Update query

    In cases like this i'd go with a CTE, and then LEFT JOIN it

    Untested!
    Code:
    WITH CTE AS (SELECT * FROM (VALUES ('Yes', 1), ('No', 0)) X(v1, v2)) 
    UPDATE T 
    SET T.PrevailingWage=C.v2 
    FROM Trips T 
    LEFT JOIN CTE C 
    ON C.v1=@pwValue 
    WHERE T.Control=@Control
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,460

    Re: Want to use CASE WHEN in a typed dataset (xsd) Update query

    Thanks for the posts. It is not a stored procedure. It is a query. I have datasets/xsd files. So I go into dsTrips.xsd and in my QueriesTableAdapter "box" (for lack of a better word, it's a box that lists all the queries I've written so far) I right click and say Add Query. I write my code. I save it and give it a name. Then in my code...
    Code:
                            using (dsTripsTableAdapters.QueriesTableAdapter qryTrips = new dsTripsTableAdapters.QueriesTableAdapter())
                            {
                                qryTrips.qryUpdatePrevailingWageCTE(...
                            }
    when I type that open paren, intellisense tells me what the query wants for parameters. For the CTE one I just tried from zvoni, it doesn't recognize any parameters. For the TG one I just tried, it only wants an integer parameter called Control. Which was my problem in OP. Maybe I just can't do it in a query and need a sproc. I wanted to just write a simple query and have it in the code.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width