-----------------------创建和使用函数-----------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[F_TEST2]‘)
and xtype in (N‘FN‘, N‘IF‘, N‘TF‘))
drop function [dbo].[F_TEST2]
GO
CREATE FUNCTION F_TEST2(@NUM INT)
RETURNS INT
AS
BEGIN
RETURN @NUM+2
END
--调用自定义函数
SELECT DBO.F_TEST2(4)--不加dbo报错
SELECT DBO.F_TEST2(4) AS NUM,* FROM SHANGPIN
----------------------------创建和使用存储过程-----------------------------------------
if exists (select 1 from dbo.sysobjects where id = object_id(N‘[dbo].[P_TEST]‘)
and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1)
drop procedure [dbo].[P_TEST]
GO
CREATE PROC P_TEST
@NUM INT
AS
BEGIN
SELECT @NUM
END
EXEC P_TEST 4
------------------日期转换为星期---------------------
SET DATEFIRST 1--星期的第一天为周一
select DATEPART(W,GETDATE())AS 星期
---------------------------两日期相隔所少天--------------------------------------
--SELECT DATEDIFF(DAY,开始日期,结束日期)
select datediff(day, BYZD14,BYZD15) from shangpin
SQLServer中的存储过程和函数的创建使用以及一些常用的日期函数