|
-
Nov 8th, 2006, 12:47 PM
#1
Thread Starter
New Member
[RESOLVED] Null Value into SQL Para Query
Is there syntax to pass a null value into a MS SQL 2000 database query set up for a parameter value so that the query will disregard the parameter? I've encountered this situation with both ASP 2.0 dataset added to a web site and a VB.net data adapter filling a dataset. With a query using multiple filter criteria using the =@ParamName within the query I hope there is syntax to tell the query to disregard one or more filter criteria not used. Otherwise I'll need a large combination of queries to handle all possible combinations of filter items selected. If yes, I'd love to see a code snippet. Thanks.
-
Nov 8th, 2006, 02:09 PM
#2
Re: Null Value into SQL Para Query
I write the Where clause using something like
Code:
(@Param1 Is Null Or Field1 = @Param1) And
(@Param2 Is Null Or Field2 = @Param2) And
(@Param3 Is Null Or Field3 = @Param3)
-
Nov 8th, 2006, 04:19 PM
#3
Thread Starter
New Member
Re: Null Value into SQL Para Query
Here is a bit more info.
The SQL for the query looks like this:
SELECT int4FilingRecordNumber, int4DocketRecordId, int4DocketFilingNumber, dtm8WhenFiled, int1FilingTypeNumber
FROM tblFiledDocument
WHERE (int1FilingTypeNumber = @FilingType) AND (int4DocketRecordId = @DocketRecordId)
The vb code from the ASPX.VB partial class looks like this:
Dim dadapter As New tblFiledDocumentTableAdapter
DataGrid1.DataSource = dadapter.GetDataByFilingType(FilingType, DocketRecord)
DataGrid1.DataBind()
DataGrid1.Visible = True
This method of binding for the control works great as long as you supply an integer for the two parameter fields. The question is how to allow the parameter query to work if the user wants to filter only by one of the fields which means the other field essentially wants a * passed into a int field.
-
Nov 10th, 2006, 09:37 AM
#4
Re: Null Value into SQL Para Query
I found THIS and it seems to work me.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 13th, 2006, 08:53 AM
#5
Thread Starter
New Member
Re: Null Value into SQL Para Query
Thanks much for the response. The COALESCE is required at the SP to let the query disregard null inbound parameters and the variable must be dimensioned in code as nullable. Here is code snippet:
In SP properties:
WHERE int1FilingTypeNumber = COALESCE(@int1FilingTypeID, int1FilingTypeNumber)
AND int1DocketTypeID = COALESCE(@int1DocketTypeID, int1DocketTypeID)
In ASP.NET code: Dim FilingType As Nullable(Of Integer) = Nothing
If lstFilingType.SelectedValue.ToString <> Nothing Then
FilingType = lstFilingType.SelectedValue.ToString
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|