Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Function problem

SQL Function problem

Scheduled Pinned Locked Moved Database
sharepointdatabasehelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    Anyone have any ideas why this won't work? I just want to check whether some fields are not null before I set the value of @UserName

    IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = Object_ID('[dbo].[__Reporting_GetUserName]')
    AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    BEGIN
    DROP FUNCTION [dbo].[__Reporting_GetUserName]
    END

    BEGIN
    EXEC dbo.sp_executesql N'
    CREATE FUNCTION [dbo].[__Reporting_GetUserName] (@userId int)
    RETURNS VARCHAR(MAX)
    AS
    BEGIN

    DECLARE @UserName VARCHAR(MAX)

    SET @UserName =
    (
    IF [FirstName] is not null
    BEGIN
    SELECT [FirstName] + '' '' + [LastName]
    FROM __User
    WHERE Id = @userId
    END
    )

    RETURN @UserName
    END
    '
    END

    P L 2 Replies Last reply
    0
    • E Etienne_123

      Anyone have any ideas why this won't work? I just want to check whether some fields are not null before I set the value of @UserName

      IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = Object_ID('[dbo].[__Reporting_GetUserName]')
      AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
      BEGIN
      DROP FUNCTION [dbo].[__Reporting_GetUserName]
      END

      BEGIN
      EXEC dbo.sp_executesql N'
      CREATE FUNCTION [dbo].[__Reporting_GetUserName] (@userId int)
      RETURNS VARCHAR(MAX)
      AS
      BEGIN

      DECLARE @UserName VARCHAR(MAX)

      SET @UserName =
      (
      IF [FirstName] is not null
      BEGIN
      SELECT [FirstName] + '' '' + [LastName]
      FROM __User
      WHERE Id = @userId
      END
      )

      RETURN @UserName
      END
      '
      END

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Why not use a CASE? SELECT CASE WHEN [FirstName] is not null THEN [FirstName] + '' '' + [LastName] ELSE null END FROM __User WHERE Id = @userId (Or something.)

      1 Reply Last reply
      0
      • E Etienne_123

        Anyone have any ideas why this won't work? I just want to check whether some fields are not null before I set the value of @UserName

        IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = Object_ID('[dbo].[__Reporting_GetUserName]')
        AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
        BEGIN
        DROP FUNCTION [dbo].[__Reporting_GetUserName]
        END

        BEGIN
        EXEC dbo.sp_executesql N'
        CREATE FUNCTION [dbo].[__Reporting_GetUserName] (@userId int)
        RETURNS VARCHAR(MAX)
        AS
        BEGIN

        DECLARE @UserName VARCHAR(MAX)

        SET @UserName =
        (
        IF [FirstName] is not null
        BEGIN
        SELECT [FirstName] + '' '' + [LastName]
        FROM __User
        WHERE Id = @userId
        END
        )

        RETURN @UserName
        END
        '
        END

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Etienne_123 wrote:

        Anyone have any ideas why this won't work?

        What would it fill the variable with if FirstName does equall null? You'd better assign it directly without the IF statement, and use the [ISNULL](http://msdn.microsoft.com/en-us/library/ms184325.aspx)[[^](http://msdn.microsoft.com/en-us/library/ms184325.aspx "New Window")] function.

        Bastard Programmer from Hell :suss:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups