if/then or case return?

  • Hi folks.

    I get the if/then and case keywords for conditionally setting field values, what I don't get is how to get a return variable.

    So I say   if var1=1 then var2=1

    But I have no idea what's been done, how do I return that var2 has been set to 1 or left alone?  And how do I get a return if it's a case clause?  What do I check after I call the statement?

    I can't rely on a secondary select as there are going to be many nodes checking records simultaneously.

    I'm working primarily in VB but will need to port the code to Python also.

    Any help welcome.  Many thanks.

  • It depends entirely on the language you are working with.  As this is SQL Server Central, I am assuming you are working in SQL Server.

    The way I would do this is with a stored procedure and an OUTPUT variable assigned to it.  Such as:

    CREATE PROCEDURE [dbo].[test1]
    @var1 INT,
    @var2INT OUT
    AS
    IF (@var1= 1)
    BEGIN
    SELECT @var2=1
    END
    GO

    Then in the application side of things, you just need to check the value of the second parameter you passed into the stored procedure.

    If you are wanting it as a case statement:

    CREATE PROCEDURE [dbo].[test1]
    @var1 INT,
    @var2INT OUT
    AS
    SELECT @var2 = CASE
    WHEN (@var1=1)
    THEN 1
    ELSE NULL
    END
    GO

    Now, if the code you want is really VB or Python, I'd check in your debugger while you are designing the code to see the value of var2 and once you are done designing, you don't need to worry about that anymore unless you want to see the values for bug fixing in which case writing the values to a log file may not hurt.

     

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • That's great, many thanks.  :o)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply