【问题标题】:SQL Query behaving inappropriately.SQL 查询行为不当。
【发布时间】:2019-04-08 15:14:06
【问题描述】:

我是一名新手开发人员,但我当然知道一旦编写的脚本应该以某种方式运行,但在以下用例中,这显然不会发生。

我创建了一个包含 INSERT INTO SELECT 语句的脚本,该语句应该过滤掉重复项并将差异放入 STAGING 表中,并将过去 7 天的记录保存在 STAGING2 表中。我在后端运行 SQL 作业,以确保我的脚本每隔几分钟运行一次。现在,以前和现在发生过的奇怪的事情是作业继续正常工作,我的 FACT 表被正确填充,没有重复,但突然 JOBS 开始失败,因为不知何故重复进入系统。如果有人可以提供帮助,我将分享下面的代码。 (这是几个月来一直正常工作的相同代码)。

USE [PowerBI]
GO
/****** Object:  StoredProcedure [ALJAZEERA\Naeemf].[sp_fact_ticket]    Script Date: 11/4/2018 1:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [ALJAZEERA\Naeemf].[sp_fact_ticket]
AS
BEGIN
truncate table fact.ticket
insert into fact.ticket
select 
[ticket_type],
[Assigned_to],
[caller],
[Due_Date],
[Ticket_Number],
[active],
[Approval_History],
[Duration_Seconds],
[Assigned to],
[Email Address (Assigned to)],
[Assignment Group],
[Business_Duration],
[Closed By Email Address],
[Link],
[Location],
[Made_SLA],
[Opened By Full Name],
[Opened By Email Address],
[Opened at date],
[Reassignment_Count],
[Service_Offering],
[Short_Description],
[State Name],
[u_Type],
[Created On Date],
[u_Comments_Updated],
[u_One_To_One_Training],
[u_Priority_Changed],
[Vendor],
[Vendor_Number],
[Caller Primary Group],
[Opened By Primary Group],
[Closed By Primary Group],
[Category],
[CMDB_CI],
[Opened By Job Title],
[Opened Month Name],
[Opened Year],
[Opened at Day Name],
[Opened Quarter],
[Closed at year],
[Closed at month name],
[Closed at Date],
[Closed at quarter],
[Contact_Type],
[Sharepoint_Request],
[SD_Request],
[Priority Name],
[Assigned to Primary Group],
[closed by],
[Sys_ID],
[u_Confidential],
[close_notes],
[u_bomgar]
from
(
select  'Incident' as ticket_type, [assigned to] AS Assigned_to,[Caller ID Name] AS caller, number AS Ticket_Number,active as active,[Approval_History],Calendar_STC as Duration_Seconds,[Assigned to],[Email Address (Assigned to)],[Assignment Group],
Business_Duration,[Closed By Email Address],Due_Date,Link,[Location],Made_SLA,[Opened By Full Name],[Opened By Email Address],[Opened at date],Reassignment_Count,Service_Offering,Short_Description,[State Name],u_Type,[Created On Date],u_Comments_Updated,
[u_One_To_One_Training],u_Priority_Changed,u_Vendor as Vendor,u_Vendor_Number as Vendor_Number,[Caller Primary Group],[Opened By Primary Group],[Closed By Primary Group],Category,CMDB_CI,[Opened By Job Title],[Opened Month Name],[Opened Year],[Opened at date] as OD1,[Opened at Day Name],
[Opened Quarter],[Closed at year],[Closed at month name],[Closed at Date],[Closed at quarter],Contact_Type,null as Sharepoint_Request,null as SD_Request,[Priority Name],[Assigned to Primary Group],[closed by],[Sys_ID],u_confidential,
close_notes,[u_bomgar]
from [ALJAZEERA\Naeemf].[Incidents Hierarchy View]
UNION
select  'Task', [assigned to], NULL,  number,active,approval,calendar_stc,[Assigned To],[Assigned to Email],[Assignment Group],business_duration,[Closed By Email],due_date,Link,[Location],made_sla,[Opened By],[Opened By Email],opened_at,reassignment_count,[business_service],short_description,[State],sys_class_name,[opened_at],u_comments_updated,
u_one_to_one_training,u_priority_changed,u_vendor,u_vendor_number,null,[Opened By Primary Group],[Closed By Primary Group],u_first_category,[CMDB CI],[Opened By Title],[Opened at Month Name],[Opened at Year],opened_at,[Opened at Day Name],
[Opened at Quarter],[Closed at year],[Closed at Month Name],[Closed At],[Closed at Quarter],null,null,null,[Priority],[Assigned To Primary Group],[closed by name],null,u_confidential,close_notes,u_bomgar
from [ALJAZEERA\Naeemf].[Catalog Tasks Hierarchy View]
Union
select  'Request', null,null,number,active,approval,calendar_stc,[Assigned to],[Assigned to email],[Assignment Group],business_duration,[Closed By Email],due_date,link,[Location],made_sla,[Opened By],[Opened By Email],opened_at,reassignment_count,Service_Offering,short_description,request_state,sys_class_name,sys_created_on,u_comments_updated,
u_one_to_one_training,u_priority_changed,u_vendor,u_vendor_number,null, [Opened By Primary Group],[Closed By Primary Group],null,CMDB_CI,[Opened By Title],[Opened Month Name],[Opened Year],opened_at,[Opened WeekdayName],[Opened Quarter]
,[Closed Year],[Closed Month Name],closed_at,[Closed Quarter],contact_type,u_sharepoint_request,u_sd_request,null,null,[closed by],[sys_id],null,close_notes,null
from [ALJAZEERA\Naeemf].[Requests Hierarchy View]
Union
select  'Change',[assigned to],null,number,active,[approval_history],null,[Assigned To],[Assigned to Email],[Assignment Group],[business_duration],[Closed by Email],[due_date],null,[location],
[made_sla],[Opened By],[Opened By Email],[opened_at],[reassignment_count],[Service Offering],[short_description],[State],null,null,[u_comments_updated],[u_one_to_one_training],[u_priority_changed],
[u_vendor],[u_vendor_number],null,[Opened By Primary Group],[Closed by Primary Group],[category],[CMDB_CI],[Opened By Title],[Opened Month Name],[Opened Year],[opened_at] as OD1,null,
[Opened Quarter],[Closed Year],[Closed Month Name],[closed_at],[Closed Quarter],null,null,null,null,[Assigned to Primary Group],[Closed By],[sys_id],u_confidential,close_notes,null
from dbo.Fact_Change
Union
select  'Problem',[Assigned to],null,number,active,approval,null,[Assigned to],[Assigned to Email],[Assignment Group],business_duration,[Closed By Email],due_date,link,Null,made_sla,[Opened By],null,[Created Date],reassignment_count,Service_Offering,short_description,[state],sys_class_name,[Created Date],u_comments_updated,
u_one_to_one_training,u_priority_changed,u_vendor,u_vendor_number,null,null,null,u_category,CMDB_CI,[Opened By Title],[Created Month Name],[Created Year],[Created Date],[Created Day Name],[Created Quarter],[Closed Year],[Closed Month Name],[Closed at Date],
[Closed Quarter],null,null,null,[Priority],null,[closed by],[sys_id],u_confidential,close_notes,null
from [ALJAZEERA\Naeemf].[Problems Hierarchy View]) a
END

感谢我能得到的所有帮助。

谢谢。

【问题讨论】:

  • 什么是独特的列?你的工作是什么?这里没有足够的信息可以提供帮助
  • 嗨尼克。唯一标识符列是 [sys_id]。
  • 要添加它,合并的表没有重复项。我已经验证了所有这些。只有在 fact.ticket 表中,所有其他表都被联合起来,从几天开始重复出现。
  • 我也截断并使用了 SELECT DISTINCT 语句而不是 SELECT 但无济于事。
  • 您的插入语句没有列列表,这是不好的做法,但也意味着我们不知道您要加载到哪个列。即使我们知道数据是什么样的。开始故障排除的第一步是在插入语句中添加一个列列表。

标签: sql-server tsql stored-procedures duplicates


【解决方案1】:

您的查询是否在没有过程本身的情况下运行?

查看各种联合选择,您似乎有一个交叉列,其中覆盖了不同的数据类型/空值。如果表结构发生了变化,导致其中一列无法输入空值,这也可能导致您遇到的问题。

尝试从程序中删除查询,看看是否得到任何结果/得到什么结果。

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多