0

I'm new here so go easy on me. How do I pass a column value as a function parameter?

I have this query to select months and years:

 SELECT 
     DATEPART(MONTH, a.RequestDate), 
     DATEPART(YEAR, a.RequestDate) 
 FROM 
     TABLE A

Sample data:

Months  Years 
------  -----
  5     2013
  3     2013
  1     2013
  9     2013

The values are not in order and could be duplicates.

I want to pass these value one by one to a function parameter automatically:

 SELECT WeekNo 
 FROM   _GLOBALDB.dbo.fn_WeekNoListInMonth(a.Months, a.Years)

The result would show what weeks would be in that particular month. For example:

 SELECT MIN(WeekNo) 
 FROM   fn_WeekNoListInMonth(5, 2013)

Expected result:

 WeekNO
 ------
   18
   19
   20
   21

I've tried this:

 SELECT MIN(WeekNo) 
 FROM fn_WeekNoListInMonth(A.Months, A.Years)

But it shows an error:

The multi-part identifier "A.Months" could not be bound.
The multi-part identifier "t.Months" could not be bound.

11
  • So, you want to get all the output of fn_WeekNoListInMonth() for each row in A? So, if there were 10 rows in A, you'd get between 40 and 50 rows back? Or do you need to get the lowest value from the output of the function, for each row in 'A'? Commented Dec 28, 2018 at 2:44
  • @Ann L. All the output . if i want to get the lowest or the highest , i'll just add 'Min' for the lowest and 'max' for the highest , am i right ? Commented Dec 28, 2018 at 2:52
  • Pity you're on SQL 2005 -- this became a LOT easier with SQL 2008. I'm trying to remember what we used to do before that. Commented Dec 28, 2018 at 2:55
  • (Why was MIN in the question, if you weren't trying to get the top or bottom value?) Commented Dec 28, 2018 at 2:56
  • I get the impression that the result of this isn't going to be the final result. What is the information you're hoping to wind up with? Perhaps we can avoid doing this (as I remember it) unreasonably difficult thing. Commented Dec 28, 2018 at 2:59

2 Answers 2

3

You should be able to use cross apply in SQL Server 2005:

SELECT . . .
FROM TABLE A CROSS APPLY
     dbo.fn_WeekNoListInMonth(month(a.RequestDate), year(a.RequestDate))
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, you're right. I remembered CROSS APPLY as coming in 2008 but I think that's because the employer I was working for skipped from SQL Server 2000 to SQL Server 2008.
the output is not what i want , it output random numbers and it output 2 column where as the fn_weekListInMonth function only output 1 column
@jonathan . . . This addresses what your question seems to be: "I want to pass these value one by one to a function parameter automatically:". If the rest of the query doesn't do what you want, then ask another question.
0
;with cte as ( 
SELECT DATEPART(MONTH, RequestDate) as Months, DATEPART(YEAR, RequestDate) as Years
 FROM TABLE 
 )
SELECT WeekNO  FROM cte A 
CROSS APPLY dbo.fn_WeekNoListInMonth(month(A.Months, A.Years)

1 Comment

While this might answer the authors' question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.