sql 建立作業 日期轉換 約束 游標

2021-05-22 00:04:11 字數 4582 閱讀 3027

建立作業:

declare @jobid uniqueidentifier, @jobname sysname

set @jobname = n'作業名稱'

if exists(select * from msdb.dbo.sysjobs where name=@jobname)

exec msdb.dbo.sp_delete_job @job_name=@jobname

exec msdb.dbo.sp_add_job

@job_name = @jobname,

@job_id = @jobid output

--定義作業步驟

declare @sql nvarchar(4000),@dbname sysname

select @dbname=db_name(),  --作業步驟在當前資料庫中執行

@sql=n'--作業步驟內容'  --一般定義的是使用tsql處理的作業,這裡定義要執行的transact-sql語句

exec msdb.dbo.sp_add_jobstep

@job_id = @jobid,

@step_name = n'作業步驟名稱',

@subsystem = 'tsql', --步驟的型別,一般為tsql

@database_name=@dbname,

@command = @sql

--建立排程(使用後面專門定義的幾種作業排程模板)

exec msdb..sp_add_jobschedule

@job_id = @jobid,

@name = n'排程名稱',

@freq_type=4,                --每天

@freq_interval=1,            --指定每多少天發生一次,這裡是1天.

@freq_subday_type=0x8,       --重複方式,0x1=在指定的時間,0x4=多少分鐘,0x8=多少小時執行一次

@freq_subday_interval=1,     --重複週期數,這裡每小時執行一次

@active_start_date = null,   --作業執行的開始日期,為null時表示當前日期,格式為yyyymmdd

@active_end_date = 99991231, --作業執行的停止日期,預設為99991231,格式為yyyymmdd

@active_start_time = 00000,  --作業執行的開始時間,格式為hhmmss

@active_end_time = 235959    --作業執行的停止時間,格式為hhmmss

--定義目標伺服器

declare @server_name sysname

select @server_name=@@servername --本地伺服器名

exec   msdb.dbo.sp_add_jobserver

@job_name=@jobname

go日期轉換:

--///日期轉換//

select convert(varchar(10),getdate(),120)

2004-09-12

select convert(varchar, getdate(), 120 )

2004-09-12 11:06:08

select replace(replace(replace(convert(varchar, getdate(), 120 ),'-',''),' ',''),':','')

20040912110608

select convert(varchar(12) , getdate(), 111 )

2004/09/12

select convert(varchar(12) , getdate(), 112 )

20040912

select convert(varchar(12) , getdate(), 102 )

2004.09.12

其它我不常用的日期格式轉換方法:

select convert(varchar(12) , getdate(), 101 )

09/12/2004

select convert(varchar(12) , getdate(), 103 )

12/09/2004

select convert(varchar(12) , getdate(), 104 )

12.09.2004

select convert(varchar(12) , getdate(), 105 )

12-09-2004

select convert(varchar(12) , getdate(), 106 )

12 09 2004

select convert(varchar(12) , getdate(), 107 )

09 12, 2004

select convert(varchar(12) , getdate(), 108 )

11:06:08

select convert(varchar(12) , getdate(), 109 )

09 12 2004 1

select convert(varchar(12) , getdate(), 110 )

09-12-2004

select convert(varchar(12) , getdate(), 113 )

12 09 2004 1

select convert(varchar(12) , getdate(), 114 )

11:06:08.177

新增約束:

--------新增主鍵約束(bookid作為主鍵)

alter table bookmessage

add constraint pk_bookid primary key(bookid)

--------新增唯一約束

alter table bookmessage

add constraint uq_bookid unique(bookid)

---------新增預設約束

alter table bookmessage

add constraint df_address defauit('位址不詳') for address

--------新增檢出約束,要求年齡只能在15---40之間

alter table readermessage

add constraint ck_age check(age between 15 and 40)

-------新增外來鍵約束

alter table bookmessage

add constraint fk_bookid

foreign key(bookid)《外來鍵》 references readermessage《表》(readerid)《表中的主鍵》  

-------刪除約束

alter table 表名

drop constraint 約束名

游標應用:

declare @flow_instance_id decimal,@flow_process_id decimal

declare @tab_temp table(flow_instance_id decimal)

declare cur_test cursor for

select distinct flow_instance_id

from flow_process

where base_flow_node_id=109 and base_flow_status_id=7 and created_date > '2008-12-1'

open cur_test

fetch from cur_test into @flow_instance_id

while @@fetch_status = 0

begin

set @flow_process_id=(

select top 1 flow_process_id from flow_process where flow_instance_id=@flow_instance_id and base_flow_node_id=109 order by flow_process_id desc

)if not exists(select 1 from flow_process where flow_instance_id=@flow_instance_id and (base_flow_status_id=1 or base_flow_status_id=-1) and flow_process_id>@flow_process_id)

insert into @tab_temp values(@flow_instance_id)

fetch next from cur_test into @flow_instance_id

endclose cur_test

deallocate cur_test

select * from flow_process where flow_instance_id in (select * from @tab_temp) order by flow_instance_id,flow_process_id

SQL日期轉換

select convert varchar,getdate 120 2004 09 12 11 06 08 select replace replace replace convert varchar,getdate 120 20040912110608 select convert varcha...

SQL日期轉換

sql語句日期用法及函式 day month year 返回指定日期的天數 月數 年數 select day cl s time as 日 from class 返回天 select 月 month cl s time from class 返回月 select 年 year cl s time f...

sql建立作業

定義建立作業 declare jobid uniqueidentifier exec msdb.dbo.sp add job job name n 有獎問答使用者統計 job id jobid output 定義作業步驟 declare sql nvarchar 400 dbname sysname...