<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

    sqlserver: Invalid use of a side-effecting operator within a function (Print/Raiserror)

    寫函數(shù)的時候遇到了這樣的問題
    使用
    raiserror('enter this line', 16, -1) with log
    print('enter this line')

    分別遇到了
    Invalid use of a side-effecting operator 'Print' within a function
    Invalid use of a side-effecting operator 'Raiserror' within a function

    原來,在函數(shù)中使用DML還真不能太隨意,SELECT可以;UPDATE/DELTE/INSERT還真不可以。
    網(wǎng)上google了下,還真是不少類似的案例
    1. Functions are used to return data: scalar-value or table-value.
    2. 
    Invalid use of side-effecting or time-dependent operator in ‘newid’ within a function.
    It states that the use of side-effecting and time-dependent operators is not allowed within a function. 


    Side-Effecting Operators
    A side-effecting operator is basically what it says.  
    It is an operator that affects anything outside the function.  
    This could be seen when trying to create an object or insert or update a table.  

    Functions should be self-contained.  
    They can use data from the database, but should not affect data in the database.  
    In other words, functions should be read-only.

    There is a list of side-effecting operators, which include INSERT, CREATE, UPDATE, OPEN, CLOSE, DELETE, SELECT … INTO, and more.  
    You cannot use these inside a function.  
    If you do, you will receive a slightly different message.  The message will not mention time-dependent operators.  It will look like this.
    Msg 443, Level 16, State 15, Procedure FN_TEST, Line 9
    Invalid use of a side-effecting operator ‘INSERT’ within a function. 

    To get around some of the side-effecting operator rules shown by Msg 443, we can use a table variable.  
    Below is an example on a table valued function.  It returns a table variable.  
    In the function we insert, update, and delete from the table variable.  
    You cannot create a temp table, but table variables are OK.

    CREATE FUNCTION FN_TEST()
    RETURNS @table TABLE (
     i int,
     j int)
    AS
    BEGIN
     
     INSERT INTO @table
     SELECT 1, 0
     UNION ALL
     SELECT 2, 2
      
     UPDATE @table
     SET j = 1
      
     DELETE @table
     WHERE i = 1
      
     RETURN
      
    END

    If you try this function yourself, you will see that it compiles and executes with no problem. 
    Operators used on table variables are not considered side-effecting operators.

    Time-Dependent Operators
    You will also receive Msg 443 if you use time-dependent operators in a function.  
    Time-dependent operators are those which return a value based on the time.  
    This include, but are not limited to GETDATE(), SYSDATETIME(), NEWID(), and RAND().  
    This is because functions should to return the same value when provided with the same set of inputs.  
    Since these functions return a different value each time, they are not allowed. 
    There is one solution to part of this problem.  If you wish to use a date or an id, you can provide it to the function.  
    You could pass NEWID() or GETDATE() as the value of a parameter sent to the function.  
    If you need to use time-dependent operators multiple times, you can’t use a function.

    posted on 2013-10-25 18:24 crazycy 閱讀(2992) 評論(0)  編輯  收藏 所屬分類: DBMS

    主站蜘蛛池模板: 国产亚洲综合网曝门系列| 国产精品久免费的黄网站| 99久热只有精品视频免费观看17| 69xx免费观看视频| 免费乱码中文字幕网站| 日产亚洲一区二区三区| 亚洲av日韩av永久无码电影| 国产精品区免费视频| 大陆一级毛片免费视频观看| 久久久无码精品亚洲日韩软件 | 亚洲精品高清久久| 国产偷国产偷亚洲清高APP| 国产精品免费无遮挡无码永久视频| 久久亚洲精品中文字幕无码| 特级毛片A级毛片100免费播放| 亚洲高清免费在线观看| 国产成人精品久久亚洲| 亚洲人成网站999久久久综合| 国产一级a毛一级a看免费视频 | 亚洲蜜芽在线精品一区| 中文字幕永久免费| 亚洲AV美女一区二区三区| 67194国产精品免费观看| 亚洲色精品三区二区一区| 无人在线直播免费观看| 亚洲精选在线观看| 国产又黄又爽又猛免费app| 最新亚洲人成无码网www电影| 亚洲中文字幕无码一区| 羞羞的视频在线免费观看| 亚洲日韩欧洲无码av夜夜摸| 国产成人高清亚洲一区91| 成人免费看黄20分钟| 久久亚洲国产最新网站| 免费看成人AA片无码视频羞羞网| 色综合久久精品亚洲国产| 亚洲成av人片天堂网| 久久福利青草精品资源站免费| 亚洲午夜久久久久久久久久| 一二三四在线播放免费观看中文版视频 | 国产免费啪嗒啪嗒视频看看|