sql - statement for update - id

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default sql - statement for update - id

    Sorry, my knowledge in SQL is not much to cheer for.

    Hope anyone can help me with this statement:

    I have two column : employeeID and employeeName and DB is employee.

    In employeeName have 22 names.

    If i will add two new names and have two new ID, ie 23 and 24 but now I will
    take away id 23,

    this I want to see id move up to 23. How possible to write expression in sql?

    <cfquery name="utdata" datasource="palett">
    select Max(employeeID) AS MaxID
    from employee
    </cfquery>

    <cfquery datasource="palett" name="ans">
    SELECT foroefterNamn, employeeID
    FROM employee
    where employeeID = #utdata.MaxID#
    </cfquery>

    // Pilem



    pilem17 Guest

  2. Similar Questions and Discussions

    1. Select-And-Update in one statement?
      On my site I often do SELECT counter from counters where id = 10 UPDATE counter set counter = counter + 1 Can I somehow merge them into one...
    2. Update Statement error
      Hello, Ii'm receiving the syntax error listed below for my update statement. Doesn't anyone have any ideas? Syntax error or access...
    3. Generated Update Statement
      I have a DataGrid that contains records from a SQL view. I would like to be able to update those records from the DataGrid. I am getting this...
    4. update statement
      Hi Try: Update T1 SET Fld2 = T2.Fld2 FROM Table1 T1 JOIN Table2 T2 ON T1.Fld1 = T2.Fld1 John
    5. Update and Select Statement
      Hi to all, I have VB application calling a SP. It works when: --return this to application SELECT * FROM tblVariance WHERE VarianceID =...
  3. #2

    Default Re: sql - statement for update - id

    What you want to do seems like a really bad idea, but it's your application.
    Assuming employeeid is not your primary key (in some databases, single field
    primary keys can't be updated), and it's numeric, this is your query

    update employee
    set employeeid = employeeid -1
    where employeeid >= 23

    Dan Bracuk Guest

  4. #3

    Default Re: sql - statement for update - id

    If your employeeID field is a primary key, and you EVER want to relate other
    entities to it with a foreign key, then what you want to do is a VERY VERY VERY
    bad idea. It sounds like you desperately need some lessons on data modeling,
    and how a relational database is supposed to work.

    [url]http://www.utexas.edu/its/windows/database/datamodeling/index.html[/url]

    Phil

    paross1 Guest

  5. #4

    Default Re: sql - statement for update - id

    I am only interested to see ordinary ID as MAX after employeeID 22 and if 23 is
    taken away, so it will be back in same id as 23
    when add new name.

    Because I have a locked list where 1-22 with name will not changed anything
    but if add new name so it doesn't matter when
    update or delete away this id 23 away from the datebase.

    That wat my question.



    pilem17 Guest

  6. #5

    Default Re: sql - statement for update - id

    Another alternative to update your Employee ID is as follows:

    First read all the records in the database:
    <cfquery name="ReadId" DataSource="DBSrc">
    SELECT * FROM Employee
    </cdquery>

    Suppose you have the delete query as:
    <cfquery name="deleteID" datasource="DBSrc">
    Delete FROM Employee
    Where employeeid=22
    </cfquery>

    Now you can have a loop that updates all the records from 23 onwards:

    <cfloop index="loop" from="23" to=#ReadID.RecordCount#>
    <cfquery name="UpdateID" DataSource="DBSrc">
    update employee
    set employeeid = employeeid -1
    where employeeid=#loop#
    </cfquery>
    </cfloop>

    This might not be the most efficient way, but its just a way to work around it.

    surenr Guest

  7. #6

    Default Re: sql - statement for update - id

    If I understand you question the way I would do it is.

    After you delete.....

    I did not test the syntax but it should give you an Idea.

    <cfquery name="EmpID" datasource="DBSrc">
    Select employeeid From Employee
    </cfquery>

    <cfset iID_Number = 1>
    <cfloop query="EmpID">
    <cfif employeeid GT iID_Number>
    <cfquery name="UpdateID" DataSource="DBSrc">
    update employee
    set employeeid = iID_Number
    where employeeid=#employeeid#
    </cfquery>
    </cfif>
    <cfset iID_Number = iID_Number+1>
    </cfloop>

    This should re-number your employee list.

    Rick



    "pilem17" <webforumsuser@macromedia.com> wrote in message
    news:dhggva$p2h$1@forums.macromedia.com...
    > Sorry, my knowledge in SQL is not much to cheer for.
    >
    > Hope anyone can help me with this statement:
    >
    > I have two column : employeeID and employeeName and DB is employee.
    >
    > In employeeName have 22 names.
    >
    > If i will add two new names and have two new ID, ie 23 and 24 but now I
    > will
    > take away id 23,
    >
    > this I want to see id move up to 23. How possible to write expression in
    > sql?
    >
    > <cfquery name="utdata" datasource="palett">
    > select Max(employeeID) AS MaxID
    > from employee
    > </cfquery>
    >
    > <cfquery datasource="palett" name="ans">
    > SELECT foroefterNamn, employeeID
    > FROM employee
    > where employeeID = #utdata.MaxID#
    > </cfquery>
    >
    > // Pilem
    >
    >
    >

    Rick Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139