我定义了以下存储过程:
USE [BcmMetrice]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ActivityAdd_proc] @Name nvarchar(max),@Description nvarchar(max) =null ,@Users nvarchar(max),@Object_id nvarchar (15) =null, @Source nvarchar(10) =null, @TemplateId bigint =null, @UserID bigint =null
AS
DECLARE activityUsers_cursor CURSOR FOR
select s from dbo.SplitString(@Users, ';')
DECLARE
@new_ActivityId bigint,
@new_CommentId bigint,
@activityUser_l bigint
BEGIN TRY
INSERT INTO [BcmMetrice].[dbo].[Activity]
([Name]
,[Description]
,[Type]
,[Created])
VALUES
(@Name
,@Description
,ISNULL(@TemplateId,0)
,GETDATE())
SET @new_ActivityId = (SELECT SCOPE_IDENTITY())
INSERT INTO [BcmMetrice].[dbo].[Comment] ([UserID],[CommentText],[Timestamp])
VALUES (ISNULL(@UserID,151),'Activity has been created',GETDATE())
SET @new_CommentId = (SELECT SCOPE_IDENTITY())
INSERT INTO [BcmMetrice].[dbo].[ActivityComment] ([ActivityID],[CommentID])
VALUES (@new_ActivityId, @new_CommentId)
INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','users='+ISNULL(CAST(@Users as varchar(max)),'empty'),null)
OPEN activityUsers_cursor
FETCH NEXT FROM activityUsers_cursor INTO @activityUser_l
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','Inserting users='+ISNULL(CAST(@activityUser_l as varchar(max)),'empty'),null)
INSERT INTO [BcmMetrice].[dbo].[ActivityUser]
([ActivityId]
,[UserId]
,[Role])
VALUES
(@new_ActivityId
,@activityUser_l
,1)
FETCH NEXT FROM activityUsers_cursor INTO @activityUser_l
END
CLOSE activityUsers_cursor
DEALLOCATE activityUsers_cursor
END TRY
BEGIN CATCH
PRINT 'ERROR'
INSERT INTO [BcmMetrice].[dbo].[Log]([Timestamp],[Type],[Data],[StackTrace]) VALUES (GETDATE(),'SQL.ActivityAdd_proc','ERROR CATCHED!'+ERROR_MESSAGE(),null)
END CATCH
select @new_ActivityId
我想做的事情是从过程中返回一个新添加的活动的 id。这就是为什么在非常和我使用线:
select @new_ActivityId
在 SQL Management Studio 中测试此过程时,一切似乎都运行良好。当我尝试在我的 .NET 项目中使用此过程时,问题就开始了。我更新了我的 edmx 模型表单数据库,但是当我执行此过程时,返回值无效。
过程执行如下所示:
int ret = dc.Db.ActivityAdd_proc(name, description, users, object_id, source, templateId, userId);
有谁知道我可能做错了什么?