我有一些查询正在检查表中的记录,如果没有这样的记录它被添加。有参与这一进程2个查询:SQL CE参数传递
1)select id from table where <conditions>
如果没有对应于我什么都不做(或更新记录与IDS,因为我想要的)任何标识,如果没有IDS我执行第二次查询:
2)insert into table(<columns>) values(<values>)
当然<conditions>
中,<columns>
和<values>
是其上下文正确的字符串。 我要的是这2个查询的连接到一个:
insert into table(<values>) select <parameter list> where not exists(select id from table where <conditions>)
所以只会有参与,而不是2 此查询的例子有一个查询为:
insert into persons(name) select @Name where not exists (select id from persons where name = @Name)
问题是我使用查询的参数,当我执行这个组合查询时,我得到一个异常说:“这个位置不允许有参数,确保'@'符号位于有效的位置,或者参数在所有这一切SQL语句“。 有没有办法绕过参数,并没有得到这个查询的异常? 我正在使用C#和SqlCe相关的类。
有几十个或几百个这样的查询,通过合成他们我可以至少缩短代码。我肯定会尝试一下TableDirect,从来没有使用它 – Blablablaster
虽然我是一个减少LOC的巨大粉丝,但更短但较少可读的代码并不一定是一个胜利。并且要小心TableDirect。一旦你使用它,你可能永远不会再想写另一个SQL查询。 – ctacke