SQL Server CE:如果存在则更新else插入

时间:2021-08-21 10:18:38

How can I INSERT a row if does not yet exist in a SQL Server CE database table and UPDATE it if it exists?

如果SQL Server CE数据库表中尚不存在,则如何插入行,如果存在则更新它?

I have tried lot of SQL queries and keep getting errors. This is not working.

我已经尝试了很多SQL查询并不断收到错误。这不起作用。

IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
    UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
    INSERT INTO Table1 VALUES (...)

Update:

I have found this which is working for me. Any other good suggestion is welcome.

我发现这对我有用。欢迎任何其他好的建议。

    INSERT INTO Table1 VALUES (...)
    SELECT (........)
    WHERE NOT Exists (SELECT ........)
    -- INSERT with Default value if not exist. Next, UPDATE it
    UPDATE Table1 SET (...) WHERE Column1='SomeValue'

1 个解决方案

#1


2  

I know you've tagged sql and sql-server-ce but in case you're open to using c# code to fix this.. :

我知道你已经标记了sql和sql-server-ce但是如果你愿意使用c#代码来解决这个问题..:

Using c# and result sets this is what I did for my mobile app using SQL CE:

使用c#和结果集这是我使用SQL CE为我的移动应用程序所做的:

            // UpdateRow is a struct/class to hold the data for each row
            // SqlCeConn = connection string for db

            SqlCeCommand cmd = new SqlCeCommand("Table1", SqlCeConn);
            cmd.CommandType = CommandType.TableDirect;
            cmd.IndexName = "Column1";

            using (SqlCeResultSet rsltSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
            {
                if (UpdateRow.Column1> 0) // or != "" if it's string etc 
                {
                    if (rsltSet.Seek(DbSeekOptions.FirstEqual, UpdateRow.Column1))
                        FoundRecord = true;
                }
                rsltSet.Read();

                if (FoundRecord)
                {
                    // Update
                    rsltSet.SetInt32(1, UpdateRow.Column1);
                    rsltSet.SetInt32(2, UpdateRow.Column2);

                    // etc

                    rsltSet.Update();
                }
                else
                {
                    // Insert new record
                    SqlCeUpdatableRecord record = rsltSet.CreateRecord();
                    record.SetInt32(0, UpdateRow.Column1);
                    record.SetInt32(1, UpdateRow.Column2);

                    // etc
                    rsltSet.Insert(record, DbInsertOptions.PositionOnInsertedRow);
                }
            }
            cmd.Dispose();
        }
        catch (Exception e)
        {
            // Deal with exception

        }

#1


2  

I know you've tagged sql and sql-server-ce but in case you're open to using c# code to fix this.. :

我知道你已经标记了sql和sql-server-ce但是如果你愿意使用c#代码来解决这个问题..:

Using c# and result sets this is what I did for my mobile app using SQL CE:

使用c#和结果集这是我使用SQL CE为我的移动应用程序所做的:

            // UpdateRow is a struct/class to hold the data for each row
            // SqlCeConn = connection string for db

            SqlCeCommand cmd = new SqlCeCommand("Table1", SqlCeConn);
            cmd.CommandType = CommandType.TableDirect;
            cmd.IndexName = "Column1";

            using (SqlCeResultSet rsltSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
            {
                if (UpdateRow.Column1> 0) // or != "" if it's string etc 
                {
                    if (rsltSet.Seek(DbSeekOptions.FirstEqual, UpdateRow.Column1))
                        FoundRecord = true;
                }
                rsltSet.Read();

                if (FoundRecord)
                {
                    // Update
                    rsltSet.SetInt32(1, UpdateRow.Column1);
                    rsltSet.SetInt32(2, UpdateRow.Column2);

                    // etc

                    rsltSet.Update();
                }
                else
                {
                    // Insert new record
                    SqlCeUpdatableRecord record = rsltSet.CreateRecord();
                    record.SetInt32(0, UpdateRow.Column1);
                    record.SetInt32(1, UpdateRow.Column2);

                    // etc
                    rsltSet.Insert(record, DbInsertOptions.PositionOnInsertedRow);
                }
            }
            cmd.Dispose();
        }
        catch (Exception e)
        {
            // Deal with exception

        }