我目前有一个在 c# 中运行的更新语句和前端来编辑我的 sql server 数据库中的一些值。但我收到转换失败错误。我似乎无法找到来源。
错误信息
Conversion failed when converting the nvarchar value 'UPDATE T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID = ' to data type int.
14
Sql
ALTER PROCEDURE [dbo].[USP_UPDATE_SYS_MAPPING]
-- Add the parameters for the stored procedure here
@SYSTEM VARCHAR(50),
@UNIT VARCHAR(50),
@MEDCTRLEVEL VARCHAR(50),
@MEDCTR VARCHAR(50),
@FACID VARCHAR(50),
@ENTN VARCHAR(50),
@DEPT_ID INT,
@ROLLUP_TYPE_ID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @SQL VARCHAR(MAX);
DECLARE @MEDCTRID INT;
SELECT @MEDCTRID = MED_CTR_ID FROM T_ROLLUP_MED_CTR WHERE MED_CTR = @MEDCTR
PRINT (@MEDCTRID);
-- Insert statements for procedure here
SET @SQL = N'UPDATE T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID =
'''+@DEPT_ID''' , ROLLUP_TYPE_ID = '''+@ROLLUP_TYPE_ID+'''
, UPDATE_DT = GETDATE()
WHERE SYSTEM = '''+@SYSTEM+'''
AND ENTN = '''+@ENTN+'''
AND MED_CTR_ID = '+CONVERT(VARCHAR,@MEDCTRID)+'
AND MED_CTR_LEVEL = '''+@MEDCTRLEVEL+'''
AND FAC_ID = '''+@FACID+'''
AND UNIT = '''+@UNIT+''''
PRINT (@SQL);
EXEC (@SQL);
HTML
string[] mdctrvalue = medctr.Text.Split('[', ']');
string[] mpvalue = mpSearch.Text.Split('(', ')');
string sys = acctsys.ToString();
string unit = txtunit.ToString();
string mdctrlvl = mdctrvalue[1].ToString();
string mdctr = mdctrvalue[0].ToString();
string facid = fac.ToString();
string entn = txtentn.ToString();
string dept_id = dept.SelectedValue.ToString();
string rollup_type_id = rolluptype.SelectedValue.ToString();
SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Rollup2ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = myconn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "USP_UPDATE_SYS_MAPPING";
cmd.Parameters.Add("@SYSTEM", SqlDbType.VarChar).Value = acctsys.ToString();
cmd.Parameters.Add("@UNIT", SqlDbType.VarChar).Value = unit.ToString();
cmd.Parameters.Add("@MEDCTRLEVEL", SqlDbType.VarChar).Value = mdctrlvl.ToString();
cmd.Parameters.Add("@MEDCTR", SqlDbType.VarChar).Value = mdctr.ToString();
cmd.Parameters.Add("@FACID", SqlDbType.VarChar).Value = facid.ToString();
cmd.Parameters.Add("@ENTN", SqlDbType.VarChar).Value = entn.ToString();
cmd.Parameters.Add("@DEPT_ID", SqlDbType.Int).Value = dept_id.ToString();
cmd.Parameters.Add("@ROLLUP_TYPE_ID", SqlDbType.Int).Value = rollup_type_id.ToString();
myconn.Open();
int retVal = cmd.ExecuteNonQuery();