如何使用存储过程在 mssql 数据库中获取新记录
How to fetch new records in mssql database using stored procedures
我有一个 select 查询,我可以连接两个 table 来获取数据。我想每天 运行 查询和 return 当天插入的最新行。
这是我 using.I 对存储过程还很陌生的查询。任何人都可以帮助我如何使用它来获取新行。
select [dbo].[Archive1].id,[dbo].[Archive1].loadId,[dbo].[Archive2].costResponse
from [dbo].[Archive1],[dbo].[Archive2]
where [dbo].[Archive1].id = [dbo].[Archive2].transactionNumber;
我能够编写适用于我的 use-case
的程序
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'getReport')
DROP PROCEDURE getReport
GO
CREATE PROCEDURE getReport AS
BEGIN
DECLARE @nowLocal DATETIMEOFFSET = getDate() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'
DECLARE @midnightLocal DATETIMEOFFSET = CONVERT(DATETIME2, CAST(@nowLocal as DATE)) AT TIME ZONE 'Central Standard Time'
DECLARE @dayStart DATETIMEOFFSET = DATEADD(hh, -17, @midnightLocal)
DECLARE @reportTime DATETIMEOFFSET = DATEADD(hh, 07, @midnightLocal)
if @nowlocal < @reportTime
SELECT @dayStart = DATEADD(d, -1, @dayStart)
DECLARE @dayEnd DATETIMEOFFSET = DATEADD(ms, 86399999, @dayStart)
select @dayStart as dayStart, @dayEnd as dayEnd, @reportTime as reportTime, @midnightLocal as midnight
select [dbo].[Archive1].id,[dbo].[Archive1].loadId,[dbo].[Archive2].costResponse
from [dbo].[Archive1],[dbo].[Archive2]
where [dbo].[Archive1].id = [dbo].[Archive2].transactionNumber
BETWEEN @dayStart AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC' and @dayEnd AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC'
END
我有一个 select 查询,我可以连接两个 table 来获取数据。我想每天 运行 查询和 return 当天插入的最新行。
这是我 using.I 对存储过程还很陌生的查询。任何人都可以帮助我如何使用它来获取新行。
select [dbo].[Archive1].id,[dbo].[Archive1].loadId,[dbo].[Archive2].costResponse
from [dbo].[Archive1],[dbo].[Archive2]
where [dbo].[Archive1].id = [dbo].[Archive2].transactionNumber;
我能够编写适用于我的 use-case
的程序
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'getReport')
DROP PROCEDURE getReport
GO
CREATE PROCEDURE getReport AS
BEGIN
DECLARE @nowLocal DATETIMEOFFSET = getDate() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'
DECLARE @midnightLocal DATETIMEOFFSET = CONVERT(DATETIME2, CAST(@nowLocal as DATE)) AT TIME ZONE 'Central Standard Time'
DECLARE @dayStart DATETIMEOFFSET = DATEADD(hh, -17, @midnightLocal)
DECLARE @reportTime DATETIMEOFFSET = DATEADD(hh, 07, @midnightLocal)
if @nowlocal < @reportTime
SELECT @dayStart = DATEADD(d, -1, @dayStart)
DECLARE @dayEnd DATETIMEOFFSET = DATEADD(ms, 86399999, @dayStart)
select @dayStart as dayStart, @dayEnd as dayEnd, @reportTime as reportTime, @midnightLocal as midnight
select [dbo].[Archive1].id,[dbo].[Archive1].loadId,[dbo].[Archive2].costResponse
from [dbo].[Archive1],[dbo].[Archive2]
where [dbo].[Archive1].id = [dbo].[Archive2].transactionNumber
BETWEEN @dayStart AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC' and @dayEnd AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'UTC'
END