I am trying to write a stored procedure that uses openquery and gets a date range fron the user.
The query works when I use:
Create Procedure (proc1)
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > '31 oct 01 12:00pm' and col2 < '31 oct 01 3:00pm'"
I want to be able to get date input from a user, so I tried this two ways:
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > '" + @startdate + "' and col2 < '" + @enddate + "'"
This did not pass Sql Server's Syntax check.
I also tried:
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > " + @startdate + " and col2 < " + @enddate)
This also did not pass.
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > ' + @startdate + ' and col2 < ' + @enddate + '"
This passed the syntax check, but would not execute. It told me there was incorrect syntax near ' + @enddate + '.
Is there any way of doing this? Please help!
The query works when I use:
Create Procedure (proc1)
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > '31 oct 01 12:00pm' and col2 < '31 oct 01 3:00pm'"
I want to be able to get date input from a user, so I tried this two ways:
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > '" + @startdate + "' and col2 < '" + @enddate + "'"
This did not pass Sql Server's Syntax check.
I also tried:
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > " + @startdate + " and col2 < " + @enddate)
This also did not pass.
Create Procedure (proc1)
@startdate datetime,
@enddate datetime
as
Select * from OpenQuery(Server,"select avg(col1),avg(col2),avg(col3) from tablename where col1 > 0 and col2 > ' + @startdate + ' and col2 < ' + @enddate + '"
This passed the syntax check, but would not execute. It told me there was incorrect syntax near ' + @enddate + '.
Is there any way of doing this? Please help!