How do you write a query for comparing SQL Server CE data?
-
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)";
-
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)";
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
-
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
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#?
-
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)";
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; -
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;Ok, How would you use it with
mycommand.CommandText = "write your sql statements";
-
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#?
-
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)";
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.
-
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.
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 -
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.
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