我有以下代碼作爲SQL中的存儲過程,但它不接受@DBNAME,聲明它需要聲明,但我已經聲明瞭它。參數化一個簡單的SQL更新查詢
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE UpdateSQL
-- Add the parameters for the stored procedure here
@UpdateField varchar(25),
@UpdateValue varchar(25),
@FilterField varchar(25),
@FilterValue varchar(25),
@DBNAME sysname
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE @DBNAME SET @UpdateField = @UpdateValue WHERE @FilterField = @FilterValue
END
GO
下面是一個C#函數調用SQL過程:
internal static bool UpdateSql(SqlArgs pSqlArgs)
{
var pwd = GetPwd();
var sqlCred = new SqlCredential(Sqluser, pwd);
var tCatalog = GetDbo(pSqlArgs.PCatalog);
var sqlConnection = new SqlConnection
{
ConnectionString = $"Data Source={SqlServer};Initial Catalog={tCatalog};",
Credential = sqlCred
};
var sqlCommand = new SqlCommand
{
Connection = sqlConnection,
CommandText = "UpdateSQL",
Parameters = { new SqlParameter("@DB", pSqlArgs.PDbo), new SqlParameter("@UpdateField", pSqlArgs.PUpdateField),
new SqlParameter("@UpdateValue", pSqlArgs.PUpdateValue), new SqlParameter("@FilterField", pSqlArgs.PFilterField),
new SqlParameter("@FilterValue", pSqlArgs.PFilterValue) },
CommandType = CommandType.StoredProcedure,
};
try
{
sqlConnection.Open();
return sqlCommand.ExecuteNonQuery().Equals(1);
}
catch (SqlException ex)
{
MessageBox.Show([email protected]"Error: {ex.Message}", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
finally
{
sqlConnection.Close();
}
}
我試圖運行的最終功能是:用@Database UPDATE @Table SET @UpdateField = @UpdateValue WHERE @ FilterField = @FilterValue
請附上您的程序運行調用爲好。 – Ethilium
您將其聲明爲類型sysname,但更新語句需要一個表。這就是爲什麼你會得到這個錯誤。該錯誤實際上說「必須聲明表變量@dbname – geekzster