如何将内联sql语句更改为存储过程?

时间:2010-09-08 19:21:48

标签: c# asp.net sql

我有一些带有一些数组和if循环的内联sql语句。我想将它们全部更改为SQL Server 2005中的一个存储过程。 在我学习SQL时,我不知道应该如何使用数组和if循环。可以将数组发送到存储过程中,还是有其他解决方案。

请参阅我要更改为SP的代码。在此先感谢!!

public bool TempUpdateMerchantCategories(long LocationID, ArrayList CategoryList, ArrayList ImageData)
{
    try
    {
        int j = 0;
        bool hasImage = false;
        string previousPID = string.Empty;
        bool isFirstPID = true;
        int numberOfSubCategories = 3;
        int pIDCount = 0;
        foreach (string data in CategoryList)
        {
            string pID = data.Split(',')[0];
            if (isFirstPID)
            {
                isFirstPID = false;
                previousPID = pID;
                pIDCount++;
            }
            else
            {
                if (pID != previousPID)
                {
                    previousPID = pID;
                    pIDCount++;
                }
            }
        }
        ArrayList stepsThresholdList = new ArrayList();
        if (pIDCount > 1)
        for (int k = 1; k < pIDCount; k++)
        {
            stepsThresholdList.Add(k * numberOfSubCategories);
        }


        if (CategoryList == null || CategoryList.Count == 0) return true;
        SqlParameter[] parameters = new SqlParameter[]
        {
            new SqlParameter("@LocationID", LocationID)
        };

        //SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);

        string commandText = String.Format("DELETE FROM [TempMerchant_Location_Category] WHERE locationid = @LocationID");
        Debug.Write(commandText);
        SqlHelper.ExecuteNonQuery(DbConnString, CommandType.Text, commandText, parameters);

        for (int i = 0; i < CategoryList.Count; i++)
        {
            if (ImageData.Count > 0 && j < ImageData.Count)
            {
                string imageID = ImageData[j].ToString().Split(',')[0];
                string primaryID = ImageData[j].ToString().Split(',')[1];

                if (primaryID == CategoryList[i].ToString().Split(',')[0])
                if (imageID != "IsDefault")
                hasImage = true;
            }

            if (!hasImage)
            {
                parameters = new SqlParameter[]
                {
                    new SqlParameter("@LocationID", LocationID),
                    new SqlParameter("@PrimaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
                    new SqlParameter("@SecondaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[1]))
                };

                //SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);
                commandText = String.Format("INSERT INTO [dbo].[TempMerchant_Location_Category] ([LocationID],[PrimaryID],[SecondaryID])  VALUES (@LocationID,@PrimaryID,@SecondaryID)");
            }
            else
            {
                parameters = new SqlParameter[]
                {
                    new SqlParameter("@LocationID", LocationID),
                    new SqlParameter("@PrimaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
                    new SqlParameter("@SecondaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[1])),
                    new SqlParameter("@ImageID", Convert.ToInt64(ImageData[j].ToString().Split(',')[0]))
                };
                // SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);
                commandText = String.Format("INSERT INTO [dbo].[TempMerchant_Location_Category] ([LocationID],[PrimaryID],[SecondaryID],[ImageID])  VALUES (@LocationID,@PrimaryID,@SecondaryID,@ImageID)");
            }
            if (stepsThresholdList.Count > 0 && j < stepsThresholdList.Count)
            if (i == (Convert.ToInt32(stepsThresholdList[j]) - 1))
            j++;
            Debug.Write(commandText);
            SqlHelper.ExecuteNonQuery(DbConnString, CommandType.Text, commandText, parameters);
        }
        return true;
    }


    catch (Exception ex)
    {
        LogError("Error Occurred When Updating TempMerchant Ctegories: LocationID:" + LocationID.ToString(), ex);
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

如果您有SQL Server 2008,则可以使用表变量作为存储过程的输入变量,即处理数组的方式。在联机丛书中查找如何执行此操作。

就循环和IF而言,我建议您尝试使用基于集合的处理,因为它明显更快。有关基于集合的逻辑的想法,可以做你想做的事情:

http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them

您可能会特别注意使用CASE声明的示例

答案 1 :(得分:0)

考虑使用#Temp表或表变量作为'数组'。存储过程可以执行循环,但您可能需要在集合中进行思考以避免RBAR(按行Agonizing Row)处理。