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
How to insert current date into table using stored procedure?
提问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 date
then you can change the parameter type from datetime
to date
如果您只需要一个,date
那么您可以将参数类型从 更改datetime
为date
回答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()