How to send an IF statement by a Transaction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    How to send an IF statement by a Transaction

    Hi guys,

    how can I send an IF statement by T-SQL?

    Lets suppose that I want to insert something, for example;

    Code:
    int n_count = 5;
    string querytosend = @"if  (" + n_count + " > 0)
         INSERT persons (name, age, address) values ('raul', 23, 'Könnigstraße 23')";
    the if statements change regarding the DB engine, I have installed MySQL Server 2005, when I send transactions the server version is 5.0.67-community-nt. I think that's why I get and 1064 Code Error. If you could advise me somehow I will appreciate so much.

    Raul Bolaños,
    Best regards.
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    #2
    Originally posted by raulbolanos
    Hi guys,

    how can I send an IF statement by T-SQL?

    Lets suppose that I want to insert something, for example;

    Code:
    int n_count = 5;
    string querytosend = @"if  (" + n_count + " > 0)
         INSERT persons (name, age, address) values ('raul', 23, 'Könnigstraße 23')";
    the if statements change regarding the DB engine, I have installed MySQL Server 2005, when I send transactions the server version is 5.0.67-community-nt. I think that's why I get and 1064 Code Error. If you could advise me somehow I will appreciate so much.

    Raul Bolaños,
    Best regards.
    I cant make this work... please help me.

    Code:
    int x = 23;
    string queryString = @"CASE WHEN (" + x + @" > 0)
                                         THEN UPDATE person SET person.age = 23 WHERE age = 22
                                         ELSE 'x is less than 0'
                                       END";

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      You will have to go through the syntax of t-sql statements .
      Link
      MSDN

      Comment

      • raulbolanos
        New Member
        • Apr 2009
        • 31

        #4
        Originally posted by DeepBlue
        You will have to go through the syntax of t-sql statements .
        Link
        MSDN
        The problem is that I have found only examples where whether the IF or CASE..WHEN statements are inside the Query. And I need to apply them outside.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You don't need to send an if to T-SQL at all. You have all value of n in your C# code, test it there using C# and run the insert only if your required conditions are met.
          You use an IF in T-SQL only if the arguments to the IF are themselves coming from the database.

          Comment

          Working...