Question

An application I am working on is exhibiting some strange behaviour that I cannot explain when it saves data. The application uses a custom c# data layer. When a save is made, it inserts data into a table variable then executes a stored procedure to save the data, passing the table variable as a parameter. The save works when the application runs, but when I profile the sql that is running, I cannot execute the queries in SSMS because of a conversion failure to datetime. An example of the SQL recorded by profiler is below, I've added in the table variable as well for reference.

table variable...

CREATE TYPE [dbo].[ExampleType] AS TABLE(
    [Name] [nvarchar](200) NOT NULL DEFAULT (''),
    [CreationDate] [datetime] NULL,
    [UpdateDate] [datetime] NULL
)

sql from profiler...

declare @p1 dbo.ExampleType
insert into @p1 values(N'','2014-01-29 09:48:18.8000000','2014-01-29 10:05:31.9647267')

exec [dbo].[save_Example] @tableParameter=@p1

When the application is running, I can see the saved data in the table and the datetimes are truncated to the datetime format i.e. the last 4 characters are removed and the data is saved. Am I missing something in the profiler which allows the sql to run, or is there another explanation?

Was it helpful?

Solution

The answer given be Martin looks correct to me and explains the behaviour I'm seeing.

Link from his comment...

Why does SqlClient use unnecessary XML conversion while passing SqlXml?

OTHER TIPS

You have to insert it in a variable. E.g.

@CurrentDate = CONVERT(DATETIME, '2014-01-09 09:48:18')
INSERT INTO @p1 VALUES (.., @CurrentDate, ...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top