将数据从SQL Server视图导出到具有其他标题行的表

时间:2017-06-05 08:50:12

标签: sql-server database export

我正在使用视图来提供数据库中的表。在提供表格的同时,我还在表格中添加了一个标题行。我正在为5张桌子做这个。

但是,对于2个表,我可以看到插入最顶部的标题行,其余5个表,标题行向下。我该如何解决?

这是我用来提供数据的查询。我可以在JOB和EMPJOB表中看到标题为第1行。

INSERT INTO [JOB_Export](Job_Cd, Job_Name, Job_Note, Job_URL, OptJob_Currency, Domain_OrgCd, OptJob_Txt1, OptJob_Txt2, OptJob_Txt3, OptJob_Txt4, OptJob_Dt1, OptJob_Dt2, OptJob_Int1, OptJob_MONEY, OptJob_FLOAT1, OptJob_Ind1, OptJob_Memo1) 
    SELECT 
        'JOB_CODE' AS "JOB_CODE", 'JOB_NAME' AS "JOB_NAME",
        'JOB_DESC' AS "JOB_DESC", 'JOB_URL' AS "JOB_URL",
        'JOB_CURRENCY_CD' AS "JOB_CURRENCY_CD", 'JOB_DOMAIN' AS "JOB_DOMAIN", 
        'CUSTOM_TEXT1' AS "CUSTOM_TEXT1", 'CUSTOM_TEXT2' AS "CUSTOM_TEXT2",
        'CUSTOM_TEXT3' AS "CUSTOM_TEXT3", 'CUSTOM_TEXT4' AS "CUSTOM_TEXT4",
        'CUSTOM_DATE1' AS "CUSTOM_DATE1", 'CUSTOM_DATE2' AS "CUSTOM_DATE2",
        'CUSTOM_INTEGER1' AS "CUSTOM_INTEGER1", 'CUSTOM_MONEY1'AS "CUSTOM_MONEY1", 
        'CUSTOM_FLOAT1' AS "CUSTOM_FLOAT1", 'CUSTOM_IND1' AS "CUSTOM_IND1",
        'CUSTOM_MEMO1' AS "CUSTOM_MEMO1" 
    UNION
    SELECT  
        RTRIM(Job_Cd),
        Job_Name,
        CONVERT (VARCHAR(MAX),Job_Note) Job_Note,
        CONVERT (VARCHAR(255), Job_URL) Job_URL, 
        CONVERT (VARCHAR(50), OptJob_Currency) OptJob_Currency,
        Domain_OrgCd, 
        OptJob_Txt1,
        CONVERT (VARCHAR(255), OptJob_Txt2) OptJob_Txt2, 
        CONVERT (VARCHAR(255), OptJob_Txt3)  OptJob_Txt3 ,
        CONVERT (VARCHAR(255), OptJob_Txt4)  OptJob_Txt4 ,
        CONVERT (VARCHAR(500), OptJob_Dt1)  OptJob_Dt1 , 
        CONVERT (VARCHAR(500), OptJob_Dt2)  OptJob_Dt2 , 
        CONVERT (VARCHAR(500), OptJob_Int1)  OptJob_Int1 ,
        CONVERT (VARCHAR(500), OptJob_MONEY) OptJob_MONEY ,
        CONVERT (VARCHAR(500), OptJob_FLOAT1) OptJob_FLOAT1,
        CONVERT (VARCHAR(500), OptJob_Ind1)  OptJob_Ind1 ,
        CONVERT (VARCHAR(MAX), OptJob_Memo1)   OptJob_Memo1 
    FROM 
        [OVERRIDE_JOB_VIEW] 

INSERT INTO [EMPJOB_Export](Emp_No,Job_Cd,EmpJob_JoiningDate,EmpJob_PrmyInd,Domain_Cd)
SELECT 'EMP_NO' AS "EMP_NO",'JOB_CODE' AS "JOB_CODE",'JOIN_DATE' AS"JOIN_DATE",'PRIMARY' AS "PRIMARY",'JOB_DOMAIN' AS "JOB_DOMAIN"
UNION
SELECT [Emp_No], 
    CASE    WHEN LEN(Job_Cd)<=30 THEN CONVERT (VARCHAR(500), JOB_CD) 
    ELSE CONVERT(VARCHAR(500),SUBSTRING(rtrim([Job_Cd]),1,30))
    END AS Job_Cd,
    CONVERT(VARCHAR(500),[EmpJob_JoiningDate]) [EmpJob_JoiningDate] ,
    [EmpJob_PrmyInd],
    CASE    WHEN LEN(rtrim([Domain_Cd]))<=50 THEN [Domain_Cd]
    ELSE SUBSTRING(rtrim([Domain_Cd]),1,50)
    END AS [Domain_Cd]
    FROM [EMPJOB_VIEW]



INSERT INTO [EMPORG_Export] (Emp_No,Org_Cd,EmpOrg_JoiningDate,EmpOrg_RoleName,EmpOrg_PrmyInd,EmpOrg_DomainInd) 
SELECT 'EMP_NO' AS "EMP_NO",'ORG_CODE' AS "ORG_CODE",'JOIN_DATE' AS "JOIN_DATE",'ROLE' AS "ROLE",'PRIMARY' AS "PRIMARY",'DOMAIN' AS "DOMAIN"
UNION   
SELECT  [Emp_No],rtrim([Org_Cd]) AS [Org_Cd],[EmpOrg_JoiningDate],[EmpOrg_RoleName],    [EmpOrg_PrmyInd],[EmpOrg_DomainInd] FROM [EmpOrg_View]


INSERT INTO [ORG_Export]
(Org_Cd,Org_Name,POrg_Cd,HierType_Name,Org_DomainInd,Org_Note,Org_URL,Org_Add1,Org_Add2,Org_City,Org_State,Org_Zip,Org_Cntry,Org_Phn1,Org_Phn2,Org_Fax,OptOrg_Txt1,OptOrg_Txt2,OptOrg_Txt3,OptOrg_Txt4,OptOrg_Dt1,OptOrg_Dt2,OptOrg_Int1,OptOrg_FLOAT1,OptOrg_Ind1,OptOrg_MONEY,OptOrg_Memo1,OptOrg_Currency)
SELECT  'ORG_CODE'              AS "ORG_CODE",
    'ORG_NAME'              AS "ORG_NAME",
    'ORG_PARENT_CODE'       AS "ORG_PARENT_CODE",
    'HIERARCHY'             AS  "HIERARCHY",
    'DOMAIN_FLAG'           AS "DOMAIN_FLAG",
    'NOTE'                  AS "NOTE",
    'ORG_URL'               AS "ORG_URL",
    'ADDRESS1'              AS "ADDRESS1",
    'ADDRESS2'              AS "ADDRESS2",
    'CITY'                  AS "CITY",
    'STATE'                 AS "STATE",
    'ZIP'                   AS "ZIP",
    'COUNTRY'               AS "COUNTRY",
    'PHONE1'                AS "PHONE1",
    'PHONE2'                AS "PHONE2",
    'FAX'                   AS "FAX",
    'CUSTOM_TEXT1'          AS "CUSTOM_TEXT1",
    'CUSTOM_TEXT2'          AS "CUSTOM_TEXT2",
    'CUSTOM_TEXT3'          AS "CUSTOM_TEXT3",
    'CUSTOM_TEXT4'          AS "CUSTOM_TEXT4",
    'CUSTOM_DATE1'          AS "CUSTOM_DATE1",
    'CUSTOM_DATE2'          AS "CUSTOM_DATE2",
    'CUSTOM_INTEGER1'       AS "CUSTOM_INTEGER1",
    'CUSTOM_FLOAT1'         AS "CUSTOM_FLOAT1",
    'CUSTOM_IND1'           AS "CUSTOM_IND1",
    'CUSTOM_MONEY1'         AS "CUSTOM_MONEY1",
    'CUSTOM_MEMO1'          AS "CUSTOM_MEMO1",
    'CUSTOM_CURRENCY_CODE'  AS "CUSTOM_CURRENCY_CODE"
UNION
SELECT rtrim([Org_Cd]) AS [Org_Cd],[Org_Name],[POrg_Cd],[HierType_Name],
CASE    WHEN LEN(RTRIM([Org_DomainInd]))=1  THEN RTRIM([Org_DomainInd])
        ELSE SUBSTRING(rtrim([Org_DomainInd]),1,1)
END AS [Org_DomainInd],
[Org_Note],[Org_URL],[Org_Add1],[Org_Add2], 
CASE    WHEN LEN(RTRIM([Org_City]))<=30 THEN RTRIM([Org_City])
        ELSE SUBSTRING(rtrim([Org_City]),1,30)
END AS [Org_City],
[Org_State],[Org_Zip],[Org_Cntry],  
CASE    WHEN LEN(RTRIM([Org_Phn1]))<=30 THEN RTRIM([Org_Phn1])
        ELSE SUBSTRING(rtrim([Org_Phn1]),1,30)
END AS [Org_Phn2],
CASE    WHEN LEN(RTRIM([Org_Phn2]))<=30 THEN RTRIM([Org_Phn2])
        ELSE SUBSTRING(rtrim([Org_Phn2]),1,30)
END AS [Org_Phn2],
CASE    WHEN LEN(RTRIM([Org_Fax]))<=30 THEN RTRIM([Org_Fax])
        ELSE SUBSTRING(rtrim([Org_Fax]),1,30)
END AS [Org_Fax],   
[OptOrg_Txt1],
[OptOrg_Txt2],[OptOrg_Txt3],[OptOrg_Txt4],[OptOrg_Dt1], [OptOrg_Dt2],[OptOrg_Int1],[OptOrg_FLOAT1],
CASE    WHEN LEN(RTRIM([OptOrg_Ind1]))=1  THEN RTRIM([OptOrg_Ind1])
        ELSE SUBSTRING(rtrim([OptOrg_Ind1]),1,1)
END AS [OptOrg_Ind1],
[OptOrg_MONEY],[OptOrg_Memo1],[OptOrg_Currency]
FROM [Org_View]



INSERT INTO [EMP_Export]
(Emp_No,Emp_Title,Emp_LName,Emp_FName,Emp_MI,Emp_Suffix,Emp_Ttl,EmpCd_Name,EmpStat_Name,Mgr_No,DefApprEmp_No,Emp_StartDt,Emp_EndDt,Emp_LastRvwDt,Emp_Add1,Emp_Add2,Emp_City,Emp_State,Emp_Zip,Emp_Cntry,Emp_Email,Emp_Phn1,Emp_Phn2,Emp_Fax,Emp_URL,Emp_Note,EmpDomain_Cd,Org_Cd,Job_Cd,Emp_Active,Usr_Enabled,Usr_Name,Usr_NTLogin,Role_Name,Usr_Timezone,Usr_Currency,Usr_Language,OptEmp_Txt1,OptEmp_Txt2,OptEmp_Txt3,OptEmp_Txt4,OptEmp_Memo1,OptEmp_Dt1,OptEmp_Dt2,OptEmp_Ind1,OptEmp_MONEY,OptEmp_Int1,OptEmp_FLOAT1,Emp_Job_JoiningDate,PublishCalendar,ViewAllEmpsInd,IsMobileEnabled,MobileUserName)
SELECT  'EMP_NO'                AS "EMP_NO"
    ,'NAME_PREFIX_TEXT'     AS "NAME_PREFIX_TEXT"
    ,'LAST_NAME'            AS "LAST_NAME"
    ,'FIRST_NAME'           AS "FIRST_NAME"
    ,'MIDDLE_NAME'          AS "MIDDLE_NAME"
    ,'NAME_SUFFIX_TEXT'     AS "NAME_SUFFIX_TEXT"
    ,'CUSTOM_TEXT5'         AS "CUSTOM_TEXT5"
    ,'USER_CODE_TEXT'       AS "USER_CODE_TEXT"
    ,'USER_STATUS_TEXT'     AS "USER_STATUS_TEXT"
    ,'MANAGER_NO'           AS "MANAGER_NO"
    ,'TRNG_DEF_APPROVER_NO' AS "TRNG_DEF_APPROVER_NO"
    ,'START_DATE'           AS "START_DATE"
    ,'END_DATE'             AS "END_DATE"
    ,'LAST_REVIEW_DATE'     AS "LAST_REVIEW_DATE"
    ,'WORK_ADDRESS1'        AS "WORK_ADDRESS1"
    ,'WORK_ADDRESS2'        AS "WORK_ADDRESS2"
    ,'WORK_CITY'            AS "WORK_CITY"
    ,'WORK_STATE_TEXT'      AS "WORK_STATE_TEXT"
    ,'WORK_ZIP'             AS "WORK_ZIP"
    ,'WORK_COUNTRY_TEXT'    AS "WORK_COUNTRY_TEXT"
    ,'EMAIL'                AS "EMAIL"
    ,'WORK_PHONE'           AS "WORK_PHONE"
    ,'WORK_PHONE_EXT'       AS "WORK_PHONE_EXT"
    ,'WORK_FAX1'            AS "WORK_FAX1"
    ,'URL'                  AS "URL"
    ,'NOTES'                AS "NOTES"
    ,'PRIMARY_DOMAIN'       AS "PRIMARY_DOMAIN"
    ,'PRIMARY_ORG'          AS "PRIMARY_ORG"
    ,'PRIMARY_JOB'          AS "PRIMARY_JOB"
    ,'ACTIVE_FLAG'          AS "ACTIVE_FLAG"
    ,'LOGIN_ENABLED_FLAG'   AS "LOGIN_ENABLED_FLAG"
    ,'LOGIN_USERNAME'       AS "LOGIN_USERNAME"
    ,'NT_LOGIN'             AS "NT_LOGIN"
    ,'SECURITY_ROLE'        AS "SECURITY_ROLE"
    ,'TIMEZONE_CD'          AS "TIMEZONE_CD"
    ,'CURRENCY_TEXT'        AS "CURRENCY_TEXT"
    ,'LOCALE_CD'            AS "LOCALE_CD"
    ,'CUSTOM_TEXT1'         AS "CUSTOM_TEXT1"
    ,'CUSTOM_TEXT2'         AS "CUSTOM_TEXT2"
    ,'CUSTOM_TEXT3'         AS "CUSTOM_TEXT3"
    ,'CUSTOM_TEXT4'         AS "CUSTOM_TEXT4"
    ,'CUSTOM_MEMO1'         AS "CUSTOM_MEMO1"
    ,'CUSTOM_DATE1'         AS "CUSTOM_DATE1"
    ,'CUSTOM_DATE2'         AS "CUSTOM_DATE2"
    ,'CUSTOM_IND1'          AS "CUSTOM_IND1"
    ,'CUSTOM_MONEY1'        AS "CUSTOM_MONEY1"
    ,'CUSTOM_INTEGER1'      AS "CUSTOM_INTEGER1"
    ,'CUSTOM_FLOAT1'        AS "CUSTOM_FLOAT1"
    ,'PRIMARY_JOB_JOINDATE' AS "PRIMARY_JOB_JOINDATE"
    ,'AUTO_PUBLISH_CAL'     AS "AUTO_PUBLISH_CAL"
    ,'CAN_VIEW_ALL_USERS_FLAG' AS "CAN_VIEW_ALL_USERS_FLAG"
    ,'MOBILE_ENABLED'       AS "MOBILE_ENABLED"
    ,'MOBILE_USERNAME'      AS "MOBILE_USERNAME"
UNION
SELECT      Emp_No,
        Emp_Title,
        CASE    WHEN LEN(RTRIM(Emp_LName)) <= 50  THEN RTRIM(Emp_LName)
                ELSE SUBSTRING(rtrim(Emp_LName),1,50)
        END AS Emp_LName,
        CASE    WHEN LEN(RTRIM(Emp_FName)) <= 50  THEN RTRIM(Emp_FName)
                ELSE SUBSTRING(rtrim(Emp_FName),1,50)
        END AS Emp_FName,
        CASE    WHEN LEN(RTRIM(Emp_MI)) <= 50  THEN RTRIM(Emp_MI)
                ELSE SUBSTRING(rtrim(Emp_MI),1,50)
        END AS Emp_MI,
        Emp_Suffix,
        Emp_Ttl     ,
        EmpCd_Name  ,       
        EmpStat_Name,
        Mgr_No      ,
        CONVERT (VARCHAR(25),DefApprEmp_No) DefApprEmp_No,
        Emp_StartDt ,
        Emp_EndDt   ,
        Emp_LastRvwDt,
        Emp_Add1    ,
        Emp_Add2    ,
        CASE    WHEN LEN(RTRIM(Emp_City)) <= 50  THEN RTRIM(Emp_City)
                ELSE SUBSTRING(rtrim(Emp_City),1,50)
        END AS Emp_City,    
        CASE    WHEN LEN(RTRIM(Emp_State)) <= 35  THEN RTRIM(Emp_State)
                ELSE SUBSTRING(rtrim(Emp_State),1,35)
        END AS Emp_State,
        Emp_Zip     ,
        CASE    WHEN LEN(RTRIM(Emp_Cntry)) <= 50  THEN RTRIM(Emp_Cntry)
                ELSE SUBSTRING(rtrim(Emp_Cntry),1,50)
        END AS Emp_Cntry,
        Emp_Email,
         Emp_Phn1,
         Emp_Phn2,
         Emp_Fax,
         Emp_URL,
         Emp_Note       ,
         EmpDomain_Cd   ,
         rtrim(Org_Cd)  ,
         CONVERT (VARCHAR(500),rtrim(Job_Cd)) Job_Cd    ,
         Emp_Active     ,
         Usr_Enabled    ,
        CASE    WHEN LEN(RTRIM(Usr_Name)) <= 85  THEN RTRIM(Usr_Name)
                ELSE SUBSTRING(rtrim(Usr_Name),1,85)
        END AS Usr_Name,
         Usr_NTLogin    ,
        CASE    WHEN LEN(RTRIM(Role_Name)) <= 100  THEN RTRIM(Role_Name)
                ELSE SUBSTRING(rtrim(Role_Name),1,100)
        END AS Role_Name,
         Usr_Timezone   ,
         Usr_Currency   ,
         Usr_Language   ,
        CASE    WHEN LEN(RTRIM(OptEmp_Txt1)) <= 255  THEN RTRIM(OptEmp_Txt1)
                ELSE SUBSTRING(rtrim(OptEmp_Txt1),1,255)
        END AS OptEmp_Txt1,
        CASE    WHEN LEN(RTRIM(OptEmp_Txt2)) <= 255  THEN RTRIM(OptEmp_Txt2)
                ELSE SUBSTRING(rtrim(OptEmp_Txt2),1,255)
        END AS OptEmp_Txt2,
        CASE    WHEN LEN(RTRIM(OptEmp_Txt3)) <= 255  THEN RTRIM(OptEmp_Txt3)
                ELSE SUBSTRING(rtrim(OptEmp_Txt3),1,255)
        END AS OptEmp_Txt3,
        CASE    WHEN LEN(RTRIM(OptEmp_Txt4)) <= 255  THEN RTRIM(OptEmp_Txt4)
                ELSE SUBSTRING(rtrim(OptEmp_Txt4),1,255)
        END AS OptEmp_Txt4,
         OptEmp_Memo1   ,
         OptEmp_Dt1 ,
         OptEmp_Dt2 ,
         CONVERT (VARCHAR(500),OptEmp_Ind1) OptEmp_Ind1, 
        OptEmp_MONEY,
        OptEmp_Int1, 
        CONVERT (VARCHAR(500),OptEmp_FLOAT1) OptEmp_FLOAT1, 
        Emp_Job_JoiningDate,
        PublishCalendar,
        ViewAllEmpsInd  ,
        IsMobileEnabled ,
        MobileUserName  
FROM [Employee_VIEW]

1 个答案:

答案 0 :(得分:0)

不使用ORDER子句,从MSSQL Server中的SELECT返回的结果总是随机排序。由于MSSQL Server缓存功能(计划缓存),您有时会多次执行查询时看到相同的结果。

你可以使用额外的列进行排序,你可以依赖ORDER,RANK或最适合你的东西。