编写存储过程以在两个日期范围之间插入记录循环的最佳方法 [英] best possible ways to write the stored procedure to insert the record looping between two date range

查看:77
本文介绍了编写存储过程以在两个日期范围之间插入记录循环的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个 下表 我正在编写存储过程,将Employee Bank假期插入其出勤日志及其相应的 该日期的小时数。 因此,我必须通过提供两个日期来编写存储过程

I have Three  following table  and I am writing the stored procedure to insert Employee Bank holiday into its attendance Log with its corresponding  hours of that date.  So I have to write the stored procedure by giving two date

声明@ Fromdate  datetime

declare @Fromdate  datetime

声明@ ToDate    datetime

declare @ToDate   datetime

string @EmpID varchar(max)

string @EmpID varchar (max)

设置@ Fromdate  = '01 -01-2019'

Set @Fromdate  = '01-01-2019'

设置@ToDate = '12 -31-2019'

Set @ToDate = '12-31-2019'

设置@Empid ='1,2, 5,6,7'

set @Empid = '1,2,5,6,7'

我想在那之间插入所有银行假期  @fromdate和  @ TODATE 适用于所有员工及其相应的 在变量@Empid中为每个员工工作小时。 

I want to insert all bank holidays between that  @fromdate and  @todate  for all employess and its corresponding  working Hrs for each employee in the variable @Empid. 

我正在寻求帮助。我给出了表结构和记录下面

Please I am looking for the help . I have given the table structure and record below

EmpID int EmpName varchar MonHrs decimal (5,2) TueHrs decimal (5,2) WedHrs decimal (5,2) ThuHrs decimal (5,2) BankHoliday DateHoliday datetime Descritption varchar (max) AttendanceLog EmpID int Attdate datetime Hrs decimal (5,2) Descritption varchar (max)

Insert into Employee

Insert into Employee

(id,Name,MonHrs,TueHrs,WedHrs, ThuHrs)

(id, Name, MonHrs, TueHrs, WedHrs, ThuHrs)

(1, 'AA',2.3,4.3,5.3,3),

(1,'AA',2.3,4.3,5.3,3),

(2, 'B-B',3.15,4.3 ,7.30,2)

(2,'BB',3.15,4.3,7.30,2)

插入BankHoliday

Insert into BankHoliday

DateHoliday Descritption

('01 -01-2019','新年'),

('01-01-2019', 'New Year'),

( '10 -01-2019','其他假期'),

('06-05-2019','Holiday'),

(25-12-2019','Christmas')

我必须为给定日期和日期以及

选中 员工进入表 AttendanceLog表

我在Employee Master和BankHoliday中有以下记录

I have the following record in Employee Master and BankHoliday

polachan

推荐答案

如果您提供excel文件而不是我们无法使用的图像,它会有所帮助。

It can help if you provide the excel file instead of image which we cannot use.

更好的选择是提供查询以创建表格并插入示例数据

Better option is to provide queries to create the tables and insert the sample data

>>  我想在@fromdate和@ todate 之间插入所有银行假期对于变量@Empid中的每个员工的所有雇员及其相应的工作小时。 

>> I want to insert all bank holidays between that  @fromdate and  @todate  for all employess and its corresponding  working Hrs for each employee in the variable @Empid. 

您希望如何将表结构中的多个值插入单个值输入NVARCHAR?

How do you want to insert multiple values from table structure into a single value type NVARCHAR?

我们需要 连接所有行的所有值,a为此,我们需要知道您想要获得结果的格式

We will need to concatenation all the values from all rows, and for this we need to know which format you want to get the result

一般情况下,如果您使用的是SQL Server 2017及以上版本,则可以使用该函数  STRING_AGG
的任务

In general, if you are using SQL Server 2017 and above then you can use the function STRING_AGG for the task


这篇关于编写存储过程以在两个日期范围之间插入记录循环的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆