The below code is executed in C# Winforms. What I'm trying to do is creating tables and stored procedures when the program starts. I'm using `IF NOT EXISTS` in my SQL commands. If not exists, create, else do nothing.

Creating the tables is ok, it works beautifully.

But when creating stored procedures, Visual Studio throws this error;

> 
> Must declare the scalar variable @xxxx
> 
> 
> 

Before getting this error I was using `USE` and `GO` commands and this approach also gave `CREATE/ALTER PROCEDURE` errors. As I researched on web, I gradually learnt that `USE` and `GO` is a no-no in C#. That's why I use `Else` instead of `GO` after `IF NOT EXISTS`.

The code works in T-SQL, but I guess I need to convert it to C# somehow.

Any help is appreciated. Thanks.

**Update:**

After I changed the StringBuilder to the just string, it fixed the previous error. Now it gives the below one. Couldn't find anything amiss though.

```
" System.Data.SqlClient.SqlException
    HResult=0x80131904
    Message=Incorrect Syntax near the 'Trendyol'.
  Unclosed quotation mark after the character string "', ')))))AS varchar) FROM urnBilgi INNER JOIN urn ON urn.stkID = bVeriID WHERE bVeriID = @stkID and bBilgiID = @bBilgiID AND(urn.fiyatS BETWEEN @fiyat_baslangic and @fiyat_bitis) END END'"

```

```
    public void createSP()
    {
        StringBuilder sbSP = new StringBuilder();

        sbSP.AppendLine(" IF NOT EXISTS(SELECT * FROM sys.objects WHERE type = 'P' AND object_id = object_id('etradeCore'))" +
                        " exec('CREATE PROCEDURE [dbo].[etradeCore]("  +

                        " ELSE"+ //Used Instead of GO

                        " ALTER PROCEDURE [dbo].[etradeCore](" +
                        " @pazaryeri VARCHAR(50)," +
                        " @magaza VARCHAR(50)," +
                        " @stkID int," +
                        " @komisyon decimal(9, 4)," +
                        " @fiyat_baslangic decimal(9, 4)," +
                        " @fiyat_bitis decimal(9, 4)," +
                        " @eklenecekfiyat decimal(9, 4)" +
                        ")" +

                        " AS BEGIN" +

                        " SET NOCOUNT OFF" +
                        " SET ANSI_NULLS ON" +
                        " SET QUOTED_IDENTIFIER ON" +

                        " DECLARE @bBilgiID tinyint" +

                        " IF(@pazaryeri = 'Trendyol' and @magaza = 'Kozmeti')" +
                        " SET @bBilgiID = 58" +
                        " ELSE IF(@pazaryeri = 'Trendyol' and @magaza = 'Golden Rose')" +
                        " SET @bBilgiID = 52" +
                        " ELSE IF(@pazaryeri = 'Trendyol' and @magaza = 'Ziaja')" +
                        " SET @bBilgiID = 60" +
                        " ELSE IF(@pazaryeri = 'Hepsiburada' and @magaza = 'Kozmeti')" +
                        " SET @bBilgiID = 43" +
                        " ELSE IF(@pazaryeri = 'Hepsiburada' and @magaza = 'Golden Rose')" +
                        " SET @bBilgiID = 44" +
                        " ELSE IF(@pazaryeri = 'Hepsiburada' and @magaza = 'Ziaja')" +
                        " SET @bBilgiID = 43" +

                        " IF NOT EXISTS(select * from urnBilgi WHERE bVeriID = @stkID and bBilgiID = @bBilgiID and bDeger >= convert(varchar, 0))" +
                        " BEGIN" +
                        " INSERT INTO urnBilgi(bVeriID, bBilgiID, bDeger) VALUES(@stkID, @bBilgiID, '0')" +
                        " END" +
                        " ELSE" +
                        " BEGIN" +
                        " UPDATE urnBilgi SET bDeger =" +
                        " CAST(CONVERT(decimal(9, 4), ((((Select fiyatS from urn where stkID = @stkID) + @eklenecekfiyat)) *" +
                        " CONVERT(decimal(9, 4), convert(varchar, '1.' + REPLACE(@komisyon, '.', ''))) ))AS varchar)" +
                        " FROM urnBilgi" +
                        " INNER JOIN" +
                        " urn ON urn.stkID = bVeriID" +
                        " WHERE bVeriID = @stkID and bBilgiID = @bBilgiID AND(urn.fiyatS BETWEEN @fiyat_baslangic and @fiyat_bitis)" +
                        " END" +
                        " END" 
                        );
        using (SqlConnection connection = new SqlConnection(constring))
        {

            using (SqlCommand cmd = new SqlCommand(sbSP.ToString(), connection))
            {
                connection.Open();
                cmd.CommandType = CommandType.Text;

                    cmd.ExecuteNonQuery();

                connection.Close();
            }
        }
    }

```