Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:46:00+00:00 2026-05-16T05:46:00+00:00

I wrote the code for a View which needs to call a user defined

  • 0

I wrote the code for a View which needs to call a user defined function which returns a table to join with it. The problem here is passing the parameter that this functions needs straight out of my view.

Below is the code of my view:

select
    GG.Gid,
    GG.StockType StockType,
    COALESCE(STC.Contract, 0) ContractId,
    COALESCE(C.[$Refex], null) ContractRefex,
    ST.[$Refex] StockTypeRefex
from 
(
    select
        G.Gid,
        coalesce(max(G.GEStockType), max(G.EAStockType)) StockType--,
        --case when coalesce(G.GEStockType, G.EAStockType) is null then null else coalesce(G.GEStartDate, G.EAStartDate) end StartDate
    from
    (
        select
            G.Gid, SI.StockType EAStockType, SI.[Date] EAStartDate, null GEStockType, null GEStartDate
        from Goods G
        inner join SiteIn SI on G.SiteIn=SI.[$Id]

        union

        select G.Gid, null EAStockType, null EAStartDate, GE.StockType, GE.EventOn
        from 
        (
            Select
                GE.Gid,  max(GE.EventOn) GEStartDate
            from GoodsEvent GE
            where GE.IsDeleted=0 and GE.[Type]='ST' and GE.EventOn < GETDATE()
            group by Gid 
        ) G
        inner join GoodsEvent GE on GE.Gid=G.Gid
            and G.GEStartDate=GE.EventOn
            and GE.[Type]='ST'
    ) G
    group by G.Gid
) GG
left outer join StockType ST on ST.[$Id]=GG.StockType
inner join (SELECT * FROM [dbo].StockTypeContractGetClosestStartDate(ST.[$Id]))
 STC on  GG.StockType = STC.[Parent]
 inner join Contract C On STC.Contract = C.[$Id]

And this is the code of my function:

CREATE FUNCTION StockTypeContractGetClosestStartDate
(
    @ParentId int
)
RETURNS  @StockTypeContract TABLE 
(
    [StartDate] [DateTime] null,
    [Parent] [int] not null,
    [Contract] [int] null
)
AS
BEGIN

 INSERT @StockTypeContract 
    SELECT TOP 1 STC.StartDate , STC.[$ParentId] , STC.Contract
     from StockTypeContract STC
        where STC.[$ParentId] = @ParentId AND STC.StartDate <= GETDATE() 
        order by STC.StartDate desc

    RETURN
END

It gives me an error when trying to pass ST.[$Id] to my function, the error is “The multi-part identifier ST.$Id could not be bound”.

Is there any work-around for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:46:00+00:00Added an answer on May 16, 2026 at 5:46 am

    You actually needs CROSS or OUTER APPLY. And from SO too

    ....
    left outer join StockType ST on ST.[$Id]=GG.StockType
    CROSS APPLY
    [dbo].StockTypeContractGetClosestStartDate(ST.[$Id])
    ...
    

    (I’ve simplified parenthesis here BTW, probably wrongly)

    Your problem is “get a resultset from StockTypeContractGetClosestStartDate per ST.[$Id]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote code to get name of printer which is installed in my system.but
i added sharekitkit my project. And wrote code like here : SHKItem *item =
I have a grid view which contains some sequence numbers. Each column needs to
I got error when I write this code @Html.X().ResourceManager() in view error message :
I wrote code first without using functions to prototype, and of course, it worked
I recently wrote code that didnt work as i would expect, it was: message
I wrote some code in VHDL that is expected to look at a rotory
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote some code that activity starts a service to get some text from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.