0

我目前有一个在 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();
4

1 回答 1

4

也许改变:

+@ROLLUP_TYPE_ID+

到:

+ CONVERT(VARCHAR(12), @ROLLUP_TYPE_ID) +

您应该始终为您的 varchar 列/变量指定一个长度...

也就是说,您可以重新编写存储过程以完全不使用动态 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().

于 2012-08-13T18:22:37.553 回答