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
 error message date conversion to string

Author  Topic 

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2012-09-07 : 18:43:23

have a sp1 calling a sp2. getting this error msg
Msg 241, Level 16, State 1, Procedure SetKPIReportFormat, Line 27
Conversion failed when converting date and/or time from character string.


How can i pass datetime to dynamic sql?




alter PROC Report.SetKPIReportFormat
@Procedure VARCHAR(100),
@StartDate AS DATETIME,
@EndDate AS DATETIME
AS

DECLARE @AdAttributes XML
DECLARE @SQL VARCHAR(2000)

SET @SQL = 'EXEC Report.' + @Procedure + ' ''' + CAST(@StartDate AS DATETIME) + ''', ''' + CAST(@EndDate AS DATETIME) + ''''

DECLARE @ReportResult TABLE (KPICount XML)
INSERT @ReportResult
EXEC (@SQL)

SELECT @AdAttributes = KPICount FROM @ReportResult


SELECT Attr.col.value('@Name', 'VARCHAR(30)') AS 'Name',

Attr.col.value('@Value', 'INT') AS 'Value'
FROM @AdAttributes.nodes('//KPICounts/KPICount') Attr(col)
GO

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-09-07 : 19:14:10
Cast it to varchar.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2012-09-07 : 19:48:10
THANKS tkizer, it works
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-09-07 : 19:56:02
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -