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

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

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

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發展
    數據加載中……

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

    寫函數的時候遇到了這樣的問題
    使用
    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

    原來,在函數中使用DML還真不能太隨意,SELECT可以;UPDATE/DELTE/INSERT還真不可以。
    網上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

    主站蜘蛛池模板: 亚洲人成人77777在线播放| 亚洲日韩乱码久久久久久| 91福利免费体验区观看区| 国产福利在线观看永久免费| 免费大片黄在线观看| 亚洲日本国产精华液| 久久国产亚洲高清观看| 亚洲成年轻人电影网站www| 最近最新中文字幕完整版免费高清 | 成人无码区免费A∨直播| 亚洲精品一卡2卡3卡三卡四卡| 午夜亚洲国产理论秋霞| 亚洲视频精品在线| 亚洲国模精品一区| 亚洲国产a级视频| 国产成人99久久亚洲综合精品| 亚洲综合色成在线播放| 亚洲精品成人片在线播放 | 免费人妻无码不卡中文字幕系 | 亚洲美女又黄又爽在线观看| 成人毛片18女人毛片免费| 在线观看免费成人| 日批日出水久久亚洲精品tv| 亚洲色偷偷综合亚洲AV伊人| 亚洲精品午夜国产VA久久成人| 亚洲人成影院在线| 亚洲一本之道高清乱码| 亚洲a∨国产av综合av下载 | 国产大片91精品免费看3| 亚洲人成无码网WWW| 亚洲精品无码永久在线观看你懂的 | 日韩人妻无码精品久久免费一 | 免费无码一区二区| eeuss影院www天堂免费| 久久午夜夜伦鲁鲁片无码免费| 巨波霸乳在线永久免费视频 | 亚洲国产成人综合精品| 黄色网址免费在线| 亚洲日韩精品无码专区| 日韩一级片免费观看| 免费无码又爽又刺激网站|