Showing posts with label parameter. Show all posts
Showing posts with label parameter. Show all posts

Wednesday, March 21, 2012

How to include parameter in WHERE statment of a stored procedure?

I have a stored procedure like:

PROCEDURE Procedure_ABC
@.Id VARCHAR(8000),
@.LocationId VARCHAR(8000)
AS
BEGIN
SELECT table.id
FROM table
WHERE table.id = @.Id
AND table.LocationId in (SELECT id FROM tempTable)

END

My problem is, the locationId will be null or length equals 0 some time, how can I make the statement "table.LocationId in (SELECT id FROM tempTable)" included into the WHERE condition dynamically depends on locationId is null or length equals 0?

Is that possible to do it at Database side? Or I have to do it at code side?

Thank you.

Perhaps something like this:

SELECT t.id

FROM Table t

WHERE ( t.id = @.Id
AND ( t.LocationID in (SELECT ID FROM TempTable)

OR t.LocationID IS NULL

)

)
END

|||

Looks like you want dynamic search capabilities based on the parameters passed. You can take a look at the link below for various options:

http://www.sommarskog.se/dyn-search.html

You can do below in your case:

SELECT table.id
FROM table
WHERE table.id = @.Id
AND table.LocationId in (SELECT id FROM tempTable) and @.LocationId is not null and len(@.LocationId) > 0

UNION ALL

SELECT table.id
FROM table
WHERE table.id = @.Id
AND table.LocationId in (SELECT id FROM tempTable) and (@.LocationId is null or len(@.LocationId) = 0)

Query optimizer will use startup expression filter to evaluate the expression containing @.LocationId at run-time. This will result in only one of the UNION ALL branches being executed. You could use this approach. You can also put this query in a inline TVF and use it.

|||

Thank you for your advise.

For your code, my understanding is you list both situations, either parameter has value or not has value, run both and combine the results. Is that correct?

My question is, if @.LocationId is not null, the condition "(@.LocationId is null or len(@.LocationId) = 0)" will be false, and it will AND with condition "table.LocationId in (SELECT id FROM tempTable)", the result will also be false right? Finally, it will AND with "table.id = @.Id", which will be false again. That will make the result set of

SELECT table.id
FROM table
WHERE table.id = @.Id
AND table.LocationId in (SELECT id FROM tempTable) and (@.LocationId is null or len(@.LocationId) = 0)

be null?

Cause I have about 8-9 parameters need to be passed in, follow your code, I guess the stored procedure will be very long, right?

The link you gave provides a coding way to do the if statement to build the query at database.

Thank you.

|||Try the following:

Code Snippet


PROCEDURE Procedure_ABC
@.Id VARCHAR(8000),
@.LocationId VARCHAR(8000),
@.Parameter2 INT,
@.Parameter3 NUMERIC(18, 2)
AS
SELECT
table.id
FROM
table
WHERE
table.id = @.Id AND
(table.LocationId in (SELECT id FROM tempTable) OR LEN(ISNULL(@.LocationId, '')) = 0) AND
(table.Value2 = @.Parameter2 OR @.Parameter2 IS NULL) AND
(table.Value3 = @.Parameter3 OR @.Parameter3 IS NULL) etc...


If you're checking VARCHARs, NVARCHARs, CHARs, to see if they're either NULL or have length = 0, put an ISNULL around the parameter and compare the length to 0.

For every other parameter you want to check, compare it to the table value and OR it with a check to see if it's null.

Each of those OR'd NULL checks needs to be in brackets with AND clauses otherwise you'll get strange results.

Friday, March 9, 2012

How To Import Access to SQLServer with Parameter from SQLServer, Help Pls!

Hello Expert!

I have 2 Database – Access & SQLServer(ver 7)

I need to Import Data TblShift from Access to SQLServer – using DTS I’ve done this successfully!

Now I want to use parameter so I only importing record within range (e.g. ShiftDate BETWEEN 05-24-2006 AND 06-23-2006)

In SQLServer, I have created table to store the date range as following:

TblParameter
DateFrom: 04/24/2006
DateTo: 05/23/2006

How do I use the date range from TblParameter(SQLServer) to import record from TblShift(Access) using DTS?

Is this possible or any better solution for this?

TIA

Regards,

have you tried using the ole db source component? If you use a parameterized query, you can map variables to parameters to specify the values for individual parameters in the SQL statements.|||

Hi Douglas,

I newbie to sqlserver dts,

I don't know how to do as u have suggested

Can u explain step by step?

Or can u guide me to the place where I can see some sample

TIA

Regards

|||

Try doing this:

http://blogs.conchango.com/jamiethomson/archive/2005/12/09/2480.aspx

-Jamie

Sunday, February 19, 2012

How to identify all parameter values selected

I have the SQL query. If the user is selecting all the vendor Numbers available in the vendor number parameter drop down then, I will not include the vendor Number condition in the where portion of the sql query. For that I want to know whether the user has selected all the values available in the drop down. How to identify this?

Have a Default Value in your DropDown, for instance: SELECT ALL with the value of "" VALUE=""

which is basically null. In your where cause use an ISNULL

For instance:

@.COMPANY_ID INT = NULL

Select ID, [NAME], COMPANY_ID FROM EMPLOYEES WHERE COMPANY_ID = ISNULL(@.COMPANY_ID, COMPANY_ID)

Hope this helps.

How to hide/show parameter dynamic?

There is a date range parameter in my rdl, the list value is "This Week","This Month", "This Year" and "Custom", when user choose "Custom", then display two parameter "Date From" and "Date To" to select custom date range.

My question, how to show "Date From" and "Date To" when user choose "Custom", and will hide these two parameter when user choose other item.

Thanks

You don't say what kind of element Date To and Date From are, but if they are text boxes with lables, you can change their Visible property from "False" to "True" or vice versa, depending on what the user has selected. I suggest that the default poroperty should be False., then make them Visisble if the user chooses "Custom"|||

Thanks for you reply

But the parameter's visible property cannot support formula, and I cannot get the change event of parameter "Date Range", such as selectedIndexChange

I use SRS2005, the "Date Range" is a dropdownlist, "Date From" and "Date To" may be the text box

How to hide/show parameter dynamic?

There is a date range parameter in my rdl, the list value is "This
Week","This Month", "This Year" and "Custom", when user choose "Custom", then
display two parameter "Date From" and "Date To" to select custom date range.
My question, how to show "Date From" and "Date To" when user choose
"Custom", and will hide these two parameters when user choose other item.
I use SRS2005
If cannot design the above function, I want to know, how to set the layout
of 3 parameters as:
Date Range ______
Date From ______ Date To ______
The SRS will display as:
Date Range ______ Date From ______
Date To ______
ThanksIf the parameters values are coming from query byway of datasets then it can
be cascaded so whn you select "custom" then it displayes the from and to
otherwise I suppose it is not possible. when you create 3 parameters then it
displays all 3.
Amarnath
"icyer" wrote:
> There is a date range parameter in my rdl, the list value is "This
> Week","This Month", "This Year" and "Custom", when user choose "Custom", then
> display two parameter "Date From" and "Date To" to select custom date range.
> My question, how to show "Date From" and "Date To" when user choose
> "Custom", and will hide these two parameters when user choose other item.
> I use SRS2005
> If cannot design the above function, I want to know, how to set the layout
> of 3 parameters as:
> Date Range ______
> Date From ______ Date To ______
> The SRS will display as:
> Date Range ______ Date From ______
> Date To ______
> Thanks
>|||Hi
I'd be curious as to your coding behind this. I need to do similar in
giving a list that has predefined dates to be passed except when the user
chooses their own dates.
Thanks
chelle
"icyer" wrote:
> There is a date range parameter in my rdl, the list value is "This
> Week","This Month", "This Year" and "Custom", when user choose "Custom", then
> display two parameter "Date From" and "Date To" to select custom date range.
> My question, how to show "Date From" and "Date To" when user choose
> "Custom", and will hide these two parameters when user choose other item.
> I use SRS2005
> If cannot design the above function, I want to know, how to set the layout
> of 3 parameters as:
> Date Range ______
> Date From ______ Date To ______
> The SRS will display as:
> Date Range ______ Date From ______
> Date To ______
> Thanks
>