问题
I need to use the prompted input of a date range/any column to be used as a variable in the modified SQl feature of the Information Link. I have some complex queries which uses multiple conditions and i wont be able to keep them in the view and want to add those conditions in modified SQL using this variable.
This is a sample SQL where i need to use the variable/parameter instead of the dates given
SELECT xxxxx.yyyy
FROM xxxxx, xxxxx, xxxxx, xxxxx
WHERE xxxxx.yyyyy = xxxxx.yyyyy
AND ( (( xxxxx.yyyy >= (TO_DATE ('11/01/2015', 'MM/DD/YYYY'))
AND xxxxx.yyyy < (TO_DATE ('12/1/2015', 'MM/DD/YYYY'))
AND xxxxx.zzzzz >= (TO_DATE ('11/01/2015', 'MM/DD/YYYY'))
AND xxxxx.zzzzz < (TO_DATE ('12/1/2015', 'MM/DD/YYYY')) ))
OR (( xxxxx.zzzzz >= (TO_DATE ('11/01/2015', 'MM/DD/YYYY'))
AND xxxxx.zzzzz < (TO_DATE ('12/1/2015', 'MM/DD/YYYY'))
AND xxxxx.yyyy = (TO_DATE ('01/01/1753', 'MM/DD/YYYY')) ))
OR (( xxxxx.zzzzz >= (TO_DATE ('11/01/2015', 'MM/DD/YYYY'))
AND xxxxx.zzzzz < (TO_DATE ('12/1/2015', 'MM/DD/YYYY'))
AND xxxxx.yyyy > (TO_DATE ('10/15/2015', 'MM/DD/YYYY'))
AND xxxxx.yyyy < (TO_DATE ('12/1/2015', 'MM/DD/YYYY')) )) )
I need the above where conditions to be added into the Information link modified SQL with the parameter to be like this
WHERE xxxxx.yyyyy = xxxxx.yyyyy
AND ( (( xxxxx.yyyy >= @parameter1
AND xxxxx.yyyy < @parameter2
AND xxxxx.zzzzz >= @parameter1
AND xxxxx.zzzzz < @parameter2
Let me know if further clarification is required.
回答1:
This will only work for one condition. That is >= or <= to all of the dates in xxxxx.yyyy column. So, if you MUST use a view then you need to use prompts in the information designer. Here is how:
- Open your Information Link in Information Designer
- Leave your SQL alone. Set the date range to the largest range you'll need
- Under the Prompts section, Add a prompt for your xxxxx.yyyy column
- Set the Prompt Type to Range and check the Mandatory box
Now, add the Information Link to your analysis...
- When the prompt window opens, select the Use On-demand button at the bottom. Select OK/ Finish
- On the Tool Bar go to Edit > Data Table Properties and select your data table
- Select the Settings button under Type of data: within the General tab
- Highlight your xxxxx.yyyy column and click the Define Input button
- Select Values (fixed/properties/expression) from the drop down for Input for the selected parameter
- Select the Property radio button and click the Select button
- Click New and create a property control of type DATE which you will use in a text area
Again, this is not the ideal way. If you TRULY want to use multiple parameters in multiple conditions, we need to convert your view to a procedure or table-valued function. it's not hard.
EDIT
CREATE PROCEDURE dbo.myProcedure(@parameter1 datetime, @parameter2 datetime)
AS
SELECT xxxxx.yyyy
FROM xxxxx, xxxxx, xxxxx, xxxxx
WHERE xxxxx.yyyyy = xxxxx.yyyyy
AND xxxxx.yyyy >= @parameter1
AND xxxxx.yyyy < @parameter2
AND xxxxx.zzzzz >= @parameter1
AND xxxxx.zzzzz < @parameter2
Then, you can test the execution by entering this in a new SSMS window.
EXEC dbo.myProcedure '4/1/2016','7/1/2016' --or what ever dates you want to pass in.
来源:https://stackoverflow.com/questions/38225091/date-range-or-columns-values-entered-in-prompt-need-to-be-used-as-variables-in-s