tags:

views:

258

answers:

5

I have a problem/question that I may have been staring at too long today.

I have a stored procedure that receives data from an web application. The data comes in as smalldate time format. I am trying to pass this information to a second stored procedure but the second one won't fire unless the data is in single quotes. Would it be better to cast this as varchar?

The SET @CompletedDate must be '2010-01-20 15:28:00" for obvious reasons. How do I pass this information to the second procedure?

DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90
SET @CompletedDate = 2010-01-20 15:28:00

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate

Here is the usp_Task_Completion SP

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_Task_Completion]
@TaskID int 
,@CompletedDate smalldatetime
AS
BEGIN

SET NOCOUNT ON;

--Mark Transaction as complete
UPDATE dbo.Task
SET Completed = 1
    ,CompletedDate =  @CompletedDate
WHERE TaskID = @TaskID
+2  A: 

no because a varchar will also have to be enclosed in single quotes, just put quotes around it

DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90
SET @CompletedDate = '2010-01-20 15:28:00'

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate
SQLMenace
That is what I was thinking too I just can't get it to pass the smalldatetime through.
jgardner04
what is the error?
SQLMenace
That is where I am having issues. It doesn't error I just won't run the usp_Task_Completion_SP which is a simple UPDATE SET command
jgardner04
show me the code in the proc
SQLMenace
what happens when you run the proc exactly as I have in the code snippet?
SQLMenace
I just added the usp_Task_Completion to the original question.
jgardner04
Does taskID 90 exist?
SQLMenace
The TaskID is actually also being passed from the application. But let that prompted a question to ask my developer... You may be on to something with this
jgardner04
@SQLMenace It looks like the application was not passing the @TaskID variable. In my testing I was just setting it to a real value. Looks like I have been chasing my tail on this one.
jgardner04
What I should have done was fired up SQL Profiler and watched the transaction to find this before I posted anything... Live and learn but it goes as a lesson. Use the tools you have. Thank you to everyone for responding.
jgardner04
A: 

yes, you have to enclose that small datetime 2010-01-20 15:28:00 into single quotes -> '2010-01-20 15:28:00'

Mladen Prajdic
I know that I am trying to figure out how I can pass the variable enclosed in single quotes like ''' + @CompletedDate + '''
jgardner04
A: 

You probably have a naming confict on @CompletedDate. Change the variable name in the declare, and then change your set command to use single quotes, like this:

SET @CompletedDateNewName = '2010-01-20 15:28:00'

@CompletedDate can still be a smalldatetime type

Ray
that is not a problem..see herecreate proc prTest @id int asselect @id as idgodeclare @id intset @id = 5exec prTest @id = @idgo
SQLMenace
Yeah I have confirmed that the naming convention is not an issue.
jgardner04
ok - that is handy to know
Ray
A: 

This is a little much for a comment. What's the problem? Below are my tests:

/*
create proc usp_Task_Completion (
    @TaskID int,
    @CompletedDate smalldatetime)
as 

    return 1000
*/
go


DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90

/* This works */
--SET @CompletedDate = '2010-01-20 15:28:00'    

/* This doesn't work (incorrect syntax near '15'. */
--SET @CompletedDate = 2010-01-20 15:28:00      

/* This runs but yields unintended results (2010-01-20 = 1989; convert to datetime = '1905-06-13' */
SET @CompletedDate = 2010-01-20 

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate

select @return_value
Austin Salonen
the one that works doesn't because you get integer arithmeticrun thisdeclare @CompletedDate smalldatetimeSET @CompletedDate = 2010-01-20 select @CompletedDate-- output 1905-06-13 00:00:00
SQLMenace
@SQLMenace -- true. I will amend.
Austin Salonen
A: 

I'm guessing the CompletedDate column isn't smalldatetime, and it's trying to cast every value in that column into the smalldatetime type (with some failing). Please check the column type to be really sure.

Rob Farley
Nope we are good there. It looks like I have been racking my brain for nothing and was a bug that the application was not passing the @TaskID variable.
jgardner04
Incidentally - just because the web app is giving you a smalldatetime type, doesn't mean the database wants that. If the column is actually datetime, or maybe even char(19), you should cast your variable before comparing it.
Rob Farley
you are correct that the app and database need to sync up but it looks like this issue is the @TaskID variable not being set by the application.
jgardner04