SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Replacing fixed data in a WHERE clause with dynamic data


Replacing fixed data in a WHERE clause with dynamic data

Author
Message
michael.newman560
michael.newman560
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
Hi,

I am new to SQL 2005 and have a question about Stored Procedures and have not been able to find a suitable answer to my question after trawling Google and Bing for days. I hope you can help.

I have a Visual Basic form and present my data in a DataGridView. My data table consists of 4 columns, ID, FirstName, LastName and DateofBirth. I can show data for a specific date of birth range using the following stored procedure:

ALTER PROCEDURE dbo.DateRange


SELECT FirstName, LastName, DateofBirth

FROM Table1

WHERE DateofBirth BETWEEN '1-january-1952' AND '31-december-2012'
ORDER BY DateofBirth
/* SET NOCOUNT ON */
RETURN




How do I use two datetimepickers on the VB form to replace the fixed dates in the WHERE clause in the code above?

Your help please.

Mike
WolfgangE
WolfgangE
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1311 Visits: 798
hi Mike,

you try to do it the wrong way. You should not try to modify your SQL-Code from outside. You can define input parameters in SQL-Procedures too.

ALTER PROCEDURE dbo.DateRange
@inFromDate datetime,
@inToDate datetime
as
set nocount on;

SELECT FirstName, LastName, DateofBirth
FROM Table1
WHERE DateofBirth BETWEEN @inFromDate AND @inToDate
ORDER BY DateofBirth

RETURN



Know you just have to find out how to pass over the date values from Visual Basic to the procedure. As this is nothing unusual you should find your way trough.
michael.newman560
michael.newman560
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
Thanks for the prompt response.

I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?

I have seen that method on the web but do not know how to get the dates from the two datetimepickers into those variables. This what I have been search for without success.

What I want to do is select the start and end dates using the 2 datetimepickers and then use a "Button_Click sub" to start the search. I am missing the code that is executed upon that button_click.

Can you help here please?
WolfgangE
WolfgangE
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1311 Visits: 798
michael.newman560 (1/5/2013)

I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?


Yes, these are variables for the where clause. I don't know how to call a procedure using VB but maybe you find the anser here: http://support.microsoft.com/kb/146651/en-us
michael.newman560
michael.newman560
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
Thanks. I will have a look at that.


Mike
michael.newman560
michael.newman560
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
Hi Wolfgang,

I took a look at the link you gave me but that applied to VB version 4 so I guess I will have to keep looking.


Mike
GilaMonster
GilaMonster
SSC Guru
SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)SSC Guru (229K reputation)

Group: General Forum Members
Points: 229160 Visits: 46344
michael.newman560 (1/5/2013)
I assume that the two statements directly below the procedure name, "@inFromDate" and "@inToDate" are just two variables for the WHERE clause to use. Where do they get their values from?


They're passed as parameters from the application. You're looking for the ADO.net parameters collection.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


OTF
OTF
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1278 Visits: 4128
In some ways, your question is outside the scope of the Sql Forums here and you will be better off posing
the question "How can I call a sql stored procedure with parameters from VB" in a VB Forum.
However, ADO, as has been suggested previously is the technology you seek.
A google search could also provide you with pointers to a few code snippets that will help.
michael.newman560
michael.newman560
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 22
Thanks but that is what I did before coming to this forum. I will go back there and keep trying.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search