Need help to update supervisor in Sql - sql-server
I have a employee table , from where I am finding out the supervisor(team lead) and the manager details based on the User ID . I have a SSIS package which appends newly created Users into my table.
Below is what i am doing currently .
appending newly created user ID into my Sql table from the master table.
finding the supervisor and the manager , based on the employee USER ID.
Now , when an existing employee changes his manager or the supervisor, how can I update the new changes from the master table to my sql table. I dont want to truncate and load the table either since this table is used in an application.
here is the query:
-- load new employee details
INSERT INTO [dbo].[Active_Directory_COE]
(
[USER_ID]
, [USER_TYPE_NM]
, [USER_CLASS]
, [LAST_NM]
, [FIRST_NM]
, [MIDDLE_INIT]
, [EMAIL_ADDRESS]
, [EMP_JOB_DESC]
, [BUSINESS_UNIT_CD]
, [BUSINESS_UNIT_NM]
, [BUSINESS_SEGMENT]
, [SUPERVISOR_ID]
, [EMP_FULL_NM]
, [MANAGER_NAME]
, [SUPERVISOR_NAME]
, [SUPERVISOR_EMAIL_ADDRESS]
, [MANAGER_EMAIL_ADDRESS]
, [TEAM]
, [REGION]
, [SUB_REGION]
, [SALES_OFFICE]
, [SALES_GROUP]
, [EMPLOYEE_STATUS]
, [SURVEY_GROUP]
, [SURVEY_APPLICABLE]
, [REGION_CSSC]
, [SALES_ORG]
, [SALES_OFFICE_MULTIPLE]
, [TE_JOINING_DT]
)
SELECT
[A1].[USER_ID]
, [A1].[USER_TYPE_NM]
, [A1].[USER_CLASS]
, [A1].[LAST_NM]
, [A1].[FIRST_NM]
, [A1].[MIDDLE_INIT]
, [A1].[EMAIL_ADDRESS]
, [A1].[EMP_JOB_DESC]
, [A1].[BUSINESS_UNIT_CD]
, [A1].[BUSINESS_UNIT_NM]
, [A1].[BUSINESS_SEGMENT]
, [A1].[SUPERVISOR_ID]
, [A1].[EMP_FULL_NM]
, [D].[EMP_FULL_NM] AS [Manager_Name]
, [C].[EMP_FULL_NM] AS [Supervisor_name]
, [C].[EMAIL_ADDRESS] AS [Supr_Email_AD]
, [D].[EMAIL_ADDRESS] AS [Mangaer_Email_Ad]
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, [A1].[SERVICE_CREDIT_DT]
FROM
(
SELECT
[A].[USER_ID]
, [A].[USER_TYPE_NM]
, [A].[USER_CLASS]
, [A].[LAST_NM]
, [A].[FIRST_NM]
, [A].[MIDDLE_INIT]
, [A].[EMAIL_ADDRESS]
, [A].[EMP_JOB_DESC]
, [A].[BUSINESS_UNIT_CD]
, [A].[BUSINESS_UNIT_NM]
, [A].[BUSINESS_SEGMENT]
, [A].[SUPERVISOR_ID]
, [A].[EMP_FULL_NM]
, [A].[SERVICE_CREDIT_DT]
FROM [dbo].[Active_Directory] [A]
LEFT OUTER JOIN [dbo].[Active_Directory_COE] [B]
ON [A].[USER_ID] = [B].[USER_ID]
WHERE [B].[USER_ID] IS NULL
) [A1]
INNER JOIN [dbo].[Active_Directory] [C]
ON [C].[USER_ID] = [A1].[SUPERVISOR_ID]
INNER JOIN [dbo].[Active_Directory] [D]
ON [C].[SUPERVISOR_ID] = [D].[USER_ID];
Related
Error converting data type nvarchar to numeric while converting phone number
INSERT INTO PHONE_NUMBERS( [OWNER_ID] , OWNER_TYPE_ID , PHONE_NUMBER_TYPE_ID , AREA_CODE , PREFIX , PHONE_NUMBER_BODY , IS_ACTIVE) SELECT INS_COMPANY_ID , #OWNER_TYPE_ID OWNER_TYPE_ID , #PHONE_NUMBER_TYPE_ID PHONE_NUMBER_TYPE_ID , LEFT(try_convert(varchar(50), cast(([Insurance Phone]) as decimal(38,0) ) ),3) , substring( try_convert(varchar(50), cast(([Insurance Phone]) as decimal(38,0) ) ),4,3)PREFIX , RIGHT(try_convert(varchar(50), cast(([Insurance Phone]) as decimal(38,0) ) ),4) , 1 IS_ACTIVE FROM Insurance_Companies_28012020 EE JOIN INS_COMPANIES IC ON ee.[Insurance Name] = IC.COMPANY_NAME WHERE ic.is_active = 1 AND [Insurance Phone] IS NOT NULL Here my Phone is like 895-541-8541. This phone data is imported from Excel and table data type is varchar.
Your cast(([Insurance Phone]) as decimal won't work if there are dashes in the string. There is no reason to cast to a decimal before converting to varchar. If Insurance Phone is truly a varchar column, then there is a lot of unnecessary casting and converting in your query: INSERT INTO PHONE_NUMBERS( [OWNER_ID] , OWNER_TYPE_ID , PHONE_NUMBER_TYPE_ID , AREA_CODE , PREFIX , PHONE_NUMBER_BODY , IS_ACTIVE ) SELECT INS_COMPANY_ID , #OWNER_TYPE_ID OWNER_TYPE_ID , #PHONE_NUMBER_TYPE_ID PHONE_NUMBER_TYPE_ID , LEFT([Insurance Phone], 3) , substring( [Insurance Phone],4,3) PREFIX , RIGHT([Insurance Phone],4) , 1 IS_ACTIVE FROM Insurance_Companies_28012020 EE join INS_COMPANIES IC on ee.[Insurance Name] =IC.COMPANY_NAME where ic.is_active=1 and [Insurance Phone] is not null
while selecting the data from tables im getting the error
SQL : select E.ID , cp = ( select ISNULL(C.CNAME, E.CP) CP from TBL_EXPEDITE_MASTER E left join [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C on C.spid = E.CP ) , E.ECO_ORDER , E.DN , E.CBUK , E.XMPF_ID , E.CP , E.RECEIVED , E.CP_OWNER , E.CP_CONTACT_NUMBER , CP_EMAIL , E.CPREF , E.SITE_CONTACT_NAME , E.SITE_CONTACT_NUMBER , E.EXISTING_CRD , E.NEW_CRD , E.EXPEDITE_CRD , E.status , E.LINE , E.CHARGEABLE , E.RESPONSE , LAST_UPDATED , ORDER_TYPE , INITIALRESP , NOTES , BTW_EXPEDITE_REF --1.2 , ProductType --1.2 , InstallationAddress --1.2 , SITE_CONTACT_OTHER --1.2 , FastTrackReason --1.2 , FastTrackFOC --1.2 , FOC_Reason --1.2 , CP_EMAIL_OTHER --1.2 , Received_Via --1.2 , E2EData --1.2 , E.organisationName , E.postCode , E.thoroughfareDescriptor , E.county , E.country from TBL_EXPEDITE_MASTER E with (nolock) Error Message 1:
You are getting this error because in the First Select the SubQuery you are using has returned more than 1 Record for the same row. You May try using top 1 to avoid this error or any aggregate functions or Join the subquery as a separate CTE will also work fine. For a Quick Fix, you can change the query like this SELECT cp = ( SELECT TOP 1 ISNULL(C.CNAME, E.CP) CP FROM TBL_EXPEDITE_MASTER E LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C ON C.spid = E.CP ORDER BY C.CNAME, E.CP ); but looking at your query, I think this will also work SELECT E.ID, cp = ISNULL(C.CNAME, E.CP), E.ECO_ORDER, E.DN, E.CBUK, E.XMPF_ID, E.CP, E.RECEIVED, E.CP_OWNER, E.CP_CONTACT_NUMBER, CP_EMAIL, E.CPREF, E.SITE_CONTACT_NAME, E.SITE_CONTACT_NUMBER, E.EXISTING_CRD, E.NEW_CRD, E.EXPEDITE_CRD, E.STATUS, E.LINE, E.CHARGEABLE, E.RESPONSE, LAST_UPDATED, ORDER_TYPE, INITIALRESP, NOTES, BTW_EXPEDITE_REF, ProductType, InstallationAddress, SITE_CONTACT_OTHER, FastTrackReason, FastTrackFOC, FOC_Reason, CP_EMAIL_OTHER, Received_Via, E2EData, E.organisationName, E.postCode, E.thoroughfareDescriptor, E.county, E.country FROM TBL_EXPEDITE_MASTER E WITH (NOLOCK) LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C ON C.spid = E.CP
SSRS “Subquery returned more than 1 value”
I'm using MS Visual Studio 2012. I'm having an issue with writing a subquery. This is the entire query as follows: SELECT ACCOUNTNUM , NAME , ADDRESS , PHONE , TELEFAX , INVOICEACCOUNT , CUSTGROUP , PAYMTERMID , CURRENCY , DIMENSION , CELLULARPHONE , STATISTICSGROUP , PAYMMODE , NAMEALIAS , CONTACTPERSONID , STREET , PARTYID , SEGMENTID , TAXGROUP , DATAAREAID , ISNULL(( SELECT PERCENT1 AS DiscCount FROM PRICEDISCTABLE WHERE (DATAAREAID = CUSTTABLE.DATAAREAID) AND (ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM) ), 0) / 100 AS DiscCount , ( SELECT NAME FROM CONTACTPERSON WHERE (DATAAREAID = CUSTTABLE.DATAAREAID) AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM) AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID) ) AS ContactName , ( SELECT PHONE FROM CONTACTPERSON AS CONTACTPERSON_1 WHERE (DATAAREAID = CUSTTABLE.DATAAREAID) AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM) AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID) ) AS ContactPhone , ISNULL(( SELECT STATGROUPNAME FROM CUSTSTATISTICSGROUP WHERE (DATAAREAID = CUSTTABLE.DATAAREAID) AND (CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP) ), 0) AS StatisticsName FROM CUSTTABLE WHERE CUSTGROUP = #ty AND DATAAREAID = N'OTN' AND STATISTICSGROUP LIKE #ss; and then I get the error Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Where the issue comes from? Issue is that one of your subqueries returns more than one value (HEY, ERROR DESCRIPTION SAYS THAT). How to find which one? Try commenting out each subquery in your select statement until your query works fine. How to fix it? That's probably the best case to use CROSS APPLY. That'd be your query: SELECT ACCOUNTNUM , NAME , ADDRESS , PHONE , TELEFAX , INVOICEACCOUNT , CUSTGROUP , PAYMTERMID , CURRENCY , DIMENSION , CELLULARPHONE , STATISTICSGROUP , PAYMMODE , NAMEALIAS , CONTACTPERSONID , STREET , PARTYID , SEGMENTID , TAXGROUP , DATAAREAID , ISNULL(T1.DiscCount, 0) / 100 AS DiscCount , T2.ContactName , T3.ContactPhone , ISNULL(T4, 0) AS StatisticsName FROM CUSTTABLE CROSS APPLY ( SELECT PERCENT1 AS DiscCount FROM PRICEDISCTABLE WHERE DATAAREAID = CUSTTABLE.DATAAREAID AND ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM ) AS T1 CROSS APPLY ( SELECT NAME FROM CONTACTPERSON WHERE DATAAREAID = CUSTTABLE.DATAAREAID AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID ) AS T2 CROSS APPLY ( SELECT PHONE FROM CONTACTPERSON AS CONTACTPERSON_1 WHERE DATAAREAID = CUSTTABLE.DATAAREAID AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID ) AS T3 CROSS APPLY ( SELECT STATGROUPNAME FROM CUSTSTATISTICSGROUP WHERE DATAAREAID = CUSTTABLE.DATAAREAID AND CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP ) AS T4 WHERE CUSTGROUP = #ty AND DATAAREAID = N'OTN' AND STATISTICSGROUP LIKE #ss; Also, these can be rewritten as simple INNER JOINS.
When encountering this error, try running the subqueries individually and if more than one record is returned then refine your conditions to ensure only one record is returned. Of course, if more than one record is wanted then you need to change your query to use a join instead of a subquery.
What is the best way to delete bulk amount of rows in SQL Server 2008 R2
Here I am using a query, "delete from Table where id in" ({0})", string.Join(",", Key.ToArray()) Is any other best way to speed up this process? Note : I used indexes. Example : I used SQL profiller to traced it query is: delete from [Table] where Key in (27267 , 27268 , 27269 , 27270 , 27271 , 27272 , 27273 , 27274 , 27275 , 27276 , 27277 , 27278 , 27279 , 27280 , 27281 , 27282 , 27283 , 27284 , 27285 , 27286 , 27287 , 27288 , 27289 , 27290 , 27291 , 27292 , 27293 , 27294 , 27295 , 27296 , 27297 , 27298 , 27299 , 27300 , 27301 , 27302 , 27303 , 27304 , 27305 , 27306 , 27307 , 27308 , 27309 , 27310 , 27311 , 27312 , 27313 , 27314 , 27315 , 27316 , 27317 , 27318 , 27319 , 27320 , 27321 , 27322 , 27323 , 27324 , 27325 , 27326 , 27327 , 27328 , 27329 , 27330 , 27331 , 27332 , 27333 , 27334 , 27335 , 27336 , 27337 , 27338 , 27339 , 27340 , 27341 , 27342 , 27343 , 27344 , 27345 , 27346 , 27347 , 27348 , 27349 , 27350 , 27351 , 27352 , 27353 , 27354 , 27355 , 27356 , 27357 , 27358 , 27359 , 27360 , 27361 , 27362 , 27363 , 27364 , 27365 , 27366 , 27367 , 27368 , 27369 , 27370 , 27371 , 27372 , 27373 , 27374 , 27375 , 27376 , 27377 , 27378 , 27379 , 27380 , 27381 , 27382 , 27383 , 27384 , 27385 , 27386 , 27387 , 27388 , 27389 , 27390 , 27391 , 27392 , 27393 , 27394 , 27395 , 27396 , 27397 , 27398 , 27399 , 27400 , 27401 , 27402 , 27403 , 27404 , 27405 , 27406 , 27407 , 27408 , 27409 , 27410 , 27411 , 27412 , 27413 , 27414 , 27415 , 27416 , 27417 , 27418 , 27419 , 27420 , 27421 , 27422 , 27423 , 27424 , 27425 , 27426 , 27427 , 27428 , 27429 , 27430 , 27431 , 27432 , 27433 , 27434 , 27435 , 27436 , 27437 , 27438 , 27439 , 27440 , 27441 , 27442 , 27443 , 27444 , 27445 , 27446 , 27447 , 27448 , 27449 , 27450 , 27451 , 27452 , 27453 , 27454 , 27455 , 27456 , 27457 , 27458 , 27459 , 27460 , 27461 , 27462 , 27463 , 27464 , 27465 , 27466 , 27467 , 27468 , 27469 , 27470 , 27471 , 27472 , 27473 , 27474 , 27475 , 27476 , 27477 , 27478 , 27479 , 27480 , 27481 , 27482 , 27483 , 27484 , 27485 , 27486 , 27487 , 27488 , 27489 , 27490 , 27491 , 27492 , 27493 , 27494 , 27495 , 27496 , 27497 , 27498 , 27499 , 27500 , 27501 , 27502 , 27503 , 27504 , 27505 , 27506 , 27507 , 27508 , 27509 , 27510 , 27511 , 27512 , 27513 , 27514 , 27515 , 27516 , 27517 , 27518 , 27519 , 27520 , 27521 , 27522 , 27523 , 27524 , 27525 , 27526 , 27527 , 27528 , 27529 , 27530 , 27531 , 27532 , 27533 , 27534 , 27535 , 27536 , 27537 , 27538 , 27539 , 27540 , 27541 , 27542 , 27543 , 27544 , 27545 , 27546 , 27547 , 27548 , 27549 , 27550 , 27551 , 27552 , 27553 , 27554 , 27555 , 27556 , 27557 , 27558 , 27559 , 27560 , 27561 , 27562 , 27563 , 27564 , 27565 , 27566 , 27567 , 27568 , 27569 , 27570 , 27571 , 27572 , 27573 , 27574 , 27575 , 27576 , 27577 , 27578 , 27579 , 27580 , 27581 , 27582 , 27583 , 27584 , 27585 , 27586 , 27587 , 27588 , 27589 , 27590 , 27591 , 27592 , 27593 , 27594 , 27595 , 27596 , 27597 , 27598 , 27599 , 27600 , 27601 , 27602 , 27603 , 27604 , 27605 , 27606 , 27607 , 27608 , 27609 , 27610 , 27611 , 27612 , 27613 , 27614 , 27615 , 27616 , 27617 , 27618 , 27619 , 27620 , 27621 , 27622 , 27623 , 27624 , 27625 , 27626 , 27627 , 27628 , 27629 , 27630 , 27631 , 27632 , 27633 , 27634 , 27635 , 27636 , 27637 , 27638 , 27639 , 27640 , 27641 , 27642 , 27643 , 27644 , 27645 , 27646 , 27647 , 27648 , 27649 , 27650 , 27651 , 27652 , 27653 , 27654 , 27655 , 27656 , 27657 , 27658 , 27659 , 27660 , 27661 , 27662 , 27663 , 27664 , 27665 , 27666 , 27667 , 27668 , 27669 , 27670 , 27671 , 27672 , 27673 , 27674 , 27675 , 27676 , 27677 , 27678 , 27679 , 27680 , 27681 , 27682 , 27683 , 27684 , 27685 , 27686 , 27687 , 27688 , 27689 , 27690 , 27691 , 27692 , 27693 , 27694 , 27695 , 27696 , 27697 , 27698 , 27699 , 27700 , 27701 , 27702 , 27703 , 27704 , 27705 , 27706 , 27707 , 27708 , 27709 , 27710 , 27711 , 27712 , 27713 , 27714 , 27715 , 27716 , 27717 , 27718 , 27719 , 27720 , 27721 , 27722 , 27723 , 27724 , 27725 , 27726 , 27727 , 27728 , 27729 , 27730 , 27731 , 27732 , 27733 , 27734 , 27735 , 27736 , 27737 , 27738 , 27739 , 27740 , 27741 , 27742 , 27743 , 27744 , 27745 , 27746 , 27747 , 27748 , 27749 , 27750 , 27751 , 27752 , 27753 , 27754 , 27755 , 27756 , 27757 , 27758 , 27759 , 27760 , 27761 , 27762 , 27763 , 27764 , 27765 , 27766) Here I am deleting 500 records, its takes 6553 milliseconds duration.
This option will likely speed up the delete, but may not speed up the overall SQL processing time. So with that caveat... You could first insert the keys you want to delete into a temporary variable (or temp table). Then, you could use a join in the delete clause so you don't have to use an IN clause. Ex. DECLARE #MyTableVar table(id int) INSERT INTO #MyTableVar (id) VALUES (27267) //Repeat insert for other values, or use a different mechanism to insert keys //into the temp table/table variable. //Since that's not the focus of this question, I won't go into details on this //portion. ... DELETE FROM [your table] FROM [your table] T1 INNER JOIN #MyTableVar T2 ON T1.id= T2.id Obviously the Inserts will take time, so if you are concerned with the overall time of the transaction this may not help. But if making the delete portion faster is the primary concern (to minimize lock time, for example), then this may still be useful. See http://msdn.microsoft.com/en-us/library/ms189835.aspx for usage examples of a DELETE statement that includes a JOIN and http://msdn.microsoft.com/en-us/library/ms188927.aspx for info on a table variable.
how to fix sql case syntax
work on sql-server-2005 SELECT A.specific_customization_id , A.customization_id , A.customization_value , A.customization_price , A.customization_cost , A.customization_code , A.customization_check , A.sort_order , A.option_code , A.product_id , A.image_1 , A.image_2 , A.MainProductID , A.customization_product_id , A.inactive , A.customization_description , A.customization_select , A.UOM , A.allow_recur , auto_reorder = CASE A.MainProductID --it's an int type column WHEN NULL THEN A.allow_recur ELSE ( SELECT allow_recur FROM wc_product WHERE product_id = A.MainProductID ) END FROM dbo.wc_product_specific A WHERE A.product_id = 1133 Using the above query I want to set auto_reorder column value .want to set two login When My MainProductID column is null I want to set allow_recur column value When it’s not null I want to set another table column value My logic 2) works fine ,fail to understand why does 1) not work? Help me to fix the problem? If have any query plz ask thanks in advance.
Does this work:? SELECT A.specific_customization_id , A.customization_id , A.customization_value , A.customization_price , A.customization_cost , A.customization_code , A.customization_check , A.sort_order , A.option_code , A.product_id , A.image_1 , A.image_2 , A.MainProductID , A.customization_product_id , A.inactive , A.customization_description , A.customization_select , A.UOM , A.allow_recur , A.auto_reorder = CASE --it's an int type column WHEN A.MainProductID IS NULL THEN A.allow_recur ELSE B.allow_recur END FROM dbo.wc_product_specific A left outer join wc_product B on B.product_id = A.MainProductID WHERE A.product_id = 1133