简体   繁体   English

从FitNesse中具有单个标识列的表中获取标识值

[英]Getting the identity value from a table with single identity column in FitNesse

Does FitNesse/dbFit support inserting into a table with a single identity column and then returning that value into a variable? FitNesse / dbFit是否支持插入具有单个标识列的表,然后将该值返回到变量中?

For example: 例如:

create table TestTable ( ID int identity(1,1) )

!|Insert|TestTable|
|ID?              |
|>>TestID         |

results in the following exception: 导致以下异常:

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'.

Is there another way to accomplish this? 还有另一种方法可以做到这一点吗?

Your problem is not with Fitnesse, but with the SQL engine. 您的问题不在于Fitnesse,而在于SQL引擎。 If you try the same query in SQL Server Management Studio you will receive the same error. 如果您在SQL Server Management Studio中尝试相同的查询,您将收到相同的错误。 Thus, given the restriction that you cannot use set identity_insert on the question you really need to ask is how to insert a record with no insertable fields in SQL Server independent of Fitnesse ? 因此,鉴于您不能真正在问题set identity_insert on使用set identity_insert on的限制,您真正需要问的是如何在SQL Server中独立于Fitnesse插入没有可插入字段的记录? This StackOverflow post provides a simple answer: 此StackOverflow帖子提供了一个简单的答案:

INSERT INTO #TempTable DEFAULT VALUES

Having that in hand, now the task is to map this to Fitnesse. 掌握了这些之后,现在的任务是将其映射到Fitnesse。 Here is one solution: 这是一种解决方案:

!|execute|CREATE TABLE #TestTable ( ID int identity(1,1) )|

!|execute|INSERT INTO #TestTable DEFAULT VALUES|

!|query|SELECT TOP 1 @@IDENTITY [ID] FROM #TestTable|
|ID?                                                |
|>>TestID                                           |

Fitnesse's INSERT command does not support the default construct, so you must switch to the more general EXECUTE command to do the insert. Fitnesse的INSERT命令不支持默认构造,因此您必须切换到更通用的EXECUTE命令进行插入。 That, however, does not return results so you cannot glean the auto-inserted ID value directly. 但是,该操作不会返回结果,因此您无法直接收集自动插入的ID值。 The final query gives you one way to grab the just-inserted identity value. 最终查询为您提供了一种获取刚插入的身份值的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM