简体   繁体   English

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

[英]How do I change inline sql statement into Stored Procedure?

I have few inline sql statements with some arrays and if loops. 我有一些带有一些数组和if循环的内联sql语句。 I want to change them all into one stored procedure in SQL server 2005. As i am learning SQL, i have got no idea about how it should be done with arrays and if loops. 我想将它们全部更改为SQL Server 2005中的一个存储过程。在学习SQL的过程中,我对如何使用数组和if循环一无所知。 Is that possible to send arrays into stored procs or is there any other solution. 是否有可能将数组发送到存储的proc或其他解决方案。

Refer the code which i am going to change into SP. 请参阅我要更改为SP的代码。 Thanks in advance!! 提前致谢!!

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;
    }
}

If you have SQL Server 2008, you can use a table variable as an input variable for a stored proc, that is how you handle your array. 如果您有SQL Server 2008,则可以将表变量用作存储proc的输入变量,这就是处理数组的方式。 Look up how to do this in Books Online. 在联机丛书中查找如何执行此操作。

As far as the looping and IF, I suggest you try to use set-based processing instead as it is significantly faster. 至于循环和IF,我建议您尝试使用基于集合的处理,因为它的速度明显更快。 For ideas on set-based logic that may do what you want to do: 有关可能要执行的基于集合的逻辑的想法:

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

You might especially pay attention to the examples using the CASE Statement 您可能会特别注意使用CASE语句的示例

Think of using a # Temp Table or Table Variable as your 'Array'. 考虑将#临时表或表变量用作“数组”。 Stored procs can do Looping, but you may want to think in sets to avoid RBAR (Row by Agonizing Row) processing. 存储的proc可以执行循环,但是您可能需要考虑避免RBAR(逐行扫描行)处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM