Solved

Executing a Stored Procedure with a DATE

Posted on 2006-11-14
3
358 Views
Last Modified: 2008-02-26
Hi,

I've got a stored procedure:

CREATE PROCEDURE TestTable.spTest
      @StartDateGreaterThan SMALLDATETIME,
      @StartDateLessThan SMALLDATETIME
AS
   SELECT * FROM TestTable
   WHERE StartDate > @StartDateGreaterThan AND StartDate < @StartDateLessThan

When I try to execute it, via:

EXECUTE TestTable.spTest 1-1-1900, 1-1-2100

I get:

Incorrect syntax near '-'.

Here's what else I've tried:

EXECUTE TestTable.spTest '1-1-1900', '1-1-2100'
-> Error converting data type varchar to smalldatetime.
EXECUTE TestTable.spTest #1-1-1900#, #1-1-2100#
-> Incorrect syntax near '-'.
EXECUTE TestTable.spTest '1/1/1900', '1/1/2100'
-> Error converting data type varchar to smalldatetime.
EXECUTE TestTable.spTest 1/1/1900, 1/1/2100
-> Incorrect syntax near '/'.


What is the correct way to call this stored procedure?
0
Comment
Question by:CMES-IT
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17941108
EXECUTE TestTable.spTest '1900-01-01', '2100-01-01'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17941114
or, the professional way:

DECLARE @s datetime
DECLARE @e datetime
SET @s = CONVERT(datetime, '1900-01-01', 120)
SET @e = CONVERT(datetime, '2100-01-01', 120)

EXECUTE TestTable.spTest @s, @e
0
 

Author Comment

by:CMES-IT
ID: 17941165
Hi Angel,

Thanks,

This:
EXECUTE TestTable.spTest '1900-01-01', '2100-01-01'
Produces: Error converting data type varchar to smalldatetime.

And This:

DECLARE @s datetime
DECLARE @e datetime
SET @s = CONVERT(datetime, '1900-01-01', 120)
SET @e = CONVERT(datetime, '2100-01-01', 120)

EXECUTE TestTable.spTest @s, @e

Produces:
Error converting data type varchar to smalldatetime.

However, that second one got me on the right track.

The correct syntax is:

DECLARE @s smalldatetime
DECLARE @e smalldatetime
SET @s = CONVERT(smalldatetime, '1950-01-01', 120)
SET @e = CONVERT(smalldatetime, '2050-01-01', 120)

EXECUTE Accounting.spAccountCodeTest @s, @e

That works!

Thanks for getting me on the right track ;)

0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clear out MSDB file in sql server 2 30
how would you excute a sproc on another database on the fly 9 28
Import export tables 5 21
Rebooting Witness SQL Server 2 23
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question