SQL Function not returning any value

  • Hi guys,

    I have a bit of problem here.

    My SQL Server function is not returning any value. There seems to be any configuration problem.

    Below is my script. Please help. Thanks in advance.

    /****** Object: UserDefinedFunction [dbo].[fnReplaceNonPrintCharsFortxtOnly] Script Date: 04/03/2008 12:21:09 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- =============================================

    -- Author:Sandeep Sahu

    -- Create date:01-April-2008

    -- Description:To Replace the Non Print characters with some strings and

    --remove all html tags after replacement

    -- Revision History:New Creation

    -- =============================================

    create FUNCTION [dbo].[fnReplaceNonPrintCharsFortxtOnly]

    -- exec fnReplaceNonPrintCharsFortxtOnly ' '

    (

    @Text AS VARCHAR(8000)

    )

    RETURNS VARCHAR(8000)

    AS

    BEGIN

    DECLARE @return AS VARCHAR(8000)

    DECLARE @StartPos AS INT

    DECLARE @EndPos AS INT

    DECLARE @LenStr AS INT

    DECLARE @inttmpId AS INT

    DECLARE @strToReplace AS VARCHAR(8000)

    DECLARE @strToReplaceTxtOnly AS VARCHAR(8000)

    DECLARE ReplaceNonPrintChr_txtOnly CURSOR FAST_FORWARD FOR

    SELECT ReplaceNonPrintCharID,String_To_Replace,String_To_Replace_in_txtOnly

    FROM LKP_ReplaceNonPrintCharacter

    WHERE IsDeleted = 0

    OPEN ReplaceNonPrintChr_txtOnly

    FETCH NEXT FROM ReplaceNonPrintChr_txtOnly

    INTO @inttmpId,@strToReplace,@strToReplaceTxtOnly

    WHILE @@FETCH_STATUS = 0

    BEGIN

    IF(@strToReplace <> '')

    BEGIN

    IF(@strToReplaceTxtOnly <> '' AND @strToReplaceTxtOnly <> 'Empty String')

    BEGIN

    SET @Text = REPLACE(@Text,@strToReplace,@strToReplaceTxtOnly)

    END

    ELSE IF(@strToReplaceTxtOnly = 'Empty String')

    BEGIN

    SET @Text = REPLACE(@Text,@strToReplace,'')

    END

    END

    FETCH NEXT FROM ReplaceNonPrintChr_txtOnly INTO @inttmpId,@strToReplace,@strToReplaceTxtOnly

    END -- end of cursor begin statement

    CLOSE ReplaceNonPrintChr_txtOnly

    DEALLOCATE ReplaceNonPrintChr_txtOnly

    -- To Remove all HTML Tags after the above replacement is done

    SET @StartPos = CHARINDEX('<', @Text)

    SET @EndPos = CHARINDEX('>', @Text, CHARINDEX('<', @Text))

    SET @LenStr = (@EndPos - @StartPos) + 1

    WHILE (@StartPos > 0 AND @EndPos > 0 AND @LenStr > 0)

    BEGIN

    SET @Text = STUFF(@Text, @StartPos, @LenStr, '')

    SET @StartPos = CHARINDEX('<', @Text)

    SET @EndPos = CHARINDEX('>', @Text, CHARINDEX('<', @Text))

    SET @LenStr = (@EndPos - @StartPos) + 1

    END

    SET @return = @Text

    RETURN @return

    END

    Even the following script does not work.....

    CREATE FUNCTION [dbo].[FNTest]

    ()

    RETURNS VARCHAR(20)

    AS

    BEGIN

    RETURN 'RR'

    END

    It seems none of the functions are working...

    Chandrachurh Ghosh
    DBA – MS SQL Server
    Ericsson India Global Services Limited
    Quality is not an act, it is a habit.

  • Hi to all

    can anybody tell me how can i create new user login in sql server 2005 for my database?

    Thanks in advance

  • Seeing as none of your functions are working let's look at the small test function you created. What do you get when you run:

    select dbo.FNTest()

  • saratheamit2006 (4/3/2008)


    Hi to all

    can anybody tell me how can i create new user login in sql server 2005 for my database?

    Thanks in advance

    You should start a new post rather than post your question in response to someone elses question.

    But to answer your question, have a look at the CREATE LOGIN statement in books online. There are various examples in there on how to create a new login in SQL 2005.

  • What error are you getting? Your test function ran fine on my machine.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I am extremely sorry for this. I had done a EXEC

    They are working fine. It happens you know, when in tight situation, getting the basic stuff wrong.

    Anyway, as the point has come, what is the difference between a function and a stored procedure. Is it only compilation?

    Chandrachurh Ghosh
    DBA – MS SQL Server
    Ericsson India Global Services Limited
    Quality is not an act, it is a habit.

  • Much more than compilation. Functions come in two basic flavors, table valued & scalar valued. Scalar is used for a calculation, table valued are sort of like parameterized views. Only problem is, the scalar valued functions, when used in queries, can almost make the query go from a nice set based function to a RBAR (row-by-agonizing-row) function. And the table valued functions, when used in a single statement are usually fine, but when used with multi-statements, since they don't have statistics associated and the optimizer can't really generate plans for them, it assumes that the function is returning 1 (one) row and makes it's decisions from that fact. No big deal when returning a small number of rows, but a HUGE deal when returning larger numbers of rows.

    Stored procedures are utterly different critters. I'd suggest hitting the Books Online & reading up on both to get more details. Then come back & read through the articles & posts to see the good uses functions can provide and the problems they can create.

    Just as an example, we had a development team that saw how the functions worked and decided that they made the perfect sort of transition between object oriented code and database access. So they wrote queries that joined functions to functions and those functions called functions that joined between functions which called to ther functions,etc. It all appeared very elegant. Since all the execution plans were based on a single row of data, and the developers, while developing only ever used a single row of data for testing their code, everything worked fine. Then, after development & qa, we were getting ready for the production release and more extensive testing with production level data started. We got blocks, deadlocks, timeouts... It was a horrifying nightmare. We had to rewrite the entire data access layer into regular old SELECT statements inside stored procedures, not fun.

    Be VERY careful about the use of functions.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks a lot. The post was really helpful. 'll surely keep those points in mind.

    Chandrachurh Ghosh
    DBA – MS SQL Server
    Ericsson India Global Services Limited
    Quality is not an act, it is a habit.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply