SQL Server存储过程作用域\标识()问题

SQL Server存储过程作用域\标识()问题,sql,asp-classic,insert,output,procedure,Sql,Asp Classic,Insert,Output,Procedure,我有这个存储过程,在它里面你可以看到我使用了两个SCOPE_IDENTITY();问题是,对于第二个scope_标识,我将其分配给@status变量,但当我执行存储的输出@status变量时,该变量为null,但奇怪的是,这两个插入工作正常 我想将第二次插入的scope_标识作为存储的scope_的输出返回。你能帮我吗 ALTER PROCEDURE [dbo].[sp_UserInsert] ( @Username nvarchar(50)=null, @Password nvarch

我有这个存储过程,在它里面你可以看到我使用了两个SCOPE_IDENTITY();问题是,对于第二个scope_标识,我将其分配给@status变量,但当我执行存储的输出@status变量时,该变量为null,但奇怪的是,这两个插入工作正常

我想将第二次插入的scope_标识作为存储的scope_的输出返回。你能帮我吗

ALTER PROCEDURE [dbo].[sp_UserInsert]
(
@Username   nvarchar(50)=null,
@Password   nvarchar(50)=null,
@Email      nvarchar(50)=null,
@RoleId     int = 0,
@UserId     int = 0,
@typeOp     int,
@status     int = 0 OUTPUT
)   

AS
BEGIN

SET NOCOUNT ON;

if (@typeOp = 1)
/*Creazione nuovo recordo nella tabella Utenti*/
BEGIN
 if exists(select * from Gruppi where Gruppi.GroupID =@roleid)
     BEGIN
        INSERT INTO dbo.Utenti(Username,Password,Email) VALUES(@Username,@Password,@Email)
        set @UserId = SCOPE_IDENTITY()
        if (@UserId > 0)
            BEGIN
                INSERT INTO dbo.Ruoli(UserID,GroupID) VALUES(@UserId,@RoleId)

                set @status = @@rowcount
            END

     END

END


END

现在我有一个问题,如果我从SQLServerManagementStudio执行存储过程,它可以正常工作,但是如果我从代码执行存储过程,它只能在第一次插入时工作! 代码如下:

 string connectionString = ConfigurationManager.ConnectionStrings["SQLConnStr"].ConnectionString;

void SubmitNewUser_Click(Object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string email = txtEmail.Text;       
    string password = txtPassword.Text;
    int ddlRoleId = Convert.ToInt32(ddlRoles.SelectedValue);

    if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(email) && !string.IsNullOrWhiteSpace(password) && ddlRoleId > 0)
    {
        if (CheckUsernameAvailability(username))
        {

            try
            {
                if (!string.IsNullOrWhiteSpace(connectionString))
                {
                    using (SqlConnection dbconn = new SqlConnection(connectionString))
                    {
                        dbconn.Open();

                        SqlCommand command = new SqlCommand("sp_UserInsert", dbconn);
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@Username", username);
                        command.Parameters.AddWithValue("@Password", password);
                        command.Parameters.AddWithValue("@Email", email);
                        command.Parameters.AddWithValue("@RoleId", 1);
                        command.Parameters.AddWithValue("@typeOp", 1);
                        command.ExecuteNonQuery();

                        dbconn.Close();
                    }
                }
            }

            catch (Exception ex)
            {
                // Add error handling here for debugging.
                // This error message should not be sent back to the caller.
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
            }
        }
    }
}

据我所知,你在第二张桌子上没有身份。我认为,用户ID、组ID是表中唯一的键,所以您可以使用

select * from dbo.Ruoil where User_ID = @User_ID and Group_ID = @RoleId

如果您只想检查是否有一些行受到影响,您可以使用

确定您在Ruoli表中有标识吗?您是对的,我在这个表中没有标识!那么,如何为第二次插入返回受影响的行数呢?我认为您需要使用
SELECT
而不是
SET
SELECT@Status=SCOPE\u IDENTITY()
现在我遇到了一个问题,如果我执行从sql server Management Studio存储的命令,它可以正常工作,但是如果我执行从代码存储的命令,它只能在第一次插入时工作!我知道这是一个很老的问题,但它刚刚被编辑过,所以请引起我的注意。请注意:“命名过程时避免使用sp_uu前缀。SQL Server使用此前缀指定系统过程。如果存在同名系统过程,则使用前缀可能会导致应用程序代码中断。”抱歉,我不明白何时在存储中使用您编写的选择。我只想在执行存储后返回第二次插入的受影响行。我在哪里可以使用@@rowcount?非常感谢您这里没有sql server,请尝试
set@status=@@rowcount
,如果插入行,则应为1,否则为0。现在我遇到的问题是,如果从sql server管理工作室执行存储,则工作正常,但如果执行从代码中存储的内容,则仅在第一次插入时工作!请参阅我的第一篇文章,了解代码无误。问题是我在第二个if-roleid中使用的存储字段中,其大小写低于@roleid