当我尝试创建此过程时,如何解决此错误“ ERROR 102 sql state 420000”

时间:2019-05-20 09:31:46

标签: sql sql-server database procedure

我试图创建一个过程来检查输入的数字(作为nvarchar类型)是否已经存在。如果存在,则会进行更新,否则会向用户抛出错误。但是无法创建该过程,因为一旦执行命令ERROR 102/156 SQL STATE 42000,就会出现该问题。

CREATE PROCEDURE UpdateAWBUnique
(
    @AWB as nvarchar,
    @Internal as nvarchar
)
AS
BEGIN
    SET NOCOUNT ON
    if((SELECT count(*) From Def_tab WHERE AWB=@AWB) == 0) 
    BEGIN
        UPDATE Def_tab SET AWB= @AWB
        where Internal=@Internal
    END
    ELSE
    BEGIN
        PRINT 'Already Exits'
    END
END
GO

仅供参考,我使用Microsoft SQL Server。如何清除此错误并创建过程?

2 个答案:

答案 0 :(得分:2)

有条件地更新并查看是否发生任何更改

    UPDATE Def_tab 
    SET AWB = @AWB
    WHERE Internal = @Internal 
         AND NOT EXISTS (SELECT 1 FROM Def_tab t WHERE t.AWB= @AWB)
    IF (@@ROWCOUNT = 0)
        PRINT 'Already Exits'

否则,在并发环境中,两者之间可能会发生某些事情

if((SELECT count(*) From Def_tab WHERE AWB=@AWB) == 0) 

BEGIN
    UPDATE Def_tab SET AWB= @AWB

使if支票无效

答案 1 :(得分:0)

您似乎希望alter table t add constraint unq_def_tab_awb unique (awb); 是唯一的。如果是这样,则应让数据库强制执行数据完整性。定义唯一约束:

insert

不需要存储过程。

这还将检查 playerManager.addEventListener(cast.framework.events.EventType.PLAYER_LOAD_COMPLETE, () => { log('[mediacast:events:PLAYER_LOAD_COMPLETE'); console.log(playerManager.getStats()); console.log(playerManager.getMediaInformation()); }); playerManager.addEventListener(cast.framework.events.EventType.BITRATE_CHANGED, (event) => { log('[mediacast:events:BITRATE_CHANGED - ' + event.totalBitrate); stats.bitrate = event.totalBitrate; console.log(playerManager.getStats()); }); playerManager.addEventListener(cast.framework.events.EventType.PLAYING, (event) => { log('[mediacast:events:PLAYING - ', JSON.stringify(event)); }); playerManager.addEventListener(cast.framework.events.EventType.PAUSE, (event) => { log('[mediacast:events:PAUSE - ', JSON.stringify(event)); }); playerManager.addEventListener(cast.framework.events.EventType.SEEKING, (event) => { log('[mediacast:events:SEEKING - ', JSON.stringify(event)); }); playerManager.addEventListener(cast.framework.events.EventType.BUFFERING, (event) => { log('[mediacast:events:BUFFERING - ', JSON.stringify(event)); }); playerManager.addEventListener(cast.framework.events.EventType.TIME_UPDATE, (event) => { // log('[mediacast:events:TIME_UPDATE - ', JSON.stringify(event)); stats.currentMediaTime = event.currentMediaTime; }); playerManager.addEventListener(cast.framework.events.EventType.MEDIA_STATUS, (event) => { log('[mediacast:events:MEDIA_STATUS - ', JSON.stringify(event)); stats.state = event.mediaStatus.playerState; }); 上的值。而且由于数据库正在执行检查,因此您不必担心有人更新表时是否实际调用了存储过程。