What are the challenges you face when working across database platforms? Take the survey
Options

How do I use If Then Else logic using values from the current column and another column?

I tried using a Python script, but it doesn't recognize the current column:

def main(config):     
     if OtherColumn == "":
         return CurrentColumn
     else:
         return ""

How do I reference the current column in a Python script? If this isn't possible, can this feature be added or is there another generator I can use? If not, it seems I could create a view that duplicates the CurrentColumn (e.g., CurrentColumnCopy) so I can reference the duplicated version of the CurrentColumn, but that seems like an odd way to handle this scenario.
Tagged:

Answers

  • Options
    Hi @KD34XBR960

    Are you able to provide some more specifics around your use case so I can try and create an example.

    I don't think it would be possible to reference the current column as it would not exist when the data generator runs. It runs via INSERT statements and not UPDATE so the existing data is already gone by the time the python script is executed. 
    It may be possible to do via a view, or export from the database and then read in a file and manipulate that.

    One thing I see in your code - you will potentially want to add a loop at the top of your code - the config argument loads in the entire dataset and you will ideally want to loop through each record/iteration within the entity to make your logic checks.

    def main(config):
        for record in config:
            if NameStyle == True:
                return("A")
            else:
               return("B")

    Jon Kirkwood | Technical Support Engineer | Redgate Software
  • Options
    KD34XBR960KD34XBR960 Posts: 2 New member
    My use case is to get a subset of production data, sanitize it, and move it to a dev server using SQL Data Generator. I solved the issue by using the logic in a view. I find it odd not being able to apply the same logic in SQL Data Generator (i.e., insert source value if other column = "" else insert ""). Is this a feature that could be added? For example, add column_value to the config dictionary for a Python script or add if/then/else support to the Simple expression generator (e.g., OtherColumn == "" ? CurrentColumn : "").
  • Options

    Hi @KD34XBR960

     Glad to hear you got a workaround using views.

     

    I would recommend posting your request on our UserVoice site. It allows users of our tools to post their feature requests direct to the developers and help shape the roadmap of our future releases.

    Having a quick scan through previous requests I wasn't able to see a request matching yours but would invite you to create a request here:

    https://redgate.uservoice.com/forums/936348-sql-toolbelt-essentials

     

    Jon Kirkwood | Technical Support Engineer | Redgate Software
Sign In or Register to comment.