Creating a new table with a unique field

  • Hi,

    I have a requirement to create a view with data from a query. The issue I am stuck with is how to create a unique field. Some of the  data rows are :-

     

    01/11/06 RES1


    Kindest Regards,

    Nick

  • What do you need to do exactly?  I have no (or too many) idea on how to help you!

  • Hi,

    The view is a mixture of data from multiple tables I need to have a ROWID number that is unqiue for each record.

    So... For example

    The row containing data for Machine1 would be say :-

    DESC, ID, DESC, POS1, POS2

     PRT1, 307,MACH1, 307, 624

    I would need to add a unique ROWID for each column so the data would look like

    ROWID,DESC, ID, DESC, POS1, POS2

    1, PRT1, 307,MACH1, 307, 624

    2, PRT1, 3087,MACH1, 308, 624

    etc etc

    I hope that makes sense!


    Kindest Regards,

    Nick

  • That makes sens.  Why do you need to have a row id in the data?  There are ways to do it but they all are pretty costly.  Can the application generate the ID when presenting the data?

  • Hi, Unfortunately not. The data source I am using does not have a unique column that can be used for each record, however the target database requires one.

    Sadly I have been told that data must have a unique field so the cost does not matter.

    Thanks


    Kindest Regards,

    Nick

  • Is this an export / import task or will the application will have to be able to update those records (using the view as datasource), cause that ain't gonna be possible?

  • I am exporting the data from one database into a data warehouse, ideally I would like the sql script to create the unique ID's as the application will not touch the data

    Regards,


    Kindest Regards,

    Nick

  • SELECT IDENTITY(int, 1,1), <list of columns>

    INTO #TempTable

    FROM <your query>

    SELECT * FROM #TempTable

    _____________
    Code for TallyGenerator

  • So would that be...

    SELECT IDENTITY(int, 1,1), DESC, ID, DESC, POS1, POS2 INTO #TempTable

    FROM <your query>

    so How would I apply that to a view?

     

    regards,

     


    Kindest Regards,

    Nick

  • You must do it in SP.

    You should not allow application to run queries on tables or views.

    _____________
    Code for TallyGenerator

  • Agreed, if you are to transform the data, then transform it permanently and let the users see the final data, not something autogenerated at each run.

  • The data is going into a table for presentation to an ETL process, I am merely creating a new view containing data from multiple tables. The target table requires a unique reference for each item starting at record 1 and incrementing as the view is populated with data from the other tables.

    So as I understand it from the threads above this is not possible?


    Kindest Regards,

    Nick

  • Yes create a temp table with identity(1,1), then insert the data to that table.  That'll create the unique id starting at one.  Also you can truncate that table and reseed the identity column to 1 so you can rerun that part of the code.

     

    Using a view to do this is possible but exponentially more costly as you add more rows.  I would strongly advise against this.  Especially if you have a lot of rows to transform.

  • Hi,

    Here is the code that I have to date for the view

    SELECT 

     CAST('' AS VARCHAR(20)) AS 'cre_nm',

            create_date AS 'cre_dt',

     update_date AS 'lst_updt_dt',

     CAST('FLAXBY' AS VARCHAR(20)) AS 'lst_updt_nm',

    --  CONVERT(VARCHAR(10),CAST(WW_EDIT_DATE AS DATETIME),112) AS 'lst_updt_dt',  

     CAST('N' AS VARCHAR) AS 'del_flg',

     CAST('QTMS' AS VARCHAR) AS 'src_syst_nm',

     CAST(machine_mode_id AS INT) AS 'mchn_mode_id',

     CAST(machine_mode_desc AS VARCHAR)AS 'mchn_mode_dscr',

     CAST('0' AS INTEGER) AS 'lds_dlay_cd',

     CAST('0' AS INTEGER) AS 'lds_oper_typ'

    FROM  MACHINE_MODE

    Any help would be appreciated.


    Kindest Regards,

    Nick

  • Hi,

    Here is the code that I have to date for the view

    SELECT 

     CAST('' AS VARCHAR(20)) AS 'cre_nm',

            create_date AS 'cre_dt',

     update_date AS 'lst_updt_dt',

     CAST('FLAXBY' AS VARCHAR(20)) AS 'lst_updt_nm',

    --  CONVERT(VARCHAR(10),CAST(WW_EDIT_DATE AS DATETIME),112) AS 'lst_updt_dt',  

     CAST('N' AS VARCHAR) AS 'del_flg',

     CAST('QTMS' AS VARCHAR) AS 'src_syst_nm',

     CAST(machine_mode_id AS INT) AS 'mchn_mode_id',

     CAST(machine_mode_desc AS VARCHAR)AS 'mchn_mode_dscr',

     CAST('0' AS INTEGER) AS 'lds_dlay_cd',

     CAST('0' AS INTEGER) AS 'lds_oper_typ'

    FROM  MACHINE_MODE

    Any help would be appreciated.


    Kindest Regards,

    Nick

Viewing 15 posts - 1 through 15 (of 21 total)

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