Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do you write a query for comparing SQL Server CE data?

How do you write a query for comparing SQL Server CE data?

Scheduled Pinned Locked Moved C#
databasesql-serversysadminquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    If you wanted to write a query that contains 'IF' clause, how would you do it? Please revise this query:

    mycommand.CommandText = "IF EXISTS(SELECT * FROM [MyData] WHERE NOT Wo = 'ABC' OR Code = 1200)";

    If above statement satisfied:

    INSERT INTO[MyData] VALUES('New001', 768353)";

    L R A 3 Replies Last reply
    0
    • A Alex Dunlop

      If you wanted to write a query that contains 'IF' clause, how would you do it? Please revise this query:

      mycommand.CommandText = "IF EXISTS(SELECT * FROM [MyData] WHERE NOT Wo = 'ABC' OR Code = 1200)";

      If above statement satisfied:

      INSERT INTO[MyData] VALUES('New001', 768353)";

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It's a 2 part act: One query to select; then an Insert based on the result of the first query. You're using C# without stored procs; the "if" check is done on the client.

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      A 1 Reply Last reply
      0
      • L Lost User

        It's a 2 part act: One query to select; then an Insert based on the result of the first query. You're using C# without stored procs; the "if" check is done on the client.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        A Offline
        A Offline
        Alex Dunlop
        wrote on last edited by
        #3

        What do you mean by CLIENT? Do you mean that if statement must be in C# part? I have written a query in SQL Management Tool. I can use the IF statement and achieve what I want. But SQL CE gives errors in multiple sections. How can I write queries which have multiple lines in SQLCE and C#?

        L 1 Reply Last reply
        0
        • A Alex Dunlop

          If you wanted to write a query that contains 'IF' clause, how would you do it? Please revise this query:

          mycommand.CommandText = "IF EXISTS(SELECT * FROM [MyData] WHERE NOT Wo = 'ABC' OR Code = 1200)";

          If above statement satisfied:

          INSERT INTO[MyData] VALUES('New001', 768353)";

          R Offline
          R Offline
          RedDk
          wrote on last edited by
          #4

          I know little about C# and how, once your SQL Query gets constructed as mycommand.CommandText, you'd use it from your C# frontend, but I'lll wager you're going to need some more TSQL describing the redirection that "IF EXISTS" intimates in order to complete a statement ... I assume there's a target of your INSERT, something like a TABLE (called [MyData]).

          IF EXISTS
          (
          SELECT *
          FROM [MyData]
          WHERE NOT EXISTS [Wo] = 'ABC'
          OR [Code] = 1200
          )
          INSERT INTO [MyData]
          VALUES('New001', 768353)
          END;

          A 1 Reply Last reply
          0
          • R RedDk

            I know little about C# and how, once your SQL Query gets constructed as mycommand.CommandText, you'd use it from your C# frontend, but I'lll wager you're going to need some more TSQL describing the redirection that "IF EXISTS" intimates in order to complete a statement ... I assume there's a target of your INSERT, something like a TABLE (called [MyData]).

            IF EXISTS
            (
            SELECT *
            FROM [MyData]
            WHERE NOT EXISTS [Wo] = 'ABC'
            OR [Code] = 1200
            )
            INSERT INTO [MyData]
            VALUES('New001', 768353)
            END;

            A Offline
            A Offline
            Alex Dunlop
            wrote on last edited by
            #5

            Ok, How would you use it with

            mycommand.CommandText = "write your sql statements";

            1 Reply Last reply
            0
            • A Alex Dunlop

              What do you mean by CLIENT? Do you mean that if statement must be in C# part? I have written a query in SQL Management Tool. I can use the IF statement and achieve what I want. But SQL CE gives errors in multiple sections. How can I write queries which have multiple lines in SQLCE and C#?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I though it would be easier for you. You know ... partitioning the problem.

              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

              1 Reply Last reply
              0
              • A Alex Dunlop

                If you wanted to write a query that contains 'IF' clause, how would you do it? Please revise this query:

                mycommand.CommandText = "IF EXISTS(SELECT * FROM [MyData] WHERE NOT Wo = 'ABC' OR Code = 1200)";

                If above statement satisfied:

                INSERT INTO[MyData] VALUES('New001', 768353)";

                A Offline
                A Offline
                Alex Dunlop
                wrote on last edited by
                #7

                Can anyone please check this SQL CE query and revise its syntax for me?

                insert into MyData values('test01', '1122/035', '666','ty01', 'tt01') where not exists (select * from MyData where 'test01', '1122/035', '666','ty01', 'tt01')

                In query above, I add a new row to SQL data but it checks for duplicates before adding.

                D L 2 Replies Last reply
                0
                • A Alex Dunlop

                  Can anyone please check this SQL CE query and revise its syntax for me?

                  insert into MyData values('test01', '1122/035', '666','ty01', 'tt01') where not exists (select * from MyData where 'test01', '1122/035', '666','ty01', 'tt01')

                  In query above, I add a new row to SQL data but it checks for duplicates before adding.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  There is no such thing as a WHERE clause on an INSERT statement. You have to write this as two queries. The first is the SELECT to see if anything is returned. If there isn't, then you can execute the INSERT query.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • A Alex Dunlop

                    Can anyone please check this SQL CE query and revise its syntax for me?

                    insert into MyData values('test01', '1122/035', '666','ty01', 'tt01') where not exists (select * from MyData where 'test01', '1122/035', '666','ty01', 'tt01')

                    In query above, I add a new row to SQL data but it checks for duplicates before adding.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Quote:

                    I found a solution on the internet which fixes it. You can use a command of the form: INSERT INTO TableName (ColumnName) SELECT '" + value + "' WHERE NOT EXISTS ( SELECT ColumnName from TableName WHERE Name = '" + value + "')"; Followed by an ExecuteNonQuery().

                    [SQL Server Equivalent of MySQL INSERT IGNORE](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/14dc135d-8570-484b-9265-8e3c575e8e1b/sql-server-equivalent-of-mysql-insert-ignore?forum=sqlgetstarted)

                    It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups