create table by using select command return data?

  • How to create table by using select command return data.

    Export utility not possible to create another data with table due to duplicate datab in tables.

    select status, * from tbl_contract_emp_master TR inner join TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    thanks

    ananda

  • select status, *

    into NewTable from tbl_contract_emp_master TR inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    select * from NewTable

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • error

    Msg 2705, Level 16, State 3, Line 1

    Column names in each table must be unique. Column name 'STATUS' in table 'NewTable' is specified more than once.

    1.

    [TBL_IRIS_IMAGES_MASTER]

    [GUID] [varchar](50) NOT NULL,

    [LEFT_IMAGE] [image] NULL,

    [RIGHT_IMAGE] [image] NULL,

    [IMAGE_FORMAT] [varchar](20) NULL,

    [DEVICE_ID] [varchar](20) NULL,

    [ENROLLTIME] [datetime] NULL,

    [OPERATORID] [int] NULL

    2.

    [TBL_CONTRACT_EMP_MASTER]

    [ID] [numeric](18, 0) NOT NULL,

    [GUID] [varchar](50) NULL,

    [EP_NO] [varchar](20) NULL,

    [NAME] [varchar](200) NULL,

    [CONTRACTOR_CODE] [varchar](50) NULL,

    [CONTRACTOR_NAME] [varchar](200) NULL,

    [SUBCONTRACTOR_NAME] [varchar](200) NULL,

    [PLANT_CD] [varchar](50) NULL,

    [SKILLCODE] [varchar](50) NULL,

    [WORK_AREA] [varchar](50) NULL,

    [DISCIPLINE] [varchar](50) NULL,

    [PASSTYPE] [varchar](50) NULL,

    [PRES_ADD] [varchar](200) NULL,

    [PRES_VILLAGE] [varchar](100) NULL,

    [PRES_TALUKA] [varchar](100) NULL,

    [PRES_DISTRICT] [varchar](100) NULL,

    [PRES_TEHSIL] [varchar](100) NULL,

    [PRES_CITY] [varchar](75) NULL,

    [PRES_STATE] [varchar](50) NULL,

    [PRES_PIN] [varchar](15) NULL,

    [PRES_TEL_NO] [varchar](50) NULL,

    [FATHER_HUS_NAME] [varchar](100) NULL,

    [DT_BIRTH] [datetime] NULL,

    [DT_JOIN] [datetime] NULL,

    [EP_VALID_TILL] [datetime] NULL,

    [LICENSE_NO] [varchar](50) NULL,

    [GENDER] [varchar](10) NULL,

    [BLOOD_GROUP] [varchar](10) NULL,

    [RELIGION_CD] [varchar](50) NULL,

    [ID_MARK] [varchar](150) NULL,

    [HEIGHT] [smallint] NULL,

    [DT_LEAVE] [datetime] NULL,

    [STATUS] [char](1) NULL,

    [COMMENT] [varchar](200) NULL,

    [ENROLLTIME] [datetime] NULL,

    [OPERATORID] [int] NULL,

    [MODIFY_DT] [datetime] NULL,

    [CONTRACTOR_TYPE] [varchar](80) NULL,

    [WORKAREA_CATEGORY] [varchar](50) NULL,

  • you'll need to specify each column in the select statement and for the dupe column names use AS to rename.

    eg

    SELECT

    a.dupe_col as dupe_col_1

    ,b.dupe_col as dupe_col_2

    from tableA a

    join tableB b on ..............

  • Just change the name of the first "Status" field that you are returning to something else. It's not a field from a table, so it's already a name you've created...

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

  • You mentioned status field explicitly in select query and put * so there is already one status is filed there.so please remove first status.

    This query will execute sucessfully

    IF EXISTS(SELECT 1 FROM sys.objects where name='NewTable' and type='U')

    begin

    DROP TABLE NewTable

    end

    GO

    select *

    into NewTable from tbl_contract_emp_master TR inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • sorry Boss, the same error come again

    I m using SQL 2K8 R2 64 bit-version.

    IF EXISTS(SELECT 1 FROM sys.objects where name='NewTable' and type='U')

    begin

    DROP TABLE NewTable

    end

    GO

    select *

    into NewTable from tbl_contract_emp_master TR inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    Erorr

    Msg 2705, Level 16, State 3, Line 1

    Column names in each table must be unique. Column name 'GUID' in table 'NewTable' is specified more than once.

    this below code working fine, but not able to create table New Table

    select tr.[GUID]as gid,[EP_NO],[NAME],

    [CONTRACTOR_CODE],[CONTRACTOR_NAME],

    [SUBCONTRACTOR_NAME],[PLANT_CD],[SKILLCODE],[WORK_AREA],

    [DISCIPLINE],[PASSTYPE],[PRES_ADD],[PRES_VILLAGE],

    [PRES_TALUKA],[PRES_DISTRICT],[PRES_TEHSIL],[PRES_CITY],

    [PRES_STATE],[PRES_PIN],[PRES_TEL_NO],[FATHER_HUS_NAME],

    [DT_BIRTH],[DT_JOIN],[EP_VALID_TILL],[LICENSE_NO],

    [GENDER],[BLOOD_GROUP],[RELIGION_CD],[ID_MARK],[HEIGHT],

    [DT_LEAVE],[COMMENT],[STATUS],tr.[ENROLLTIME] as EntTime,

    tr.[OPERATORID] as OptID, [MODIFY_DT],[CONTRACTOR_TYPE],[WORKAREA_CATEGORY]

    from TBL_CONTRACT_EMP_MASTER TR

    inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.[GUID]=tm.[GUID]

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

  • Just a suggestion to try, I can't tell if you tried something like this or not:

    select tr.[GUID]as gid,[EP_NO],[NAME],

    [CONTRACTOR_CODE],[CONTRACTOR_NAME],

    [SUBCONTRACTOR_NAME],[PLANT_CD],[SKILLCODE],[WORK_AREA],

    [DISCIPLINE],[PASSTYPE],[PRES_ADD],[PRES_VILLAGE],

    [PRES_TALUKA],[PRES_DISTRICT],[PRES_TEHSIL],[PRES_CITY],

    [PRES_STATE],[PRES_PIN],[PRES_TEL_NO],[FATHER_HUS_NAME],

    [DT_BIRTH],[DT_JOIN],[EP_VALID_TILL],[LICENSE_NO],

    [GENDER],[BLOOD_GROUP],[RELIGION_CD],[ID_MARK],[HEIGHT],

    [DT_LEAVE],[COMMENT],[STATUS],tr.[ENROLLTIME] as EntTime,

    tr.[OPERATORID] as OptID, [MODIFY_DT],[CONTRACTOR_TYPE],[WORKAREA_CATEGORY]

    -------------------------- put the 'into' here --------------------------------- <<<<<<<<<<<<<<<<<<

    INTO NEW_TABLE_NAME_HERE

    -------------------------- end new text -------------------------------------- <<<<<<<<<<<<<<<<<<

    from TBL_CONTRACT_EMP_MASTER TR

    inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.[GUID]=tm.[GUID]

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

  • ananda.murugesan (10/24/2012)


    sorry Boss, the same error come again

    I m using SQL 2K8 R2 64 bit-version.

    IF EXISTS(SELECT 1 FROM sys.objects where name='NewTable' and type='U')

    begin

    DROP TABLE NewTable

    end

    GO

    select *

    into NewTable from tbl_contract_emp_master TR inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    Erorr

    Msg 2705, Level 16, State 3, Line 1

    Column names in each table must be unique. Column name 'GUID' in table 'NewTable' is specified more than once.

    this below code working fine, but not able to create table New Table

    select tr.[GUID]as gid,[EP_NO],[NAME],

    [CONTRACTOR_CODE],[CONTRACTOR_NAME],

    [SUBCONTRACTOR_NAME],[PLANT_CD],[SKILLCODE],[WORK_AREA],

    [DISCIPLINE],[PASSTYPE],[PRES_ADD],[PRES_VILLAGE],

    [PRES_TALUKA],[PRES_DISTRICT],[PRES_TEHSIL],[PRES_CITY],

    [PRES_STATE],[PRES_PIN],[PRES_TEL_NO],[FATHER_HUS_NAME],

    [DT_BIRTH],[DT_JOIN],[EP_VALID_TILL],[LICENSE_NO],

    [GENDER],[BLOOD_GROUP],[RELIGION_CD],[ID_MARK],[HEIGHT],

    [DT_LEAVE],[COMMENT],[STATUS],tr.[ENROLLTIME] as EntTime,

    tr.[OPERATORID] as OptID, [MODIFY_DT],[CONTRACTOR_TYPE],[WORKAREA_CATEGORY]

    from TBL_CONTRACT_EMP_MASTER TR

    inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.[GUID]=tm.[GUID]

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    sorry Boss, the same error come again

    I m using SQL 2K8 R2 64 bit-version.

    IF EXISTS(SELECT 1 FROM sys.objects where name='NewTable' and type='U')

    begin

    DROP TABLE NewTable

    end

    GO

    select *

    into NewTable from tbl_contract_emp_master TR inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.guid=tm.guid

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    Erorr

    Msg 2705, Level 16, State 3, Line 1

    Column names in each table must be unique. Column name 'GUID' in table 'NewTable' is specified more than once.

    this below code working fine, but not able to create table New Table

    select tr.[GUID]as gid,[EP_NO],[NAME],

    [CONTRACTOR_CODE],[CONTRACTOR_NAME],

    [SUBCONTRACTOR_NAME],[PLANT_CD],[SKILLCODE],[WORK_AREA],

    [DISCIPLINE],[PASSTYPE],[PRES_ADD],[PRES_VILLAGE],

    [PRES_TALUKA],[PRES_DISTRICT],[PRES_TEHSIL],[PRES_CITY],

    [PRES_STATE],[PRES_PIN],[PRES_TEL_NO],[FATHER_HUS_NAME],

    [DT_BIRTH],[DT_JOIN],[EP_VALID_TILL],[LICENSE_NO],

    [GENDER],[BLOOD_GROUP],[RELIGION_CD],[ID_MARK],[HEIGHT],

    [DT_LEAVE],[COMMENT],[STATUS],tr.[ENROLLTIME] as EntTime,

    tr.[OPERATORID] as OptID, [MODIFY_DT],[CONTRACTOR_TYPE],[WORKAREA_CATEGORY]

    INTO newTABLE

    from TBL_CONTRACT_EMP_MASTER TR

    inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.[GUID]=tm.[GUID]

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

    Because you error guid column name is TBL_CONTRACT_EMP_MASTER,TBL_CONTRACT_EMP_MASTER

    so you are getting this erro use.alias name if there is same column name in different tables.

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • shouldn't it be this?

    If you're still getting problems run it with the table getting created with one column, then run it so it gets created with two columns, then three etc etc

    IF EXISTS(SELECT 1 FROM sys.objects where name='NewTable' and type='U')

    begin

    DROP TABLE NewTable

    end

    GO

    select tr.[GUID]as gid,[EP_NO],[NAME],

    [CONTRACTOR_CODE],[CONTRACTOR_NAME],

    [SUBCONTRACTOR_NAME],[PLANT_CD],[SKILLCODE],[WORK_AREA],

    [DISCIPLINE],[PASSTYPE],[PRES_ADD],[PRES_VILLAGE],

    [PRES_TALUKA],[PRES_DISTRICT],[PRES_TEHSIL],[PRES_CITY],

    [PRES_STATE],[PRES_PIN],[PRES_TEL_NO],[FATHER_HUS_NAME],

    [DT_BIRTH],[DT_JOIN],[EP_VALID_TILL],[LICENSE_NO],

    [GENDER],[BLOOD_GROUP],[RELIGION_CD],[ID_MARK],[HEIGHT],

    [DT_LEAVE],[COMMENT],[STATUS],tr.[ENROLLTIME] as EntTime,

    tr.[OPERATORID] as OptID, [MODIFY_DT],[CONTRACTOR_TYPE],[WORKAREA_CATEGORY]

    into NewTable from tbl_contract_emp_master TR inner join

    from TBL_CONTRACT_EMP_MASTER TR

    inner join

    TBL_IRIS_IMAGES_MASTER tm on tr.[GUID]=tm.[GUID]

    and EP_NO in ('104536884','104541256','104564417','t12035676','104567770','104556221','104567536')

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply