在一些朋友的帮助下,我能够解决这个问题!我将 tSQLt.FakeTable 命令移至 Setup.sql,因为 tSQLt 在该模式中的其他测试之前运行它。我们通过在同一个 sql 查询中运行 FakeTable 命令和 INSERT INTO 命令发现了这个异常。像这样:
EXEC [dbServername].tSQLt.FakeTable @TableName='vw_viewname', @SchemaName='dbo';
INSERT INTO [dbServername].dbo.vw_myView
(Column1, Column2, Column3, Column4, Column5, Column6, Column7)
VALUES ( '100513','C','2018-12-13','2019-03-25','2014-10-27',' ',40.500,'3');
这将导致错误:[TestSchemaName].[Test usp_MyStuff_Get Get my stuff] failed: (Error) 更新或插入视图或函数“dbServername.dbo.vw_myView”失败,因为它包含派生或常量字段。
这有效:
CREATE PROCEDURE [TestSchemaName].[Setup]
AS
BEGIN
IF @@TRANCOUNT > 0
BEGIN
EXEC [dbServername].tSQLt.FakeTable @TableName='vw_viewname', @SchemaName='dbo'
END
ELSE
BEGIN
RAISERROR('Procedure was run without tsqlt.Run. Aborting procedure', 16, 1)
END
END
CREATE PROCEDURE [TestSchemaName].[Test usp_MyStuff_Get Get my stuff]
AS
BEGIN
IF @@TRANCOUNT > 0
BEGIN
--EXEC [dbServername].tSQLt.FakeTable @TableName='vw_viewname', @SchemaName='dbo'
--I moved the line above to the Setup.sql
INSERT INTO [dbServername].dbo.vw_myView
(Column1, Column2, Column3, Column4, Column5, Column6, Column7)
VALUES ( '100513','C','2018-12-13','2019-03-25','2014-10-27',' ',40.500,'3')
END
ELSE
BEGIN
RAISERROR('Procedure was run without tsqlt.Run. Aborting procedure', 16, 1)
END
END
GO
发布项目,然后从 SSMS 运行:
EXEC tsqlt.run '[TestSchemaName].[Test usp_MyStuff_Get Get my stuff]'