Results 1 to 11 of 11

Thread: What is wrong with this UDF? MS SQL 2K5

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    What is wrong with this UDF? MS SQL 2K5

    Here is my User Defined Function that I am using with MS SQL 2K5:

    T-SQL Code:
    1. IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_InsertSplit]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    2. DROP FUNCTION [dbo].[fn_InsertSplit]
    3. GO
    4.  
    5. CREATE  FUNCTION fn_InsertSplit(
    6.     @TableName nVarChar(100),
    7.     @FieldName nVarChar(100),
    8.     @text varchar(8000),
    9.     @delimiter varchar(20) = ' '
    10. )
    11.  
    12.     RETURNS nvarchar(10)
    13.  
    14. AS
    15.  
    16. BEGIN
    17.     DECLARE @index int
    18.     DECLARE @SQL as nVarChar(1000)
    19.     DECLARE @Result nVarChar(100)
    20.  
    21.     SET @index = -1
    22.  
    23.     WHILE (LEN(@text) > 0)
    24.       BEGIN  
    25.         SET @index = CHARINDEX(@delimiter , @text)  
    26.         IF (@index = 0) AND (LEN(@text) > 0)  
    27.             BEGIN  
    28.                 SET @SQL = 'INSERT ' + @TableName + '(' + @FieldName + ') VALUES(' + (LEFT(@text, @index - 1)) + ')'
    29.                 EXEC sp_executesql @SQL
    30.                 BREAK  
    31.               END  
    32.         IF (@index > 1)  
    33.             BEGIN  
    34.                 SET @SQL = 'INSERT ' + @TableName + '(' + @FieldName + ') VALUES(' + (LEFT(@text, @index - 1)) + ')'
    35.                 EXEC sp_executesql @SQL
    36.                 SET @text = RIGHT(@text, (LEN(@text) - @index))  
    37.             END  
    38.         ELSE
    39.             SET @text = RIGHT(@text, (LEN(@text) - @index))
    40.         END
    41.     SET @RESULT = 'Done'   
    42.     RETURN @RESULT
    43. END

    and I am calling it like this:

    T-SQL Code:
    1. SELECT IADATA.dbo.fn_InsertSplit('dbo.College_CLASSES','College_CLASS','A,B,C,D,E,F',',')

    I am getting the following error:

    Msg 557, Level 16, State 2, Line 1
    Only functions and extended stored procedures can be executed from within a function.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: What is wrong with this UDF? MS SQL 2K5

    Within a UDF, you cannot execute Insert, Update, Delete statements that will affect database tables.
    sp_ExecuteSQL is not an "extended stored procedure".

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: What is wrong with this UDF? MS SQL 2K5

    Quote Originally Posted by brucevde
    Within a UDF, you cannot execute Insert, Update, Delete statements that will affect database tables.
    sp_ExecuteSQL is not an "extended stored procedure".

    Hmmm, so I guess I am out of luck then?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: What is wrong with this UDF? MS SQL 2K5

    Quote Originally Posted by brucevde
    Within a UDF, you cannot execute Insert, Update, Delete statements that will affect database tables.
    Really?

    I've never tried to do anything but SELECT's in UDF's.

    But it's amazing to me that they prohibit insert, update and delete statements.

    Triggers can do insert, update and delete - I always considered a UDF just a cousin of a trigger.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: What is wrong with this UDF? MS SQL 2K5

    What you want to do is create a Multistatement Table-valued Function.

    The function returns a table representing the splitted values, and you reference it as any other table in an INSERT statement.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: What is wrong with this UDF? MS SQL 2K5

    Table Value Functions are my new best friend...

    I put together a code bank example of using it - with the CROSS APPLY JOIN that really makes then useful

    http://www.vbforums.com/showthread.php?t=502948

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: What is wrong with this UDF? MS SQL 2K5

    Quote Originally Posted by kaffenils
    What you want to do is create a Multistatement Table-valued Function.

    The function returns a table representing the splitted values, and you reference it as any other table in an INSERT statement.

    But I don't know the name of the table that I want to insert the data into, that is why I am using the Dynamic SQL string.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: What is wrong with this UDF? MS SQL 2K5

    Why a function? Should be a stored procedure straight up....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: What is wrong with this UDF? MS SQL 2K5

    Quote Originally Posted by techgnome
    Why a function? Should be a stored procedure straight up....

    -tg

    Yeah, I was thinking about that but I never created a function before and I wanted to see what they could do. Basically, what is the difference between a SPROC and a Function? They both can return values, correct?

    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: What is wrong with this UDF? MS SQL 2K5

    Back in SQL 2000 - only SPROCS and UDF's

    SPROC could be EXECUTED.

    You could take the results of the EXEC and put them into a TEMP TABLE in another SPROC.

    But you could not JOIN to the SPROC nor include it in the SELECT list.

    UDF's - you could include in the SELECT list.

    Keep in mind that the traditional reason for a UDF is for a scalar function - such as formatting a phone #'s with ()-'s or an SSN - pass in a string - pass back out a string. Or a number... You get my meaning?

    At any rate - now - with TABLE VALUE FUNCTIONS - the lines are blurred. TVF's can return a table. Can be JOIN'd to in a SELECT. The power of that is tremendous. Now a TVF is basically a VIEW with CODE!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: What is wrong with this UDF? MS SQL 2K5

    Hmm, I need a little more time to wrap my brain around this. Thanks Steve!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width