1

Ok so I have a large query that has 4 parameters in it..

CREATE FUNCTION [dbo].[Doorsdoorsdoors]
(
@start datetime, 
@end datetime, 
@day varchar,
@doornumber int
)
RETURNS TABLE AS RETURN (

SELECT bla bla bla....

What I want to do is set their default parameters to all (*)

So when I use the select statement I can use default so it returns all results like below:

SELECT * FROM Doorsdoorsdoors (default,default,default,default)

I would imagine it is something like this:

CREATE FUNCTION [dbo].[Doorsdoorsdoors]
(
@start datetime LIKE '%', 
@end datetime LIKE '%', 
@day varchar LIKE '%',
@doornumber int LIKE '%'
)
RETURNS TABLE AS RETURN (

SELECT bla bla bla....

Or possibly done in the SELECT * FROM Doorsdoorsdoors (default,default,default,default) with something like this..

SELECT * FROM Doorsdoorsdoors ('%','%','%','%')

7
  • 1
    You mean you want to retrieve without filtering on any of the 4 at all? Commented Mar 19, 2014 at 9:15
  • Yes so i want the default value in the function to be all(*) so when i create that function and then call it using a select statement i can use defaults to return all the values Commented Mar 19, 2014 at 9:20
  • 1
    What does your query look like. That is actually important. Commented Mar 19, 2014 at 9:31
  • 1
    Yes, but the actual where clausewill be important, as you might want to look at passing NULLs as default Commented Mar 19, 2014 at 9:34
  • 1
    Have a look at this SQL Fiddle Is this what you have in mind? Commented Mar 19, 2014 at 9:38

2 Answers 2

1
CREATE FUNCTION [dbo].[Doorsdoorsdoors]
    (
    @start datetime = NULL --default value, 
    @end datetime = NULL --default value, 
    @day varchar =NULL --default value,
    @doornumber int = NULL --default value
    )
    RETURNS TABLE AS RETURN (

select * from Doorsdoorsdoors where ((@start is NULL) OR (start=@start))
AND ((@end is NULL) OR (start=@end)) AND....
)

Call procedure with all defaults:

SELECT * FROM Doorsdoorsdoors (default,default,default,default)
Sign up to request clarification or add additional context in comments.

8 Comments

I dont want the function parameters to have values, i want the default values within the parameter to be all (*) then when i use call the procedure with all defaults it returns all the values.
get error Must declare the scalar variable "@start". when i run the select part
Why you run select part if it's part of the function?
Did you fill WHERE with all of parameters? Or you just copy paste without even reading?
There is no END statement in my sample of query. Error is on your side. Find place where you use END
|
0

Try the following

CREATE FUNCTION [dbo].[Doorsdoorsdoors]
      (
      @start datetime = NULL,
      @end datetime = NULL, 
      @day varchar = NULL,
      @doornumber int = NULL
      )
      RETURNS TABLE AS RETURN (

      SELECT bla bla bla....

      WHERE ((@start is NULL) OR (default=@start))
      AND ((@end is NULL) OR (default=@end))
      AND ((@day is NULL) OR (default=@day))
      AND ((@doornumber is NULL) OR (default=@doornumber))

Then try calling it with

      SELECT * FROM Doorsdoorsdoors (default,default,default,default)

or

      SELECT * FROM Doorsdoorsdoors (default,default,default,'123')

1 Comment

i get the error **Incorrect syntax near the keyword 'default'.** when running this.

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.