C# 如何使用存储过程将当前日期插入表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15680202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:33:44  来源:igfitidea点击:

How to insert current date into table using stored procedure?

c#asp.netsql-server-2008

提问by Shaikh Nadeem

I am having a table with name medium and there is a column name medium name and creation date i have created stored procedure to insert specific above two values .below is my stored procedure

我有一个名称为 medium 的表,并且有一个列名称 medium name 和创建日期我创建了存储过程以插入特定的上述两个值。下面是我的存储过程

alter procedure insertmediumproc
    @MediumName varchar(50) ,@CreationDate datetime
as 
begin
    insert into medium  (MediumName, CreationDate) values(@MediumName,getdate())
end

when i tried to insert values in table with command below:

当我尝试使用以下命令在表中插入值时:

exec insertmediumproc Nepali,getdate()

it is showing error below: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ')'.

它显示以下错误:消息 102,级别 15,状态 1,第 1 行 ')' 附近的语法不正确。

采纳答案by jpw

If you always want the current date as the value for the CreationDate column you could use a default on the table and modify your proc to just take @mediumnamn as parameter. Or you could modify it to this:

如果您总是希望当前日期作为 CreationDate 列的值,您可以在表上使用默认值并修改您的 proc 以仅将 @mediumnamn 作为参数。或者您可以将其修改为:

alter procedure insertmediumproc @MediumName varchar(50)
as begin
insert into medium (MediumName, CreationDate) values (@MediumName,getdate())
end

That way you don't have to send the date as a parameter.

这样您就不必将日期作为参数发送。

回答by juergen d

You forgot the quotes around Nepali

你忘记了周围的引号 Nepali

exec insertmediumproc 'Nepali', getdate()

And your insert statement should be

你的插入语句应该是

insert into medium (MediumName, CreationDate) 
values (@MediumName, @CreationDate)

And if you only need a datethen you can change the parameter type from datetimeto date

如果您只需要一个,date那么您可以将参数类型从 更改datetimedate

回答by Oded

You have a syntax error - you are not passing in a proper VARCHAR- you need to quote the value:

你有一个语法错误 - 你没有传入一个正确的VARCHAR- 你需要引用这个值:

exec insertmediumproc 'Nepali',getdate()