【问题标题】:sql error. i have used sql server 2012sql错误。我用过 sql server 2012
【发布时间】:2018-03-14 06:21:37
【问题描述】:
ALTER PROCEDURE combined_report_generation
AS
BEGIN

create table temp(id int identity(1,1),form_id varchar(50),process_id varchar(50),active_status int);

insert into temp(form_id,process_id,active_status) select form_id,process_id,active_status from audit_form_active;


DECLARE @MAXID INT, @Counter INT,@FI varchar(50),@PI varchar(50),@match varchar(50),@tablename varchar(100),@match_col varchar(50)
SET @COUNTER = 2
SELECT @MAXID = COUNT(*) FROM temp where active_status=1



WHILE (@COUNTER <= @MAXID)
BEGIN

SELECT @FI=form_id FROM temp where id=@COUNTER
SELECT @PI=process_id FROM temp where id=@COUNTER
SELECT @match=cdr_match_col FROM mapping_table where process_id=(select process_id FROM temp where id=@COUNTER)
SET @match_col='cdr_'+@match
SET @tablename='AUDITFORM_'+@FI


INSERT INTO combined_report
(

      [audit_id]
      ,[form_id]
      ,[audit_score]
      ,[scorable_value]
      ,[scoring_value]
      ,[UniqueColumnMappingCdr]
      ,[crm_AgentId]
      ,[crm_AgentName]
      ,[voice_record_id]
      ,[fatal_count]
      ,[fatal_status]
      ,[agent_disposition]
      ,[agent_disposition_status]
      ,[updated_status]
      ,[TeamLeader]
      ,[calibration_call]
      ,[sample_to]
      ,[created_by]
      ,[agent_disposition_date]
      ,[Audit_Start_Time]
      ,[created_on]
      ,[updated_by]
      ,[updated_on]
)

EXEC('
SELECT 
      [audit_id]
      ,[form_id]
      ,[audit_score]
      ,[scorable_value]
      ,[scoring_value]
      ,'+@match_col+'
      ,[crm_AgentId]
      ,[crm_AgentName]
      ,[voice_record_id]
      ,[fatal_count]
      ,[fatal_status]
      ,[agent_disposition]
      ,[agent_disposition_status]
      ,[updated_status]
      ,[TeamLeader]
      ,[calibration_call]
      ,[sample_to]
      ,[created_by]
      ,[agent_disposition_date]
      ,[Audit_Start_Time]
      ,[created_on]
      ,[updated_by]
      ,[updated_on]
  FROM '+@tablename+'where  cast(updated_on as datetime)  >(select MAX(cast(updated_on as datetime)) from combined_report)')

  SET @COUNTER=@COUNTER+1
END
DROP table temp
END
------

我收到此错误,我使用的是 sql server 2012 “updated_on”不是公认的表提示选项。如果它打算作为表值函数或 CHANGETABLE 函数的参数,请确保您的数据库兼容模式设置为 90。

alter database database_name set compatibility_level=90

【问题讨论】:

  • 您有多个AUDITFORM_ 的表仅由表单的ID 分隔,这意味着您的数据模型已损坏。由于您已经将 form_id 作为这些表中的列,您只需将所有这些表合并到一个 AUDITFORMS 表中,从而无需动态 sql。

标签: sql sql-server tsql stored-procedures


【解决方案1】:

你只需要在 where 之前有一个空格

EXEC('
SELECT 
      [audit_id]
      ,[form_id]
      ,[audit_score]
      ,[scorable_value]
      ,[scoring_value]
      ,['+@match_col+']
      ,[crm_AgentId]
      ,[crm_AgentName]
      ,[voice_record_id]
      ,[fatal_count]
      ,[fatal_status]
      ,[agent_disposition]
      ,[agent_disposition_status]
      ,[updated_status]
      ,[TeamLeader]
      ,[calibration_call]
      ,[sample_to]
      ,[created_by]
      ,[agent_disposition_date]
      ,[Audit_Start_Time]
      ,[created_on]
      ,[updated_by]
      ,[updated_on]
  FROM [' + @tablename + '] where  cast(updated_on as datetime)  >(select MAX(cast(updated_on as datetime)) from combined_report)')

【讨论】:

  • 谢谢@serkan Arslan 但现在我得到“'>' 附近的语法不正确”
【解决方案2】:

执行动态 SQL 时,请确保您的对象和语句关键字之间有空格。您的表名正在与 WHERE 关键字合并,在两者之间放置一个空格。

此外,当使用动态对象名称(如列或表)时,请确保使用 QUOTENAME() 函数正确转义它们。您的动态值可能包含需要用括号括起来的空格或奇怪字符。

SET @match_col = QUOTENAME(@match_col)
SET @tablename = QUOTENAME(@tablename)

要进一步调试错误,您可以尝试将动态 SQL 分配给变量并在 PRINTEXEC 之间切换:

DECLARE @DynamicSQL VARCHAR(MAX) = 'SELECT ....'

-- Debug
PRINT (@DynamicSQL)

-- Run
EXEC (@DynamicSQL)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 2012-09-30
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多