存储过程执行出错

时间:2014-03-09 01:41:25

标签: sql sql-server stored-procedures

当我尝试执行存储过程时,它显示此错误

Msg 8152, Level 16, State 14, Procedure MA01003_SUM1, Line 12
String or binary data would be truncated.
The statement has been terminated.

以下是存储过程

ALTER procedure [dbo].[MA01003_SUM1]
as 
Begin

  declare @desc as varchar(50)
  set @desc = dbo.sdescription(1,0,3)


  declare @cost as float
  set @cost = dbo.SCost(0)

 Insert into SummaryLoad(
 SL_TierName,
 SL_CorporateName,
 SL_HospiceName, 
 SL_GroupName,
 SL_DateKey,
 SL_FactAmt,
 SL_AHT,
 SL_headingNo,
 SL_Staffno,
 SL_factno,
 SL_Description,
 SL_Cost) 



 SELECT [MA_TierName]                   
  ,[MA_CorporateName]                   
  ,[MA_HospiceName]                 
  ,[MA_GroupName]                   
  ,[MA_Datekey]                 
  ,SUM([MA_NumContacts]) Contacts                   
  ,SUM([MA_Duration]) ActualHandleTime,
  '1',
  '0',
  '3',              
  @desc,
  @cost

 FROM [DM_ResourceUtilization].[dbo].[MedicationsAdded] 
 GROUP BY [MA_TierName]                 
  ,[MA_CorporateName]                   
  ,[MA_HospiceName]                 
  ,[MA_GroupName]                   
  ,[MA_Datekey] 

  end

2 个答案:

答案 0 :(得分:0)

可能是表SummaryLoad的数据类型或宽度与select语句输出的数据类型或宽度不匹配的可能性。请参阅以下脚本来说明概念 -

use tempdb 
go 
create table #test ( c1 varchar(5))  
go 
declare @desc as varchar(10)  -- Width is 10 only
set @desc = 'AAAAAABBBBBBBBBBBbbCCCCCCCCCCCCCCcDDDDDDDDDDDDDD'  -- Assigning higher width value
select @desc -- No error thrown, SQL server do the data truncation silently

insert into #test(c1) values ( @desc )  -- Here SQL server throws an error, 
       -- reason - value in @desc is of width 10, but target column width is 5

更好的是,你可以毫不费力地找到有问题的专栏或陈述。要执行此操作,请从过程中注释掉每个列及其值,然后在事务中调用它,或者为每个列逐个提供与每个列的目标数据类型匹配的常量值。样本如下 -

Insert into SummaryLoad(
 SL_TierName, 
 SL_CorporateName,
 SL_HospiceName, 
 SL_GroupName,
 SL_DateKey,
 SL_FactAmt,
 SL_AHT,
 SL_headingNo,
 SL_Staffno,
 SL_factno,
 SL_Description,
 SL_Cost) 

 SELECT 
  /*[MA_TierName] */ -- Commented out 
   'A'  -- Sample value, alter the procedure with this insert and run in transaction
        -- If that is successful, this is offending column. Change the value with matching 
        -- data type and width of column SL_TierName of table SummaryLoad
        -- Repeat this for all columns till you find the offending column(s)
  ,[MA_CorporateName]                   
  ,[MA_HospiceName]                 
  ,[MA_GroupName]                   
  ,[MA_Datekey]                 
  ,SUM([MA_NumContacts]) Contacts                   
  ,SUM([MA_Duration]) ActualHandleTime,
  '1',
  '0',
  '3',              
  @desc,
  @cost

 FROM [DM_ResourceUtilization].[dbo].[MedicationsAdded] 
 GROUP BY [MA_TierName]                 
  ,[MA_CorporateName]                   
  ,[MA_HospiceName]                 
  ,[MA_GroupName]                   
  ,[MA_Datekey] 

应在事务中调用已修改的过程,以确保您的数据不受此测试的影响。可以使用以下语句在事务中调用过程

begin transaction 
exec [dbo].[MA01003_SUM1]
rollback transaction 

答案 1 :(得分:0)

根据您的错误消息:

Msg 8152, Level 16, State 14, Procedure MA01003_SUM1, Line 12
String or binary data would be truncated.
The statement has been terminated.

可能是您的列宽问题与列中定义的大小重叠或过大。

所以,你必须检查哪个列的大小和数量。您传递的数据(即数据大小)大于列中定义的大小。