我收到異常「必須聲明標量變量」 @strAccountID」必須聲明標量變量
string @straccountid = string.Empty;
sSQL =
"SELECT GUB.BTN,
GUP.CUST_USERNAME,
GUP.EMAIL
FROM GBS_USER_BTN GUB,
GBS_USER_PROFILE GUP
WHERE GUB.CUST_UID = GUP.CUST_UID
AND GUB.BTN = '@straccountID'
ORDER BY
CREATE_DATE DESC"
@straccountid = strAccountID.Substring(0, 10);
代碼爲針對數據庫運行查詢
try
{
oCn = new SqlConnection(ConfigurationSettings.AppSettings["GBRegistrationConnStr"].ToString());
oCn.Open();
oCmd = new SqlCommand();
oCmd.Parameters.AddWithValue("@strAccountID", strAccountID);
oCmd.CommandText = sSQL;
oCmd.Connection = oCn;
oCmd.CommandType = CommandType.Text;
oDR = oCmd.ExecuteReader(CommandBehavior.CloseConnection);
我已經聲明的變量在我的查詢中是否有任何缺陷?
你聲明變量的代碼在哪裏? – 2013-05-09 22:15:48
無論你聲明它,SQL都沒有看到它 - 你的查詢應該有一個'DECLARE @strAccountID varchar(50)'或類似的東西,在某處......它是一個存儲過程嗎? – 2013-05-09 22:17:56
可能只是一個首選項,但我發現全大寫的SQL很難閱讀。不是'SELECT gub.Btn,gup.cust_UserName,gup.Email FROM gbs_USER_BTN gub,gbs_USER_PROFILE gup WHERE ...'更容易在眼睛上? – 2013-05-09 22:23:33