我有这个存储过程:
ALTER PROCEDURE [dbo].[DeleteFromSchoolMain]
@TblName VARCHAR(50),
@MainID VARCHAR(10),
@TblCol VARCHAR(50),
@rowsCount INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RelTbl AS Nvarchar(50)
DECLARE @ColForFK AS Nvarchar(50)
DECLARE @TblMaterial AS NVARCHAR(50)
DECLARE @ColMaterialID AS NVARCHAR(50)
DECLARE @ColMaterialFK AS NVARCHAR(50)
DECLARE @cmdForMaterial AS NVARCHAR(max)
DECLARE @TblSubject AS NVARCHAR(50)
DECLARE @ColSubjectID AS NVARCHAR(50)
DECLARE @ColSubjectFK AS NVARCHAR(50)
DECLARE @cmdForSubject AS NVARCHAR(max)
Set @ColForFK = SUBSTRING(@TblCol,1, DATALENGTH(@TblCol)-2)
Set @ColForFK = @ColForFK+'FK'
DECLARE @cmd AS NVARCHAR(max)
DECLARE @TempCount int
SET @MainID = ''''+@MainID+ ''''
SET @RelTbl = 'ClassSubMatRelation'
--For Class Material
SET @TblMaterial = 'ClassMaterial'
SET @ColMaterialID = 'ClassMaterialID'
SET @ColMaterialFK = 'ClassMaterialFK'
SET @cmd = N'UPDATE ' + @RelTbl + ' SET Status_Info = 0 WHERE ' + @ColForFK + ' = ' + @MainID -- Delete from ClassRelation Table
EXEC(@cmd)
SET @TempCount = @@ROWCOUNT
SET @rowsCount = @TempCount + @rowsCount;
SET @cmd = N'UPDATE ' + @TblName + ' SET Status_Info = 0 WHERE ' + @TblCol + ' = ' + @MainID -- Delete from Main Table
EXEC(@cmd)
SET @TempCount = @@ROWCOUNT
SET @rowsCount = @TempCount + @rowsCount;
-------------------------------------Board-----------------------------------
IF (@TblName = 'Board')
BEGIN
SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set Status_Info = 0 where '+@ColMaterialID+' in ( Select s.'+@ColMaterialFK+' from '+@RelTbl+' s where s.' + @ColForFK + ' = ' + @MainID + ' AND s.ClassSubjectFK IN ( SELECT T.ClassSubjectFK FROM ' + @RelTbl + ' T WHERE T.'+@ColForFK+' = '+ @MainID + ') and s.ClassSubjectFK NOT IN ( SELECT E.ClassSubjectFK FROM ' +@RelTbl+' E WHERE E.'+@ColForFK+' != '+ @MainID +') and s.'+@ColMaterialFK+' is not null )'
EXEC(@cmdForMaterial)
SET @TempCount = @@ROWCOUNT
SET @rowsCount = @TempCount + @rowsCount;
SET @TempCount = 0;
--For Class Subject
SET @TblMaterial = 'ClassSubject'
SET @ColMaterialID = 'ClassSubjectID'
SET @ColMaterialFK = 'ClassSubjectFK'
SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set Status_Info = 0 where '+@ColMaterialID+' in ( Select s.'+@ColMaterialFK+' from '+@RelTbl+' s where s.' + @ColForFK + ' = ' + @MainID + ' AND s.ClassSubjectFK IN ( SELECT T.ClassSubjectFK FROM ' + @RelTbl + ' T WHERE T.'+@ColForFK+' = '+ @MainID + ') and s.ClassSubjectFK NOT IN ( SELECT E.ClassSubjectFK FROM ' +@RelTbl+' E WHERE E.'+@ColForFK+' != '+ @MainID +') and s.'+@ColMaterialFK+' is not null )'
EXEC(@cmdForMaterial)
SET @TempCount = @@ROWCOUNT
SET @rowsCount = @TempCount + @rowsCount;
SET @TempCount = 0;
END
------------------------------ Class Subject ------------------------------------
ELSE IF (@TblName = 'ClassSubject')
BEGIN
SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set '+@TblMaterial+'.Status_Info = 0 from '+@TblMaterial+' tm join '+@RelTbl+' rt on rt.'+@ColMaterialFK+' = tm.'+@ColMaterialID+' where rt.'+@ColForFK+ ' = '+ @MainID
EXEC(@cmdForMaterial)
SET @TempCount = @@ROWCOUNT
SET @rowsCount = @TempCount + @rowsCount;
SET @TempCount = 0;
END
END
GO
我正在使用C#执行它,代码如下:
var rowsAffected = 0;
using (SqlConnection conn = new SqlConnection(Constants.Connection))
{
conn.Open();
SqlCommand cmd = new SqlCommand("DeleteFromSchoolMain", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TblName", abundleBoard.TempName);
cmd.Parameters.AddWithValue("@MainID", abundleBoard.MainID.ToString());
cmd.Parameters.AddWithValue("@TblCol", abundleBoard.TblCol);
SqlParameter outputParam = new SqlParameter();
outputParam.ParameterName = "@rowsCount";
outputParam.SqlDbType = System.Data.SqlDbType.Int;
outputParam.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(outputParam);
object o = cmd.ExecuteScalar();
if (!outputParam.Value.Equals(DBNull.Value))
{
rowsAffected = Convert.ToInt32(outputParam.Value);
string q = o.ToString();
}
conn.Close();
}
但我的问题是,我没有得到输出参数的任何东西。我不明白问题所在,而且我感觉我的存储过程并没有优化。有什么我可以做的来加快查询执行速度吗?我已经尽力进行了索引和分区。
编辑:
在@SurjitSD和@PeterB的帮助下,我得到了解决方案:
在我的存储过程顶部添加了以下代码:
SET @rowsCount=0
在C#中,我已经改变了代码,就像这样:
cmd.ExecuteNonQuery();
if (!cmd.Parameters["@rowsCount"].Value.Equals(DBNull.Value))
{
rowsAffected = Convert.ToInt32(cmd.Parameters["@rowsCount"].Value);
}
目前这个可以正常工作!
SET @rowsCount=0
启动过程。 - Peter B.AddWithValue()
- 它可能导致意外和令人惊讶的结果... - marc_so
声明为一个对象,而输出参数是 int 类型的?如果我们使用var
声明它,然后将输出转换为 int 类型,会更好吗? - iSR5