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. writing a scalar-function

writing a scalar-function

Scheduled Pinned Locked Moved Database
helpquestion
8 Posts 5 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.
  • M Offline
    M Offline
    mrkeivan
    wrote on last edited by
    #1

    Hey, I have the following code but I dunno why I get and error!!??

    CREATE FUNCTION [dbo].[GetSubTeacher] (@ID Int)

    RETURNS VARCHAR(100)

    AS

    BEGIN

    DECLARE @TeacherName NVARCHAR(100)
    IF (@ID = 0)
    
       SET @TeacherName = 'NO SUB'
    
    IF (@ID <> 0)
       SET @TeacherName = SELECT FName + ' ' + LName AS \[Name\] from Teachers WHERE ID = @ID
    

    RETURN @TeacherName

    END

    Incorrect syntax near the keyword 'SELECT'. Regards, K

    G J P 3 Replies Last reply
    0
    • M mrkeivan

      Hey, I have the following code but I dunno why I get and error!!??

      CREATE FUNCTION [dbo].[GetSubTeacher] (@ID Int)

      RETURNS VARCHAR(100)

      AS

      BEGIN

      DECLARE @TeacherName NVARCHAR(100)
      IF (@ID = 0)
      
         SET @TeacherName = 'NO SUB'
      
      IF (@ID <> 0)
         SET @TeacherName = SELECT FName + ' ' + LName AS \[Name\] from Teachers WHERE ID = @ID
      

      RETURN @TeacherName

      END

      Incorrect syntax near the keyword 'SELECT'. Regards, K

      G Offline
      G Offline
      Goutam Patra
      wrote on last edited by
      #2

      Try this

      CREATE FUNCTION [dbo].[GetSubTeacher] (@ID Int)

      RETURNS VARCHAR(100)

      AS

      BEGIN

      DECLARE @TeacherName NVARCHAR(100)
      IF (@ID = 0) BEGIN
      
         SET @TeacherName = 'NO SUB'
      END
      IF (@ID <> 0) BEGIN
         SET @TeacherName = (SELECT FName + ' ' + LName AS \[Name\] from Teachers WHERE ID = @ID)
      END
      

      RETURN @TeacherName

      END

      modified on Monday, August 9, 2010 8:10 AM

      1 Reply Last reply
      0
      • M mrkeivan

        Hey, I have the following code but I dunno why I get and error!!??

        CREATE FUNCTION [dbo].[GetSubTeacher] (@ID Int)

        RETURNS VARCHAR(100)

        AS

        BEGIN

        DECLARE @TeacherName NVARCHAR(100)
        IF (@ID = 0)
        
           SET @TeacherName = 'NO SUB'
        
        IF (@ID <> 0)
           SET @TeacherName = SELECT FName + ' ' + LName AS \[Name\] from Teachers WHERE ID = @ID
        

        RETURN @TeacherName

        END

        Incorrect syntax near the keyword 'SELECT'. Regards, K

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        You were very close:

        IF (@ID <> 0)
        SELECT @TeacherName = FName + ' ' + LName AS [Name] from Teachers WHERE ID = @ID

        1 Reply Last reply
        0
        • M mrkeivan

          Hey, I have the following code but I dunno why I get and error!!??

          CREATE FUNCTION [dbo].[GetSubTeacher] (@ID Int)

          RETURNS VARCHAR(100)

          AS

          BEGIN

          DECLARE @TeacherName NVARCHAR(100)
          IF (@ID = 0)
          
             SET @TeacherName = 'NO SUB'
          
          IF (@ID <> 0)
             SET @TeacherName = SELECT FName + ' ' + LName AS \[Name\] from Teachers WHERE ID = @ID
          

          RETURN @TeacherName

          END

          Incorrect syntax near the keyword 'SELECT'. Regards, K

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

          What's with the ifs? :wtf:

          alter FUNCTION GetSubTeacher
          (
          @ID integer
          )
          RETURNS Varchar(100)
          AS
          BEGIN
          RETURN ( SELECT CASE @ID WHEN 0 THEN 'NO SUB' ELSE ( SELECT FName+' '+LName FROM Teachers WHERE ID=@ID ) END )
          END

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            What's with the ifs? :wtf:

            alter FUNCTION GetSubTeacher
            (
            @ID integer
            )
            RETURNS Varchar(100)
            AS
            BEGIN
            RETURN ( SELECT CASE @ID WHEN 0 THEN 'NO SUB' ELSE ( SELECT FName+' '+LName FROM Teachers WHERE ID=@ID ) END )
            END

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Add in ISNULL() as well (you may need to move the WHEN) ;P

            alter FUNCTION GetSubTeacher
            (
            @ID integer
            )
            RETURNS Varchar(100)
            AS
            BEGIN
            RETURN ( SELECT CASE ISNULL(@ID,0) WHEN 0 THEN 'NO SUB' ELSE ( SELECT FName+' '+LName FROM Teachers WHERE ID=@ID ) END )
            END

            Never underestimate the power of human stupidity RAH

            P 1 Reply Last reply
            0
            • M Mycroft Holmes

              Add in ISNULL() as well (you may need to move the WHEN) ;P

              alter FUNCTION GetSubTeacher
              (
              @ID integer
              )
              RETURNS Varchar(100)
              AS
              BEGIN
              RETURN ( SELECT CASE ISNULL(@ID,0) WHEN 0 THEN 'NO SUB' ELSE ( SELECT FName+' '+LName FROM Teachers WHERE ID=@ID ) END )
              END

              Never underestimate the power of human stupidity RAH

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

              Well I don't like the "magic number" anyway; I'd rather simply search and return "NO SUB" if the ID doesn't exist. Besides, where'd they get the ID in the first place? :confused:

              M 1 Reply Last reply
              0
              • P PIEBALDconsult

                Well I don't like the "magic number" anyway; I'd rather simply search and return "NO SUB" if the ID doesn't exist. Besides, where'd they get the ID in the first place? :confused:

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                I'd rather use a left join instead of a function - this causes a sub select on every line, lousy design!

                Never underestimate the power of human stupidity RAH

                P 1 Reply Last reply
                0
                • M Mycroft Holmes

                  I'd rather use a left join instead of a function - this causes a sub select on every line, lousy design!

                  Never underestimate the power of human stupidity RAH

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

                  True, but we don't know how it'll be (mis-)used.

                  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