August 1, 2009 at 7:42 am
ALTER PROCEDURE TraceReport
@server varchar(6),@FromDate datetime,@ToDate datetime
AS
BEGIN
SET NOCOUNT ON;
EXEC('SELECT databasename,starttime,eventclass,LoginName,TargetUsername,Objectname
FROM ' + @server + '.TRACEDB.dbo.TraceTable where starttime between '+@FromDate+' and '+@ToDate+'' )
--
END
EXEC TraceReport 'obbeaver','2009-02-11','2009-04-19'
Error :
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '11'.
August 1, 2009 at 7:52 am
Is it including the "EXEC" line as a part of the stored procedure? Or are you highlighting and running the ALTER segment separately?
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
August 1, 2009 at 7:59 am
I am executing store proc and getting the error, its not included in the proc
August 1, 2009 at 8:23 am
If you replace the exec with a print statement in to print the full text of the dynamic SQL, what does it print?
Print 'SELECT databasename,starttime,eventclass,LoginName,TargetUsername,Objectname
FROM ' + @server + '.TRACEDB.dbo.TraceTable where starttime between '+@FromDate+' and '+@ToDate+''
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
August 1, 2009 at 8:43 am
I get error when executing the procedure not within the proc.
if i replace with print i get this error:
Msg 241, Level 16, State 1, Procedure ReportTrace, Line 19
Conversion failed when converting datetime from character string.
August 1, 2009 at 9:04 am
Then try explicitly casting your dates to string before concatenating them, use the CONVERT function.
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
August 1, 2009 at 12:42 pm
It's more than just that... you also have to quote encapsulate the dates...
SELECT QUOTENAME(GETDATE(),'''')
ALTER PROCEDURE TraceReport
@server varchar(6),@FromDate datetime,@ToDate datetime
AS
BEGIN
SET NOCOUNT ON;
EXEC('SELECT databasename,starttime,eventclass,LoginName,TargetUsername,Objectname
FROM ' + @server + '.TRACEDB.dbo.TraceTable where starttime between '+QUOTENAME(@FromDate,'''')+' and '+QUOTENAME(@FromDate,''''))
END
You also don't need the last set of quotes so I took those out.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 1, 2009 at 7:05 pm
Jeff
Incorrect syntax near 'QUOTENAME'.
August 2, 2009 at 10:49 am
Ah crud... sorry Tara... I forgot that EXEC will only take constants and variables as operands. It won't take functions. For example...
EXEC('SELECT ' + SQRT(4))
... fails just like QUOTENAME does. Instead, we have to build the dynamic SQL into a variable and then execute the variable. In your case, the sproc would need to look something like this...
ALTER PROCEDURE TraceReport
@server VARCHAR(6),
@FromDate DATETIME,
@ToDate DATETIME
AS
BEGIN
--===== Supress the autodisplay of rowcounts to prevent false error returns
SET NOCOUNT ON;
--===== Declare and set a dynamic SQL variable with the correct SQL construction
-- base on the spoc input parameters. We need to do this separately because
-- of the use of functions. It's also a good practice to support troubleshooting
-- because you can print out the SQL.
DECLARE @sql VARCHAR(8000)
SELECT @sql = 'SELECT databasename,starttime,eventclass,LoginName,TargetUsername,Objectname '
+ 'FROM ' + @server + '.TRACEDB.dbo.TraceTable where starttime between '
+ QUOTENAME(@FromDate,'''') + ' AND '+QUOTENAME(@FromDate,'''')
--===== Uncomment this line for troubleshooting
--PRINT @sql
--===== Execute the constructed dynamic SQL.
EXEC (@SQL)
END
--Jeff Moden
Change is inevitable... Change for the better is not.
August 2, 2009 at 10:52 am
Actually, this is the way I write code at work... it makes debugging much, much easier...
ALTER PROCEDURE TraceReport
@server VARCHAR(6),
@FromDate DATETIME,
@ToDate DATETIME,
@Debug BIT = 0
AS
BEGIN
--===== Supress the autodisplay of rowcounts to prevent false error returns
SET NOCOUNT ON;
--===== Declare and set a dynamic SQL variable with the correct SQL construction
-- base on the spoc input parameters. We need to do this separately because
-- of the use of functions. It's also a good practice to support troubleshooting
-- because you can print out the SQL.
DECLARE @sql VARCHAR(8000)
SELECT @sql = 'SELECT databasename,starttime,eventclass,LoginName,TargetUsername,Objectname '
+ 'FROM ' + @server + '.TRACEDB.dbo.TraceTable where starttime between '
+ QUOTENAME(@FromDate,'''') + ' AND '+QUOTENAME(@FromDate,'''')
--===== Print the SQL if Debug is "on"
IF @Debug 0 PRINT @sql
--===== Execute the constructed dynamic SQL.
EXEC (@SQL)
END
Ignore this next little window... I'm "tricking" the wrap function of this display to not wrap...
____________________________________________________________________________________________________________________
--Jeff Moden
Change is inevitable... Change for the better is not.
August 2, 2009 at 1:11 pm
THanks Jeff, QUOTENAME helped me.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply