22,210
社区成员
发帖
与我相关
我的任务
分享
Create table tb(ID int identity(1,1),[name] varchar(50),addtime datetime)
Create Proc insert_tb
@name varchar(50),
@addtime datetime='9999-12-31'
as
if @addtime = '9999-12-31' set @addtime = getdate()
insert into tb values(@name,@addtime)
GO
--1、要在addtime字段中添加null
exec insert_tb 'a',null
--2、要在addtime字段中添加默认值
exec insert_tb 'b'
--3、要在addtime字段中添加用户自定义的值
exec insert_tb 'c','2007-08-09'
select * from tb
/*
1 a NULL
2 b 2009-05-15 11:57:11.203
3 c 2007-08-09 00:00:00.000
*/
Create Procedure Emp_out
@Bdate DateTime='2007-01-01',
@Edate DateTime ='2007-01-31' 我一般都是這樣的設置參數的預設值
As
.......
3、如果在调用存储过程时,传递的addtime参数为null,则在添加记录时,addtime字段的值为null。
4、如果在调用存储过程时,没有传递addtime参数,则在添加记录时,addtime字段值为默认值。
create table tb
(
id int identity(1,1),
name varchar(50),
addtime datetime not null default getdate()
)
go
create proc proc_tb(@addtime datetime)
as
if @addtime is not null
insert tb(name,addtime) select 'abc',@addtime
else
insert tb(name) select 'abc'
go
exec proc_tb null
exec proc_tb ''
exec proc_tb '2009-05-01'
select * from tb
drop table tb
drop proc proc_tb
/**
id name addtime
----------- -------------------------------------------------- ------------------------------------------------------
1 abc 2009-05-15 09:45:52.483
2 abc 1900-01-01 00:00:00.000
3 abc 2009-05-01 00:00:00.000
(所影响的行数为 3 行)
**/
--1
CREATE PROC PROC_NAME
@name VARCHAR(10),
@addtime DATETIME
AS
INSERT TABLE1 SELECT @NAME,ISNULL(@ADDTIME,GETDATE())
create proc sp_add
@name nvarchar(10),
@addtime datetime = '1900-01-01'
as
if(@addtime='1900-01-01')
insert tb(name)values(@name)
else
insert tb(name,addtime)values(@name,@addtime)
go
2,3描述重复