sql脚本分析:错误必须声明标量变量“@ SA_CONSOLE_HOSTNAME” [英] sql scripting analysis: error must declare scalar variable "@SA_CONSOLE_HOSTNAME"

查看:74
本文介绍了sql脚本分析:错误必须声明标量变量“@ SA_CONSOLE_HOSTNAME”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所做的



This is what I have done

String scriptAndStub;

scriptAndStub =
	"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" +
	"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
scriptAndStub += script;

executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", Environment.MachineName);







我试过断点并检查错误发生的位置






I tried having breakpoints and check where the error was caught

private void btnSyntaxCheck_Click(object sender, EventArgs e)
        {
            try
            {
                tsStatus.Text = "Checking Syntax...";
                LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text);
                using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection))
                {
                   foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?$", RegexOptions.Multiline | RegexOptions.IgnoreCase))
                      {
                           if (!String.IsNullOrEmpty(script))
                           {
                             myCommand.CommandText = "SET PARSEONLY ON\n" + script;
                             myCommand.ExecuteNonQuery();
                            }
                       }
                 }
                 tsStatus.Text = "Checking Syntax Complete. No errors reported.";
             }
             catch (Exception exc)\\error has been caught here....
             {
                tsStatus.Text = exc.Message;
                MessageBox.Show(exc.Message, "SQL Syntax Check");
             }
         }
   }
}

推荐答案

, RegexOptions.Multiline | RegexOptions.IgnoreCase))
{
if (!String.IsNullOrEmpty(script))
{
myCommand.CommandText = SET PARSEONLY ON \ n + script;
myCommand。 ExecuteNonQuery();
}
}
}
tsStatus.Text = 检查语法是否完整。没有报告错误。;
}
catch (Exception exc)\\ error已被捕到此处... 。
{
tsStatus.Text = exc.Message;
MessageBox.Show(exc.Message, SQL语法检查);
}
}
}
}
", RegexOptions.Multiline | RegexOptions.IgnoreCase)) { if (!String.IsNullOrEmpty(script)) { myCommand.CommandText = "SET PARSEONLY ON\n" + script; myCommand.ExecuteNonQuery(); } } } tsStatus.Text = "Checking Syntax Complete. No errors reported."; } catch (Exception exc)\\error has been caught here.... { tsStatus.Text = exc.Message; MessageBox.Show(exc.Message, "SQL Syntax Check"); } } } }


使这些变化工作精细



String scriptAndStub;



scriptAndStub =



DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\ n; +



SET @SA_CONSOLE_HOSTNAME = @ HOSTNAME @ \\ \\ n;

executeScript.Parameters.Clear();

scriptAndStub + = script;



executeScript .CommandText = scriptAndStub;

executeScript.Parameters.AddWithValue(@ HOSTNAME @,Environment.MachineName);





MADE THESE CHANGES WORKING FINE

String scriptAndStub;

scriptAndStub =

"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n"; +

"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
executeScript.Parameters.Clear();
scriptAndStub += script;

executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", Environment.MachineName);


private void btnSyntaxCheck_Click(object sender, EventArgs e)
        {

            try
            {
                tsStatus.Text = "Checking Syntax...";

                LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text);

                String scriptAndStub;

                scriptAndStub = "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n";

                using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection))
                {
                    foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?


,RegexOptions.Multiline | RegexOptions.IgnoreCase))
{
if (!String.IsNullOrEmpty(script))
{
myCommand.CommandText = SET PARSEONLY ON \ n + scriptAndStub + script;
myCommand.ExecuteNonQuery();
// 使用(SqlDataAdapter adapter = new SqlDataAdapter())
// {
// < span class =code-comment> adapter.SelectCommand = myCommand;
// using using (DataTable table = new DataTable())
// {
// table.Locale = System.Globalization.CultureInfo.InvariantCulture;
// adapter.FillSchema(table,SchemaType.Source);
// }
// }
}
}
}

tsStatus.Text = 检查语法是否完整。没有报告错误。;
}
catch (Exception exc)
{
tsStatus.Text = exc.Message;
MessageBox.Show(exc.Message, SQL语法检查 );
}
}



}
}
", RegexOptions.Multiline | RegexOptions.IgnoreCase)) { if (!String.IsNullOrEmpty(script)) { myCommand.CommandText = "SET PARSEONLY ON\n" +scriptAndStub + script; myCommand.ExecuteNonQuery(); //using (SqlDataAdapter adapter = new SqlDataAdapter()) //{ // adapter.SelectCommand = myCommand; // using (DataTable table = new DataTable()) // { // table.Locale = System.Globalization.CultureInfo.InvariantCulture; // adapter.FillSchema(table, SchemaType.Source); // } //} } } } tsStatus.Text = "Checking Syntax Complete. No errors reported."; } catch (Exception exc) { tsStatus.Text = exc.Message; MessageBox.Show(exc.Message, "SQL Syntax Check"); } } } }


这篇关于sql脚本分析:错误必须声明标量变量“@ SA_CONSOLE_HOSTNAME”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆