I want to set search criteria from outside for subqueries that are being viewed in SQL SERVER

Asked 2 years ago, Updated 2 years ago, 102 views

I wanted to manage the history of the data, so I tried to create the following view in SQL SERVER.
I don't know how to set conditions externally for columns in subqueries
If you don't give the conditional expression within the subquery, you won't get the correct search results, so I want to do this

If I set it to stored, I can set the variable, but I would like to use this result by joining other searches, so I would like to make it into a view if possible, but is it impossible?

SELECT COL1, COL2, DATE_FROM
FROM MST_FOOINNER JOIN
   (SELECT MAX(DATE_FROM) AS DATE_FROM
         FROM MST_FOO
         WHERE DATE_FROM<='2020/1/1' -- Can't you specify ★ view?
ASDATE_MAXON 
       MST_FOO.DATE_FROM =DATE_MAX.DATE_FROM

sql sql-server

2022-09-30 11:12

1 Answers

You cannot specify conditions for a view.

You can set variables when stored, but I want to use this result by JOINing it in another search

For this purpose, how about a table value function in user-defined function?In this sample

SELECT* FROM Sales.ufn_SalesByStore (602);

As you can see, the table value function can be written in the FROM clause and the result can be treated as a table, so JOIN and so on.


2022-09-30 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.