我目前有一个在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();
答案 0 :(得分:4)
也许改变:
+@ROLLUP_TYPE_ID+
要:
+ CONVERT(VARCHAR(12), @ROLLUP_TYPE_ID) +
你应该always specify a length for your varchar columns/variables ......
也就是说,您可以重新编写存储过程以完全不使用动态SQL - 为什么要使用它?
ALTER PROCEDURE [dbo].[USP_UPDATE_SYS_MAPPING]
@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;
DECLARE @MEDCTRID INT;
SELECT @MEDCTRID = MED_CTR_ID FROM dbo.T_ROLLUP_MED_CTR
WHERE MED_CTR = @MEDCTR;
PRINT (@MEDCTRID);
UPDATE dbo.T_ROLLUP_SYSTEM_EXCEPT
SET DEPT_ID = CASE
WHEN @DEPT_ID > 1 THEN @DEPT_ID
WHEN @DEPT_ID = 1 THEN NULL
ELSE REG_DEPT_ID1 END
, ROLLUP_TYPE_ID = @ROLLUP_TYPE_ID
, UPDATE_DT = GETDATE()
WHERE SYSTEM = @SYSTEM
AND ENTN = @ENTN
AND MED_CTR_ID = @MEDCTRID
AND MED_CTR_LEVEL = @MEDCTRLEVEL
AND FAC_ID = @FACID
AND UNIT = @UNIT;
END
GO
如果由于某种原因需要使用动态SQL而尚未共享,那么使用sp_executesql
而不是EXEC()
进行参数设置仍然会更好。