简体   繁体   中英

Conversion Failure In Stored Procedure But Not In SSMS

I am working on a script in a stored procedure to insert a new line that includes a getdate() value, and then select the line that has that same getdate() value. In practice, my classic ASP page will call one stored procedure after the other (the first being an insert, the next, the select).

When I construct these two queries in SQL Server Mgmt Studio, they work fine; however, once I put them into stored procedures, the select stored procedure throws an error

Conversion failed when converting date and/or time from character string

I'm not sure where the "character string" is, though, unless it is a reference to the dynamic SQL string.

Thanks for any leads. I've posted code below.

Here is the code I use in Mgmt Studio query, which works:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[001_test]
(
    [ID] [INT] IDENTITY(1,1) NOT NULL,
    [name] [NVARCHAR](50) NULL,
    [dateadded] [DATETIME] NULL
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[001_test] ON 

INSERT INTO [dbo].[001_test] ([ID], [name], [dateadded]) 
VALUES (1, N'toot', NULL), (2, N'hickory', NULL),
       (3, N'orange', NULL)

SET IDENTITY_INSERT [dbo].[001_test] OFF

INSERT INTO [dbo].[001_test] ([name], [dateadded]) 
VALUES ('boy', GETDATE())

SELECT
    [ID], [name], [dateadded]
FROM 
    [dbo].[001_test]
WHERE
    dateadded = GETDATE() 
GO

Here is the insert stored procedure, which does not throw the error. It performs the insert successfully:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[001_test_insert]
    @Name VARCHAR(50)
AS
    DECLARE @sql NVARCHAR(4000)

    SELECT @sql = ' INSERT INTO [001_test] ([name], [dateadded])' +
                  ' VALUES (@Name, GETDATE());'

    EXEC sp_executesql @sql, N'@Name VARCHAR(50)', @Name

Here is the stored procedure that is throwing the error (in practice, this stored procedure would be called from a classic ASP page, just after the insert stored procedure). Would the GETDATE() need to be cast for the datetime datatype?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[001_test_select]
AS
    SET NOCOUNT OFF
    DECLARE @sql NVARCHAR(4000)

    SELECT @sql = ' SELECT ID, Name, dateadded ' +
                  ' FROM [001_test]' +
                  ' WHERE dateadded = ' + GETDATE() + ';'

    EXEC sp_executesql @sql

Because you are using dynamic sql, and you can't concat the string with the datetime returned by getdate(). You can define a parameter in your dynamic sql:

DECLARE @current_dt datetime = getdate();
SELECT @sql = ' SELECT ' +
              ' ID ' +
              ' ,Name ' +
              ' ,dateadded ' +
              ' FROM [001_test]' +
              ' WHERE ' +
              ' dateadded = @current_dt';
EXEC sp_executesql @sql,  '@current_dt datetime', @current_dt;

You don't need dynamic sql here at all. I would simplify this procedure and remove the complication when it isn't needed. Something like this.

CREATE PROCEDURE [dbo].[001_test_insert]
(
    @Name varchar(50)
) AS

    set nocount on;

    INSERT INTO  [001_test]
    (
        [name]
        ,[dateadded] 
    )
    VALUES
    (
        @Name
        , getdate()
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM