日期:2014-05-18  浏览次数:20724 次

ms sql触发器问题
触发器代码如下:create trigger up_SEOutStock on SEOutStock 
for insert 
as 
set nocount on 
if exists( 
select 1 
from inserted c 
join seoutstockentry d 
on c.finterid=d.finterid 
join ( 
select fitemid,sum(fqty) 库存数量 
from ICInventory 
group by fitemid) e 
on d.fitemid=e.fitemid 
where d.fqty>e.库存数量) 
begin 
declare @s varchar(1000) 
select @s=isnull(@s+",","")+f.fnumber 
from inserted c 
join seoutstockentry d 
on c.finterid=d.finterid 
join ( 
select fitemid,sum(fqty) 库存数量 
from ICInventory 
group by fitemid) e 
on d.fitemid=e.fitemid 
join t_icitem f 
on d.fitemid=f.fitemid 
where d.fqty>e.库存数量 
exec("raiserror(""物料代码为 "+@s+" 超出库存数,单据不允许保存!"",18,1)") 
rollback tran 
end 
set nocount off 
go 
上面触发器语句执行 提示一下错误:
消息 1038,级别 15,状态 3,过程 up_SEOutStock,第 18 行
不能使用空白的对象或列名。如果必要,请使用一个空格。
消息 170,级别 15,状态 1,过程 up_SEOutStock,第 30 行
第 30 行: 'raiserror("物料代码为 ' 附近有语法错误。



------解决方案--------------------
try:
SQL code
create trigger up_SEOutStock on SEOutStock  
for insert  
as  
set nocount on  
if exists(  
select 1  
from inserted c  
join seoutstockentry d  
on c.finterid=d.finterid  
join (  
select fitemid,sum(fqty) 库存数量  
from ICInventory  
group by fitemid) e  
on d.fitemid=e.fitemid  
where d.fqty>e.库存数量)  
begin  
declare @s varchar(1000)  
select @s=isnull(@s+',','')+f.fnumber  
from inserted c  
join seoutstockentry d  
on c.finterid=d.finterid  
join (  
select fitemid,sum(fqty) 库存数量  
from ICInventory  
group by fitemid) e  
on d.fitemid=e.fitemid  
join t_icitem f  
on d.fitemid=f.fitemid  
where d.fqty>e.库存数量  
exec('raiserror(''物料代码为 '+@s+' 超出库存数,单据不允许保存!'',18,1)')  
rollback tran  
end  
set nocount off  
go

------解决方案--------------------
将" 变成' 试试
------解决方案--------------------
SQL code
create trigger up_SEOutStock on SEOutStock  
for insert  
as  
set nocount on  
if exists(  
select 1  
from inserted c  
join seoutstockentry d  
on c.finterid=d.finterid  
join (  
select fitemid,sum(fqty) 库存数量  
from ICInventory  
group by fitemid) e  
on d.fitemid=e.fitemid  
where d.fqty>e.库存数量)  
begin  
declare @s varchar(1000)  
select @s=isnull(@s+',','')+f.fnumber  
from inserted c  
join seoutstockentry d  
on c.finterid=d.finterid  
join (  
select fitemid,sum(fqty) 库存数量  
from ICInventory  
group by fitemid) e  
on d.fitemid=e.fitemid  
join t_icitem f  
on d.fitemid=f.fitemid  
where d.fqty>e.库存数量  
exec('raiserror('物料代码为 char(39) +@s+ char(39) 超出库存数,单据不允许保存!',18,1)')  
rollback tran  
end  
set nocount off  
go

------解决方案--------------------
SQL code

CREATE TRIGGER up_SEOutStock
ON SEOutStock
FOR  INSERT
AS
    SET NOCOUNT ON  
    IF EXISTS(
           SELECT 1
           FROM   INSERTED c
                  JOIN seoutstockentry d
                       ON  c.finterid = d.finterid
                  JOIN (
                           SELECT fitemid,
                                  SUM(fqty) 库存数量
                           FROM   ICInventory
                           GROUP BY
                                  fitemid
                       ) e
                       ON  d.fitemid = e.fitemid
           WHERE  d.fqty > e.库存数量
       )
    BEGIN
        DECLARE @s VARCHAR(1000)  
        SELECT @s = ISNULL(@s + ',', '') + f.fnumber
        FROM   INSERTED c
               JOIN seoutstockentry d