Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,
I wrote sp using dynamic query as follows:

SQL
BEGIN
                        SELECT @SQL=N'SELECT Imei_Id,Device_status,Device_DateTime,Fuel,Analog_data,Serial_data FROM dbo.'+@tablename +
                        ' WHERE Device_DateTime BETWEEN ' +@fromDateTime+ 'AND ' +@toDateTime

                        EXEC sp_executesql @SQL
                    END

While executing,
SQL
USE [Fleet_Management]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[Usp_GetImeiTable]
        @imeiNumber = N'355255040985708',
        @fromDateTime = N'2014-03-21 14:28:27.000',
        @toDateTime = N'2014-03-21 14:28:36.000'

SELECT  'Return Value' = @return_value

GO


Incorrect syntax near'14'--> Error
Posted
Comments
Schatak 24-Mar-14 3:18am    
"Usp_GetImeiTable" is your SP?

1 solution

You need quotation marks around your dates.

SQL
BEGIN
    SELECT @SQL=N'SELECT Imei_Id,Device_status,Device_DateTime,Fuel,Analog_data,Serial_data FROM dbo.'+@tablename +
    ' WHERE Device_DateTime BETWEEN '' +@fromDateTime+ '' AND '' +@toDateTime + ''

    EXEC sp_executesql @SQL
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900