动态SQL Server更新过程提供无效列错误

时间:2015-11-21 13:14:24

标签: sql sql-server tsql stored-procedures

我正在运行一个存储过程,该存储过程调用此存储过程以根据传递的参数更新表中的记录,但是我收到以下错误。

  

Msg 207,Level 16,State 1,
  第1行无效的列名称' 1.24483e + 007'。

     

Msg 207,Level 16,State 1,
  第1行无效的列名称' 1.24101e + 007'。

我无法想象我在这里想念的是什么?

代码:

ALTER PROCEDURE [dbo].[usr_UPDATEMEANSTDEV]
    @TableName Sysname,
    @Symbol NVARCHAR(50),
    @MeanLTD float, 
    @StdevLTD float, 
    @MaxLTD float,
    @MeanYTD float, 
    @StdevYTD float, 
    @MaxYTD float
    --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @SQLCommand NVARCHAR(MAX) = 
        N'UPDATE ' + QUOTENAME(@TableName) + 
        ' SET LTD_MEAN = ' + QUOTENAME(@MeanLTD) + 
        ',YTD_MEAN = ' + QUOTENAME(@MeanYTD) +
        ',LTD_MAX = ' + QUOTENAME(@MaxLTD) +
        ',YTD_MAX = ' + QUOTENAME(@MaxYTD) +
        ',LTD_STDEV = ' + QUOTENAME(@StdevLTD) +
        ',YTD_STDEV = ' + QUOTENAME(@StdevYTD) +
        + ' WHERE (SYMBOL = @SYMBOL)';

    EXECUTE [dbo].[sp_executesql]  @sqlCommand,
                  N'@Symbol nvarchar(50)', @Symbol = @Symbol;
END

2 个答案:

答案 0 :(得分:0)

问题是,你把你的值包装在方括号中,这是不正确的,你的浮点数被列为列(好吧,DB中的任何对象),当然这些不存在。这应该是正确的:

ALTER PROCEDURE [dbo].[usr_UPDATEMEANSTDEV]
    -- Add the parameters for the stored procedure here
    @TableName Sysname,
    @Symbol NVARCHAR(50),
    @MeanLTD FLOAT, 
    @StdevLTD FLOAT, 
    @MaxLTD FLOAT,
    @MeanYTD FLOAT, 
    @StdevYTD FLOAT, 
    @MaxYTD FLOAT
    --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    -- DECLARE @DATEVARCHAR NVARCHAR(4000);
    DECLARE @SQLCommand NVARCHAR(MAX) = N'
        UPDATE ' + QUOTENAME(@TableName) + N'
        SET LTD_MEAN = @MeanLTD
          , YTD_MEAN = @MeanYTD
          , LTD_MAX = @MaxLTD
          , YTD_MAX = @MaxYTD
          , LTD_STDEV = @StdevLTD
          , YTD_STDEV = @StdevYTD
        WHERE SYMBOL = @SYMBOL';

    EXECUTE dbo.sp_executesql @sqlCommand
      , N '@Symbol NVARCHAR(50), @MeanLTD FLOAT, @StdevLTD FLOAT, @MaxLTD FLOAT, @MeanYTD FLOAT, @StdevYTD FLOAT, @MaxYTD FLOAT'
      , @Symbol
      , @MeanLTD
      , @StdevLTD
      , @MaxLTD
      , @MeanYTD
      , @StdevYTD
      , @MaxYTD;
END

答案 1 :(得分:0)

删除QUOTENAME功能。你无法使用它覆盖值。它应该在任何sql reserved keywords , symbols and space中转义objects

现在,Column names语句中的值将被视为update,因为它附带square brackets,这是错误的原因。 示例[1.2]将被解析为VNASAAIT

中的列
 DECLARE @SQLCommand NVARCHAR(MAX) = 
        N'UPDATE ' + QUOTENAME(@TableName) + 
        ' SET LTD_MEAN = ' + cast(@MeanLTD as varchar(50)) + 
        ',YTD_MEAN = ' + cast(@MeanYTD as varchar(50)) +
        ',LTD_MAX = ' + cast(@MaxLTD as varchar(50)) +
        ',YTD_MAX = ' + cast(@MaxYTD as varchar(50)) +
        ',LTD_STDEV = ' + cast(@StdevLTD as varchar(50)) +
        ',YTD_STDEV = ' + cast(@StdevYTD as varchar(50)) +
        + ' WHERE (SYMBOL = @SYMBOL)';