3

I have a simple table

EMPLOYEE

ID: int
Name: nchar(20)
Birthday: date

And I create a simple procedure to add new record to EMPLOYEE table

ADD_NEW_EMPLOYEE

CREATE PROCEDURE ADD_NEW_EMPLOYEE
    @id INT,
    @Name NCHAR(20),
    @Birthday DATE
AS
BEGIN
    INSERT INTO EMPLOYEE 
    VALUES(@id, @Name, @Birthday)
END

When I execute the procedure (right-click -> Execute Stored Procedure...), I get an error

Incorrect Syntax near '-'

on the value of date.

The code execution panel:

EXEC    @return_value = [dbo].[ADD_NEW_EMPLOYEE]
        @id = 3,
        @Name = N'James Bond',
        @Birthday = 1995-06-06

However, if I change the @Birthday type in ADD_NEW_EMPLOYEE to nchar type. The value is encoded as @Birthday = N'1995-06-06' and the procedure executes normally. But doing so make the declaration in the procedure not corresponding with the value type in the table.

Can you show me the way to fix this ?

The version I use is Microsoft SQL Server 2014

Please note that I execute the procedure with SQL Server (right-click on the procedure and choose "Execute Stored Procedure"), a table to fill value show up and i just have to fill in the data for variables.

#UPDATE:

So in the table to input value, I have to input '1995-06-06' with single quotes instead of 1995-06-06 without the quotes.

What if another program like C# execute this procedure and the caller function forget to enclose the date in quotes? Is this a bug in SQL Server 2014? I feel like a joke.

Syntax error on execute:

This will cause syntax error....

This will work, but too clumsy... (notice the quotes ' ' surround the date value)

enter image description here

If you put a value to nchar variable, on execution, the value will automatically enclosed in the quotes ' ' . For the date type, the value goes straight to execute without proper quotes enclosing. And that causes the problem.

4
  • 2
    Try this @Birthday = '1995-06-06' Commented Oct 25, 2016 at 7:54
  • Pretty sure you need to use quotation marks around it as it's a string that's interpreted as date. Commented Oct 25, 2016 at 7:55
  • Please note that I execute the procedure with SQL Server (right-click on the procedure and choose "Execute Store Procedure"), a table to fill value show up and i just fill in the data for the variable. Commented Oct 25, 2016 at 8:01
  • Every language have some protocols and you will have to follow them in order to communicate. Isn't joke if i forget to put semicolon at the end of c# line and compiler would not allow me to compile the code. Commented Oct 25, 2016 at 10:16

3 Answers 3

2

Missing single qoutes Wrap date into single quotes '1995-06-06'

@Birthday = '1995-06-06'
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, but SQL Server doesn't surround those in single quotes, just @Birthday = 1995-06-06 and throws the error. Is this a bug in SQL Server 2014 ?
@Flint Can't say about this since I am not aware of this issue but date should send as string value
So in the data input table, i have to write '1990-08-08' insteads of 1990-08-08
That would be so stupid and easy to get error if another program like Windows Form C# execute this query an forget to enclose the date in the single quotes. This is so clumsy.
@ThangLeQuoc SQL Server also doesn't display the quotes around text values, ie the James in your screenshot. Rule of thumb, only numerical values can be handled without quotes.
0

Missing the semicolon and single quotes date into insert query and single quotes '1995-06-06'

INSERT INTO EMPLOYEE VALUES(@id, @Name, @Birthday);

@Birthday = '1995-06-06'

Comments

0

@Birthday is not a INT data type.

for datatype other than INT,we need use single single quotation mark ''

 @Birthday = '1995-06-06'

1 Comment

Yeah, I know. But the things is, SQL Server doesn't automatically enclose date in quotes ' ' . Just like you put in the string to the nchar type, it becomes 'abcd' in the execution. But for the date, it doesn't. Date value goes straight to execute without quotes enclosing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.