Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Passing variables into sql statements

Author  Topic 

Spangle1187
Starting Member

14 Posts

Posted - 2010-11-24 : 04:21:06
I am trying to pass a variable into an sql statement am I going about this in the right fashion?

I have declared a searchDate variable

Dim searchDate As Date ' a date varable to combine with sql and be used for seacrh


I then assign todays date to the variable
searchDate = DateTime.Now.ToString("dd,MMMM,yyyy") 'assigns the current date to search date upon page_load


And now I want to pass the variable into the sql statement

SELECT Booking.BookingDate, Room.RoomName, Booking.ClientName, Booking.StartTime, Booking.EndTime, Booking.Subject FROM (Booking INNER JOIN Room ON Booking.RoomID = Room.RoomID)


Now this is where I run into errors, I want to add to the statement
WHERE Booking.BookingDate = the dateSearch variable

I have tried:

WHERE Booking.BookingDate = searchDate

This returned the following error:
No Value given for one or more required parameters

I am using VS2010 and sql server 2008

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-11-24 : 04:23:49
try this way:
declare @searchdate datetime
set@searchdate=getdate()
select * from table name where bookingdate=@searchdate

if iam not wrong
Go to Top of Page

Spangle1187
Starting Member

14 Posts

Posted - 2010-11-24 : 05:28:55
I have tried to assign the variable like you said but it returns a syntax error

Public Class _Default
Inherits System.Web.UI.Page

Dim searchDate As Date ' a date varable to combine with sql and be used for seacrh
set searchDate = getDate()


I am trying this in the vb code
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-26 : 03:18:13
quote:
Originally posted by Spangle1187

I have tried to assign the variable like you said but it returns a syntax error

Public Class _Default
Inherits System.Web.UI.Page

Dim searchDate As Date ' a date varable to combine with sql and be used for seacrh
set searchDate = getDate()


I am trying this in the vb code


Post the full VB code that you are using

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-27 : 01:48:00
getdate() is not a VB function. In VB you need to use Now() instead

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -