试图与我的员工表合并表

时间:2018-07-06 20:34:28

标签: sql sql-server

所以首先我做了这个插入

 [JsonProperty("#text")]
 public string text { get; set; }

这很完美,但是随后我想从该员工表中获取所有用户并合并到我的vd表中

INSERT INTO vDDUP(VPUserName, ShowRates, RestrictedBatches, FullName, Phone, EMail, ConfirmUpdate, DefaultCompany, EnterAsTab, ExtendControls, SavePrinterSettings, SmartCursor, ToolTipHelp, MinimizeUse, AccessibleOnly, MenuAdmin, FormAdmin, MultiFormInstance, HideModFolders, MenuInfo, MergeGridKeys, AltGridRowColors, DefaultDestType, WindowsUserName,PRCo, Employee, MyTimesheetRole, IsHelpUpToDate, ShowLogoPanel, ShowMainToolbar, UserType, SaveLastUsedParameters, ReportViewerOptions, PMErrorCorrectionSubcontracts, PMErrorCorrectionSubcontractCOs, PMErrorCorrectionPurchaseOrders, PMErrorCorrectionPurchaseOrderCOs, AutoLogOutFlag, CSAllowDocEdit, CSSelectAllAttachments, LookupColumnAutoSize, AllowExternalLogin, LoginFailCount, AttachmentListerStyle, ShowUnsavedChangesOnStartup, DisallowSelfSecurityYN, SuppressMaxWarningRows, EnableSharedSearch)
select E.Username, 'N', 'N', E.Employee_FName + ' ' + E.Employee_LName, E.Phone_Number, E.Email, 'N', 1, 'Y', 'N', 'N', 'N', 'Y', 'N', 'N', 'N', 'N', 'N', 'N', 0, 'N', 'N', 'EMail', 'domain\' + E.Username, 1, E.Employee_ID, 1, 'Y', 'Y', 'Y', 0, 'N', 0, 'Y', 'Y', 'Y', 'Y', 'N', 'Y', 'Y', 'Y', 'N', 0, 3, 'Y', 'N', 'N', 'N'
from [server].[db].[dbo].[Employees] as E
where E.Username = 'user'

因此,我会将具有用户名的所有用户合并到我的vd中,并传递我需要设置的值以使其登录。

由于某种原因,我没有为E的第2、3、5列指定任何列名

说的是第33行,这是我的陈述所在的地方。

所以使用某些语法的原因是因为employees表位于差异服务器和数据库上,而不是vd表。

1 个答案:

答案 0 :(得分:1)

这很接近您的需求。我有可以使用的表,因此无法测试。另外请注意我是如何分散这些列的,以便您可以看到它们而不是一堵墙。

MERGE vDDUP v
Using
( 
    select E.Username
        , E.Employee_FName
        , E.Employee_LName
        , E.Phone_Number
        , E.Email
        , E.Username
        , E.Employee_ID
    from [server].[db].[dbo].[Employees] E 
    where E.Username is not null 
) E on (E.[Username] collate SQL_Latin1_General_CP1_CI_AS = v.[VPUserName] collate SQL_Latin1_General_CP1_CI_AS)
when Matched 
Then update 
    set PRCo = 1
        , Employee = E.Employee_ID
When Not Matched By Target
Then Insert
(
    VPUserName
    , ShowRates
    , RestrictedBatches
    , FullName
    , Phone
    , EMail
    , ConfirmUpdate
    , DefaultCompany
    , EnterAsTab
    , ExtendControls
    , SavePrinterSettings
    , SmartCursor
    , ToolTipHelp
    , MinimizeUse
    , AccessibleOnly
    , MenuAdmin
    , FormAdmin
    , MultiFormInstance
    , HideModFolders
    , MenuInfo
    , MergeGridKeys
    , AltGridRowColors
    , DefaultDestType
    , WindowsUserName
    , PRCo
    , Employee
    , MyTimesheetRole
    , IsHelpUpToDate
    , ShowLogoPanel
    , ShowMainToolbar
    , UserType
    , SaveLastUsedParameters
    , ReportViewerOptions
    , PMErrorCorrectionSubcontracts
    , PMErrorCorrectionSubcontractCOs
    , PMErrorCorrectionPurchaseOrders
    , PMErrorCorrectionPurchaseOrderCOs
    , AutoLogOutFlag
    , CSAllowDocEdit
    , CSSelectAllAttachments
    , LookupColumnAutoSize
    , AllowExternalLogin
    , LoginFailCount
    , AttachmentListerStyle
    , ShowUnsavedChangesOnStartup
    , DisallowSelfSecurityYN
    , SuppressMaxWarningRows
    , EnableSharedSearch
)
Values
(
    E.Username
    , 'N'
    , 'N'
    , E.Employee_FName + ' ' + E.Employee_LName
    , E.Phone_Number
    , E.Email
    , 'N'
    , 1
    , 'Y'
    , 'N'
    , 'N'
    , 'N'
    , 'Y'
    , 'N'
    , 'N'
    , 'N'
    , 'N'
    , 'N'
    , 'N'
    , 0
    , 'N'
    , 'N'
    , 'EMail'
    , 'domain\' + E.Username
    , 1
    , E.Employee_ID
    , 1
    , 'Y'
    , 'Y'
    , 'Y'
    , 0
    , 'N'
    , 0
    , 'Y'
    , 'Y'
    , 'Y'
    , 'Y'
    , 'N'
    , 'Y'
    , 'Y'
    , 'Y'
    , 'N'
    , 0
    , 3
    , 'Y'
    , 'N'
    , 'N'
    , 'N'
);