I am writing a C# application that interfaces with SQL Server CE, but I have encountered one of the many limitations that CE has.
My database has the tables Customer
, List
, and CustomerList
. Customers can be subscribed to many lists and lists can have many customers - so I have the customerlist table as a junction table (columns are CustomerListId
, CustomerId
, ListId
- that's all).
The design is that we have a master list that will always have every customer listed in it.
So in my code I am writing a function that sets all the listed customers to be subscribed to the master list. This is my very simple SQL query:
insert into customerlist (CustomerId, ListId)
values (
(select customerid from customer),
(select 1 from list where ShortDesc='Master'))
This query does not work in SQL Server CE and I am racking my brains trying to find an alternative that does not involve a painful for-each row-by-row code. Any ideas would be greatly appreciated.
Why not just use:
INSERT INTO customerlist (CustomerId, ListId)
SELECT customerid,1 FROM customer
Am I missing something?
Good luck.
I have come up with a solution.
I have first done a C# cmd that gets the ID of the master list.
string sqlGetId = "select listid from list where ShortDesc='Master'";
SqlCeCommand cmdGetMasterId = new SqlCeCommand(sqlGetId, DbConnection.ceConnection);
int key = (int)cmdGetMasterId.ExecuteScalar();
Then I used that key in a simpler second query:
sql2 = "insert into customerlist (CustomerId, ListId) select customerid, " + key + " from customer";
cmd2 = new SqlCeCommand(sql2, DbConnection.ceConnection);
cmd2.ExecuteNonQuery();
I realize that adding SQL commands with parameters is always better than concatenating - but due to the scope of this application, SQL injection is not a concern.
(Sorry for the simple question/answer, but I am new to this ^^)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.