Replacing fixed data in a WHERE clause with dynamic data

  • 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

  • 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.

  • 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?

  • 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

  • Thanks. I will have a look at that.

    Mike

  • 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

  • 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
  • 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.

  • Thanks but that is what I did before coming to this forum. I will go back there and keep trying.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply