如何自动每日执行查询SQL Server? [重复]

时间:2021-12-20 15:25:51

This question already has an answer here:

这个问题在这里已有答案:

I have some queries that I want to execute every day automatically. Should I create it as stored procedure or there are another ways? here is my queries:

我有一些想要自动每天执行的查询。我应该将其创建为存储过程还是有其他方法?这是我的疑问:

declare @tanggal_1 varchar(50)

set @tanggal_1 = (select top 1 ltrim(rtrim(cast(numweek as char)))+'-('+rangeweek+')' from tbl_weeklyflash_id where year([date]) = year(getdate()) order by numweek desc)

TRUNCATE table mytable
GO
INSERT INTO mytable (field1, field2, field3)
EXEC my_store_procedure @tanggal_1
GO
UPDATE mytable SET 
 [field4] = isnull(((nullif([cm1pl2],0)/nullif([cm1pl2_per],0))*100),0)
,[field5] = isnull(((nullif([cm1pl1],0)/nullif([cm1pl1_per],0))*100),0)
,[field6] = isnull(((nullif([cm1_cost_value],0)/nullif([cm1_cost_per],0))*100),0)
GO

3 个解决方案

#1


3  

Create and schedule a job in sqlserver

在sqlserver中创建和调度作业

I prefer to write SPs , but you can write queries directly.

我更喜欢编写SP,但您可以直接编写查询。

#2


1  

You can make an agent which will do the needful.
Reference : http://msdn.microsoft.com/en-us/library/ms191439.aspx

你可以做一个能满足需要的代理人。参考:http://msdn.microsoft.com/en-us/library/ms191439.aspx

#3


1  

One way is to write query and schedule job in sql server.

一种方法是在sql server中编写查询和调度作业。

Ohter way is to prepare windows schedule for that query. First you need to create Store procedure of that queries. After that go to Computer Managment(right click on MyComputer and select Manage). and Go to Task Scheduler and create new task. and for that you need to create batch file to execute the store procedure which you need to run daily.

更方便的是为该查询准备Windows计划。首先,您需要创建该查询的Store过程。之后转到Computer Managment(右键单击MyComputer并选择Manage)。并转到任务计划程序并创建新任务。为此你需要创建批处理文件来执行你需要每天运行的存储过程。

#1


3  

Create and schedule a job in sqlserver

在sqlserver中创建和调度作业

I prefer to write SPs , but you can write queries directly.

我更喜欢编写SP,但您可以直接编写查询。

#2


1  

You can make an agent which will do the needful.
Reference : http://msdn.microsoft.com/en-us/library/ms191439.aspx

你可以做一个能满足需要的代理人。参考:http://msdn.microsoft.com/en-us/library/ms191439.aspx

#3


1  

One way is to write query and schedule job in sql server.

一种方法是在sql server中编写查询和调度作业。

Ohter way is to prepare windows schedule for that query. First you need to create Store procedure of that queries. After that go to Computer Managment(right click on MyComputer and select Manage). and Go to Task Scheduler and create new task. and for that you need to create batch file to execute the store procedure which you need to run daily.

更方便的是为该查询准备Windows计划。首先,您需要创建该查询的Store过程。之后转到Computer Managment(右键单击MyComputer并选择Manage)。并转到任务计划程序并创建新任务。为此你需要创建批处理文件来执行你需要每天运行的存储过程。