possible to use @variable in function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheryl culwell
    New Member
    • Dec 2010
    • 4

    possible to use @variable in function?

    when using the following code in my function, I'm getting errors. Is it possible to set a variable and then use it in a function as follows?

    if OBJECT_ID ('tvf_Inline_Te st') is not null
    drop function tvf_Inline_Test
    go
    create function tvf_Inline_Test (@vstrDate char(15),
    @VSTRDTYR char(15),
    @mydatevar char(15),
    @thismo as char(15))
    set @thismo = right ('00' + CAST(DATEPART(M onth, getdate()) AS VARCHAR(2)),2)
    set @vstrDate = right ('00' + CAST(DATEPART(M onth, getdate())-7 AS VARCHAR(2)),2)
    --set @VSTRDTYR = CAST(DATEPART(Y YYY, GETDATE()) AS VARCHAR(4))
    SELECT @VSTRDTYR =
    CASE
    WHEN @thismo = 12 THEN CAST(DATEPART(Y YYY, GETDATE())-1 AS VARCHAR(4))
    ELSE CAST(DATEPART(Y YYY, GETDATE()) AS VARCHAR(4))
    END
    set @mydatevar = LTRIM(RTRIM(@VS TRDTYR)) + '/' + LTRIM(RTRIM(@vs trDate))
    returns table
    as
    return select wellID,yearmont h
    from well_summary where well_summary.ye armonth = @mydatevar
    go
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    yes you can, but you cannot use getdate() in a function
    add another param and pass getdate()in that, then every reference within the function to getdate() should refer to the parameter value

    Comment

    Working...